Test.java
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import java.io.*;
public class Test {
public static void main(String args[]) {
File docFile = new File("src\\orders.xml");
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 将xml文件解释成文档对象
doc = db.parse(docFile);
// 取得文档根
Element root = doc.getDocumentElement();
// 取得要元素名
System.out.println("The root element is " + root.getNodeName());
// 获取孩子节点
NodeList children = root.getChildNodes();
// 获取孩子节数目
System.out.println("There are " + children.getLength() + " nodes in this document.");
// 获取孩子节点,包括 order 元素和三个文本节点
// 显示第一层孩子包括 order 元素和三个文本节点
// for (Node child = root.getFirstChild();child != null;child = child.getNextSibling())
// {
// System.out.println(child.getNodeName()+" = " +child.getNodeValue());
// }
stepThrough(root);
} catch (Exception e) {
System.out.print("roblem parsing the file: " + e.getMessage());
}
}
/**
* 遍历执行
*/
private static void stepThrough(Node start) {
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
if (start.getNodeType() == start.ELEMENT_NODE) {
NamedNodeMap startAttr = start.getAttributes();
for (int i = 0; i < startAttr.getLength(); i++) {
Node attr = startAttr.item(i);
System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
}
}
for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
stepThrough(child);
}
}
}
orders.xml
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<customerid limit="1000">12341</customerid>
<status>pending</status>
<item instock="Y" itemid="SA15">
<name>Silver Show Saddle, 16 inch</name>
<price>825.00</price>
<qty>1</qty>
</item>
<item instock="N" itemid="C49">
<name>remium Cinch</name>
<price>49.00</price>
<qty>1</qty>
</item>
</order>
<order>
<customerid limit="150">251222</customerid>
<status>pending</status>
<item instock="Y" itemid="WB78">
<name>Winter Blanket (78 inch)</name>
<price>20</price>
<qty>10</qty>
</item>
</order>
</orders>
运行结果
The root element is orders
There are 5 nodes in this document.
orders = null
#text =
order = null
#text =
customerid = null
Attribute: limit = 1000
#text = 12341
#text =
status = null
#text = pending
#text =
item = null
Attribute: instock = Y
Attribute: itemid = SA15
#text =
name = null
#text = Silver Show Saddle, 16 inch
#text =
price = null
#text = 825.00
#text =
qty = null
#text = 1
#text =
#text =
item = null
Attribute: instock = N
Attribute: itemid = C49
#text =
name = null
#text = Premium Cinch
#text =
price = null
#text = 49.00
#text =
qty = null
#text = 1
#text =
#text =
#text =
order = null
#text =
customerid = null
Attribute: limit = 150
#text = 251222
#text =
status = null
#text = pending
#text =
item = null
Attribute: instock = Y
Attribute: itemid = WB78
#text =
name = null
#text = Winter Blanket (78 inch)
#text =
price = null
#text = 20
#text =
qty = null
#text = 10
#text =
#text =
#text =