import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
public class Xml {
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("E:\work\test\xml1.xml"));
NodeList nl = doc.getElementsByTagName("book");
for (int i = 0; i < nl.getLength(); i++) {
System.out.println(nl.item(i).getAttributes().item(0));
System.out.println(doc.getElementsByTagName("title").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("title").item(i)
.getAttributes().item(0));
System.out.println(doc.getElementsByTagName("author").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("year").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("price").item(i)
.getFirstChild().getNodeValue());
System.out.println();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
category="COOKING" Everyday Italian lang="en" Giada De Laurentiis 2005 30.00 category="CHILDREN" Harry Potter lang="en" J K. Rowling 2005 29.99 category="WEB" Learning XML lang="en" Erik T. Ray 2003 39.95
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有