如何在dom4J文档中使用DOMElement类

时间:2016-08-26 04:55:08

标签: java xml dom dom4j

我尝试通过dom4j修改XML 但是dom4j没有足够的方法(例如,getNextSibling,hasAttribute)
据我搜索,DOMElement Class将有助于解决这个问题 但我根本无法理解如何使用DOMElement类......

如何使用DOMElement重写下面的代码?
http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/dom/DOMElement.html

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Sampe1 {

public static void main(String[] args) {
    SAXReader reader = new SAXReader();

    try {
        Document document = reader.read("catalog.xml");

        List books =document.selectNodes("catalog/book");
        for(Iterator i = books.iterator(); i.hasNext();){
            Node book = (Node)i.next();
            List aList = book.selectNodes("./a");
            for(Iterator i2 = aList.iterator(); i2.hasNext();){
                Node aNode = (Node)i2.next();
                int aInt = Integer.parseInt( aNode.valueOf("./@volume"));


                **// I want to rewrite the next line By "getNextSibling"method of DOMElement(dom4j.dom)**
                Node NextSiblingNode= aNode.selectSingleNode("./following-sibling::*");
                int nextInt = Integer.parseInt(NextSiblingNode.valueOf("./@volume"));



**//I want to rewrite the following lines By "hasAttribute"method of DOMElement**
//                  if(aNode.hasAttribute("volume");){
//                      if(NextSiblingNode.hasAttribute("volume")){
//                          if(aInt > nextInt){
//                              System.out.println(aInt+"is larger than"+ nextInt);
//                          }
//                      }
//                  }




            }



    }


 }
             catch(DocumentException e)
            {
             System.out.println(e.getMessage());
                       }

}
}

catalog.xml

enter code here

<?xml version="1.0"?>
 <catalog>
  <book>
   <a num = "1" volume = "10">Gambardella, Matthew</a>
   <a num = "2" volume = "7">XML Developer's Guide</a>
   <a num = "3" volume = "3">Computer</a>
   <a num = "4">44.95</a>
   <a num = "5">2000-10-01</a>
   <a num = "6">An in-depth look at creating applications
  with XML.</a>
  </book>
  <book>
   <a num = "3" volume = "3">Fantasy</a>
   <a num ="4" >5.95</a>
   <a num = "5">2000-12-16</a>
   <a num = "6">A former architect battles corporate zombies,
  an evil sorceress, and her own childhood to become queen
  of the world.</a>
  </book>
  <book>
   <a num = "4">5.95</a>
   <a num = "5">2000-11-17</a>
   <a num = "6">After the collapse of a nanotechnology
  society in England, the young survivors lay the
  foundation for a new society.</a>
  </book>
 </catalog>

2 个答案:

答案 0 :(得分:0)

您可以将具有XML属性的Node个对象转换为Element,然后使用这些属性。这是该计划的工作版本:

package test;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class TestMain {

    public static void main(String[] args) {
        SAXReader reader = new SAXReader();

        try {
            Document document = reader.read("catalog.xml");

            List books = document.selectNodes("catalog/book");
            for (Iterator i = books.iterator(); i.hasNext();) {
                Node book = (Node) i.next();
                List<Node> aList = book.selectNodes("./a");
                for (Iterator<Node> i2 = aList.iterator(); i2.hasNext();) {
                    Element aNode = (Element) i2.next();
                    Attribute aNodeVolume = aNode.attribute("volume");
                    if (aNodeVolume != null) {
                        int aInt = Integer.parseInt(aNodeVolume.getValue());

                        // I want to rewrite the next line By
                        // "getNextSibling"method
                        // of DOMElement(dom4j.dom)**
                        Element NextSiblingNode = (Element) aNode.selectSingleNode("./following-sibling::*");

                        // I want to rewrite the following lines By
                        // "hasAttribute"method of DOMElement**
                        if (aNode.attributeValue("volume") != null) {
                            Attribute NextSiblingNodeVolume = NextSiblingNode.attribute("volume");
                            if (NextSiblingNodeVolume != null) {
                                int nextInt = Integer.parseInt(NextSiblingNodeVolume.getValue());
                                if (aInt > nextInt) {
                                    System.out.println(aInt + "is larger than" + nextInt);
                                }
                            }
                        }

                    }

                }
            }

        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }

    }

}

答案 1 :(得分:0)

要使用DOMElement,您需要指定org.dom4j.dom.DOMDocumentFactory来创建SAXReader。将创建DOMDocument,您可以将这些对象置于W3C或org.dom4j.dom.*类。

import java.util.*;
import org.dom4j.dom.*;
import org.dom4j.io.SAXReader;
import org.w3c.dom.*;

public class Sampe1 {

public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader(DOMDocumentFactory.getInstance());
DOMDocument document = (DOMDocument)reader.read("catalog.xml");
List books =document.selectNodes("catalog/book");
for(Iterator i = books.iterator(); i.hasNext();){
   DOMElement book = (DOMElement)i.next();
   List aList = book.elements("a");
   for(Iterator i2 = aList.iterator(); i2.hasNext();){
      DOMElement aNode = (DOMElement)i2.next();
      Node NextSiblingNode = (Node)aNode.getNextSibling();
      System.out.println(NextSiblingNode);//next one is org.dom4j.dom.DOMText...
   }
}
}