日期:2014-05-20 浏览次数:20655 次
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()); } } }