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

关于XML的解析问题
[code=XML][/code]<Cust AttrType='Cust'><Serv AttrType='Integer'>-1</ServID><Billing AttrType='Integer'>11109</Billing><Item AttrType='Item'><duleID AttrType='Integer'>100402</duleID></Item>
<Item AttrType='Item'><duleID AttrType='Integer'>100403</duleID></Item>><Item AttrType='Item'><duleID AttrType='Integer'>100404</duleID></Item>><Item AttrType='Item'><duleID AttrType='Integer'>100405</duleID></Item>><Item AttrType='Item'><duleID AttrType='Integer'>100406</duleID></Item></Cust>



这样的XML怎么获得Item 这个节点有几个,和这个节点下得数据。



谢谢大家!

------解决方案--------------------
String ReturnResult = xmls.substring(xmls.indexOf("<Item >")+14,xmls.indexOf("</Item >"));
------解决方案--------------------
for example
Java code
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XMLTest {    
    public static void main(String[] args) throws Throwable {
        String s = "<Cust AttrType='Cust'>" +
                   "<ServID AttrType='Integer'>-1</ServID>" +
                   "<Billing AttrType='Integer'>11109</Billing>" + 
                   "<Item AttrType='Item'><duleID AttrType='Integer'>100402</duleID></Item>" +
                   "<Item AttrType='Item'><duleID AttrType='Integer'>100403</duleID></Item>" +
                   "<Item AttrType='Item'><duleID AttrType='Integer'>100404</duleID></Item>" +
                   "<Item AttrType='Item'><duleID AttrType='Integer'>100405</duleID></Item>" +
                   "<Item AttrType='Item'><duleID AttrType='Integer'>100406</duleID></Item>" +
                   "</Cust>";

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(s.getBytes()));
        NodeList nl = doc.getElementsByTagName("Item");
        System.out.printf("There is [%d] items in xml document.\n", nl.getLength());
        System.out.println("-----------------------------------------------------");
        for (int i=0; i<nl.getLength(); i++) {
            Node n = nl.item(i);
            System.out.println(n.getTextContent());
        }
    }
}