forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMDemo.java
More file actions
73 lines (67 loc) · 2.13 KB
/
Copy pathDOMDemo.java
File metadata and controls
73 lines (67 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.IOException;
import org.xml.sax.SAXException;
public class DOMDemo{
public static void main(String[] args){
if(args.length!=1){
System.err.println("usage: java DOMDemo xmlfile");
return;
}
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// configure the document builder factory
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(args[0]);
System.out.printf("version=%s, encoding=%s, standalone=%b%n",
doc.getXmlVersion(), doc.getXmlEncoding(), doc.getXmlStandalone());
if(doc.hasChildNodes()){
NodeList nl = doc.getChildNodes();
for(int i=0;i<nl.getLength();i++){
Node node = nl.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
dumpElement((Element)node);
}
}
}
}catch(FactoryConfigurationError fce){
fce.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(SAXException se){
se.printStackTrace();
}
}
static void dumpElement(Element element){
System.out.printf("Element: nodename=%s, localname=%s, prefix=%s, namespaceUri=%s%n",
element.getNodeName(), element.getLocalName(), element.getPrefix(), element.getNamespaceURI());
NamedNodeMap nnm = element.getAttributes(); // dump attributes
if(nnm!=null){
for(int i=0;i<nnm.getLength();i++){
Node node = nnm.item(i);
Attr attr = (Attr)node;
System.out.printf(" Attribute: %s = %s%n", attr.getName(), attr.getValue());
}
}
if(element.hasChildNodes()){
NodeList nl = element.getChildNodes();
for(int i=0;i<nl.getLength();i++){
Node node = nl.item(i);
if(node instanceof Element){
dumpElement((Element)node);
}
}
}
}
}