(1)掌握DOM管理XML文档的基本原理
(2)能够使用DOM遍历并分析XML文件
package Dom; import javax.xml.parsers.*; import org.w3c.dom.*; public class Dom { public static void main(String args[]) { try{ DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse("order.xml"); NodeList nl=doc.getElementsByTagName("shipTo"); for(int i=0;i<nl.getLength();i++){ Element node=(Element)nl.item(i); System.out.println("name:"); System.out.println(node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()); System.out.println("street:"); System.out.println(node.getElementsByTagName("street").item(0).getFirstChild().getNodeValue()); System.out.println("city:"); System.out.println(node.getElementsByTagName("city").item(0).getFirstChild().getNodeValue()); System.out.println("zip:"); System.out.println(node.getElementsByTagName("zip").item(0).getFirstChild().getNodeValue()); System.out.println(); } NodeList n2=doc.getElementsByTagName("billTo"); for(int i=0;i<n2.getLength();i++){ Element node=(Element)n2.item(i); System.out.println("name:"); System.out.println(node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()); System.out.println("street:"); System.out.println(node.getElementsByTagName("street").item(0).getFirstChild().getNodeValue()); System.out.println("city:"); System.out.println(node.getElementsByTagName("city").item(0).getFirstChild().getNodeValue()); System.out.println("zip:"); System.out.println(node.getElementsByTagName("zip").item(0).getFirstChild().getNodeValue()); System.out.println(); } NodeList n3=doc.getElementsByTagName("item"); for(int i=0;i<n3.getLength();i++){ Element node=(Element)n3.item(i); System.out.println("productName:"); System.out.println(node.getElementsByTagName("productName").item(0).getFirstChild().getNodeValue()); System.out.println("quantity:"); System.out.println(node.getElementsByTagName("quantity").item(0).getFirstChild().getNodeValue()); System.out.println("USPrice:"); System.out.println(node.getElementsByTagName("USPrice").item(0).getFirstChild().getNodeValue()); System.out.println("comment:"); System.out.println(node.getElementsByTagName("comment").item(0).getFirstChild().getNodeValue()); System.out.println(); } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
XML代码如下
<?xml version="1.0"?> <purchaseOrder> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Street</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry,my lawn is going wild!</comment> <items> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is electric</comment> </item> </items> </purchaseOrder>
结果如下
Dom解析式一个在各种语言中都用得比较多的一种语言 所以得好好学习
Anr-C 2016.10.28
本站由以下主机服务商提供服务支持:
0条评论