Dom4j随笔

 

 

DOM4J中获取Document的方法有三种:

 

<!--[if !supportLists]-->1、  <!--[endif]-->读取XML文件,获得document对象

SAXReader    reader=new  SAXReader();

Document    document=reader.read(new  File(“Input.xml”));

2.解析XML形式的文本,得到document对象.

String text = "<members></members>";
Document document = DocumentHelper.parseText(text);

 

3.主动创建document对象.

Document document = DocumentHelper.createDocument();
//
创建根节点

Element root = document.addElement("members");

利用dom4j解析xml文档中的元素和属性

xml文档中的代码  book.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books[
    <!ELEMENT books (book*)>
    <!ELEMENT book (name,author,price)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
    <!ATTLIST author address CDATA #IMPLIED>
]>
 
<books>
   <book>
       <name>Java开发</name>
       <author>m_j1</author>
       <price>10000</price>
   </book>
  
   <book>
       <name>Java开发</name>
       <author>m_j2</author>
       <price>10000</price>
   </book>
  
   <book>
       <name>Java开发</name>
       <author>m_j3</author>
       <price>10000</price>
   </book>
  
   <book>
       <name>Java开发1</name>
       <author address="保定 何软 宿舍3号">m_j1</author>
       <price>10000</price>
   </book>
  
  
</books>
 

 

解析元素

解析元素第一种方法

    public static void parse(Element root) {
       parseAttr(root);
       List<Element> list = root.elements();
       for (Element entity : list) {
           // 判断节点类型是否带有文本类型、
           if (entity.isTextOnly()) {
              parseAttr(entity);
              System.out.println(entity.getName());
           } else {
              parse(entity);
           }
       }
    }
 

 

解析元素第二种方法

    public static void parse(Element root) {
       for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
           Element entity = it.next();
           if (entity.isTextOnly()) {
              parseAttr(entity);
              System.out.println(entity.getText());
           } else {
              parse(entity);
           }
       }
    }
 

 

解析属性

解析属性第一种方法

    private static void parseAttr(Element root) {

       List<Attribute> attrs = root.attributes();
       System.out.println(attrs.size());
       for (Attribute entity : attrs) {
           System.out.println(entity.getName() + "-----" + entity.getValue());
       }
    }
 

 

解析属性第二种方法

// 解析属性
    private static void parseAttr(Element root) {
for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext();) {
           Attribute entity = it.next();
           System.out.println(entity.getName() + "--------"
                  + entity.getValue());
       }
    }
 
 

利用dom4j创建xml文档

// 利用的demo4J创建XML文档
    public static void main(String[] args)throws Exception {
       // 创建document对象两种方法   第一种
       Document doc = DocumentHelper.createDocument();
 
       /*第二种
        * DocumentFactory df=new DocumentFactory(); Document
        * doc1=df.createDocument();
        */
       Element root = doc.addElement("books");
       // 添加四个元素
       Element book = root.addElement("book");
       book.addAttribute("isbn", "1001");
       Element name = book.addElement("name");
       name.setText("java编程");
       Element author = book.addElement("author");
       author.setText("aa");
       Element price = book.addElement("price");
       price.setText("000");
       //设置乱码
       OutputFormat format = new OutputFormat("      ",true,"gb2312");
 
        XMLWriter xm=new XMLWriter(new FileWriter("src\\bk.xml"),format);
        xm.write(doc);
        xm.close();
    }
 

 

利用dom4j修改xml文档

 

修改步骤: 1、解析xml文档,将xml文档转换Dom4j

2、利用dom4j树提供的导航方法找到需要修改的节点

3、修改指定的节点,或者在指定的节点添加新的节点

4、写入到dom4jdocument,写入修改后的xml文件中

 

public static void main(String[] args) throws Exception{
    SAXReader reader=new  SAXReader();
    //忽略空白
    reader.setStripWhitespaceText(true);
    Document doc=reader.read(new File("src\\bk.xml"));
    //获取根元素
    Element root=doc.getRootElement();
   
    //在第二个book中的auther后添加address元素
    Element book2=(Element) root.elements("book").get(1);
    List list=book2.elements();
    Element address=DocumentHelper.createElement("address");
    address.setText("保定");
    list.add(2,address);
    /*在第一个book中的name之前添加address元素
     * Element address=DocumentHelper.createElement("address");
    List list= root.element("book").elements();
    address.setText("保定");
    list.add(0, address);*/ 
//  Element address  =    root.element("book").element("author").addElement("address");
    OutputFormat format = new OutputFormat("      ",true,"gb2312");
       XMLWriter xw = new XMLWriter(new FileWriter("src\\bk.xml"),format);
       xw.write(doc);
       xw.close();
}
 
 

利用dom4j删除固定位置的元素

public static void main(String[] args) throws Exception {
       SAXReader reader = new SAXReader();
       // 忽略空白
       reader.setStripWhitespaceText(true);
       Document doc = reader.read(new File("src\\bk.xml"));
       // 获取根元素
       Element root = doc.getRootElement();
       Element book2 = (Element) root.elements("book").get(0);
       Element address = book2.element("address");
       book2.remove(address);
 
       OutputFormat format = new OutputFormat("     ", true, "gb2312");
 
       XMLWriter xw = new XMLWriter(new FileWriter("src\\bk.xml"), format);
 
       xw.write(doc);
       xw.close();
    }
 

你可能感兴趣的:(编程,xml)