日期:2014-05-20  浏览次数:20658 次

懂XML,请进......
下面XML该如何解析:
<?xml version='1.0' encoding='UTF-8'?>
<statType>
<time value=”2011-10-02 21:07:20” />
<name>
<edition value=”222” />
<defaultAD>
  <property name=”id” value=”1” />
<property name=”age” value=”2” />
<property name=”scott” value=”3” />
<property name=”sex” value=”4” />
</defaultAD>
<defaultAD>
<property name=”id” value=”1” />
<property name=”age” value=”2” />
<property name=”scott” value=”3” />
<property name=”sex” value=”4” />
</defaultAD>
<defaultAD>
<property name=”id” value=”1” />
<property name=”age” value=”2” />
<property name=”scott” value=”3” />
<property name=”sex” value=”4” />
</defaultAD>
</name>
<names>
<edition value=”333” />
<are>
<property name=”id” value=”1”/>
<property name=”age” value=”2”/>
<property name=”scott” value=”3”/>
<property name=”sex” value=”4”/>
</are>
<are>
<property name=”id” value=”1”/>
<property name=”age” value=”2”/>
<property name=”scott” value=”3”/>
<property name=”sex” value=”4”/>
</are>
</names>
</statType> 


------解决方案--------------------
使用xpath:
Java code
        FileReader reader = new FileReader("/tmp/stattype.xml");
        InputSource input = new InputSource(reader);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        NodeList list = (NodeList)xpath.evaluate("//property",input,XPathConstants.NODESET);
        for(int i = 0; i < list.getLength(); i++){
            Node node = list.item(i);
            System.out.printf("name:%s value:%s%n",
                              (String)xpath.evaluate("@name",node,XPathConstants.STRING),
                              (String)xpath.evaluate("@value",node,XPathConstants.STRING));
        }

------解决方案--------------------
探讨

5L 如果不使用xpath,而是用dom4J该如何解析呢? 也请教大家