• XML案例(使用DOM4J解析XML文档)


    1.Demo1.java

    package cn.itcast.dom4j;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.List;

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import org.junit.Test;

    public class Demo1 {

    // 读取xml文档第二本书的:<书名>Java</书名>
    @Test
    public void test1() throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));
    Element root = document.getRootElement();
    Element book = (Element) root.elements("书").get(1);
    String value = book.element("书名").getText();
    System.out.println(value);
    }

    // 读取xml文档第二本书的属性:<书名 name="xx">Java</书名>
    @Test
    public void test2() throws Exception {

    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));
    Element root = document.getRootElement();
    Element book = (Element) root.elements("书").get(1);
    String value = book.element("书名").attributeValue("name");
    System.out.println(value);

    }

    // 在第一本书上添加一个新的售价:<售价>211元</售价>
    @Test
    public void addtest() throws Exception {

    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));
    Element root = document.getRootElement();
    Element book = (Element) root.elements("书").get(0);
    book.addElement("售价").setText("211元");

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("gb2312");

    XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
    format);
    writer.write(document);
    writer.close();
    }

    // 在第一本书指定位置上添加一个新的售价:<售价>209元</售价>
    @Test
    public void addtest2() throws Exception {

    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));

    Element book = (Element) document.getRootElement().element("书");
    List list = book.elements();// [书名,作者,售价,售价]

    Element price = DocumentHelper.createElement("售价");
    price.setText("209元");

    list.add(2, price);

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("gb2312");

    XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
    format);
    writer.write(document);
    writer.close();
    }

    // 删除上面添加的售价节点
    @Test
    public void delete() throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));

    Element price = (Element) document.getRootElement().element("书").element("售价");
    price.getParent().remove(price);

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("gb2312");

    XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
    format);
    writer.write(document);
    writer.close();
    }

    // 删除上面添加的售价节点
    @Test
    public void update() throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));

    Element book = (Element) document.getRootElement().elements("书").get(1);
    book.element("作者").setText("hh");

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("gb2312");

    XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
    format);
    writer.write(document);
    writer.close();
    }
    }

    2.Demo2.java

    package cn.itcast.dom4j;

    import java.io.File;
    import java.io.FileOutputStream;

    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import org.junit.Test;

    public class Demo2 {
    /**
    * 应用xpath提取xml文档的数据
    *
    */
    @Test
    public void test() throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/book.xml"));

    String value = document.selectSingleNode("//作者").getText();
    System.out.println(value);

    }
    }

    3.Demo3.java

    package cn.itcast.dom4j;

    import java.io.File;

    import org.dom4j.Document;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    import org.junit.Test;

    public class Demo3 {
    /**
    * 查找user.xml文档是否有和用户相匹配的用户名和密码
    *
    */
    @Test
    public void test() throws Exception {
    String username = "aaa";
    String password = "123";

    SAXReader reader = new SAXReader();
    Document document = reader.read(new File("src/users.xml"));

    Node node = document.selectSingleNode("//user[@username='" + username
    + "' and @password='" + password + "']");
    if (node == null)
    System.out.println("用户名或密码错误!");
    else
    System.out.println("登录成功!");

    }
    }

  • 相关阅读:
    C# WinForm 取消DataGridView的默认选中Cell 使其不反蓝
    我们 成就了每个我的世界
    [转]firefox与IE的事件处理
    C# WinForm CheckedListBox 使用的相关简单总结
    [书目20090216]高绩效人士的五项管理 李践作品
    [转]asp.net导出Excel/Csv格式数据最优方案(C#)
    [文摘20090201]男女朋友~~需记住亦舒的77句话
    WML 参考手册
    [引]ASP.NET 移动网页 与 如何:使用仿真程序和浏览器在部署移动 Web 应用程序之前对其进行测试
    [文摘20090203]3G知识入门讲座
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359566.html
Copyright © 2020-2023  润新知