-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParseXMLTest.java
164 lines (122 loc) · 4.39 KB
/
ParseXMLTest.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.corejava.chapter2;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import static java.lang.System.*;
public class ParseXMLTest
{
private static String workDir = System.getProperty("user.dir") + File.separator + "src" + File.separator +
"com" + File.separator + "corejava" + File.separator + "chapter2" + File.separator;
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File(workDir + "XSDTest.xml");
Document doc = builder.parse(f);
// 通过匿名内部类来设置文档构建器 的错误处理器
builder.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
out.println("warning");
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
out.println("fatalError");
}
@Override
public void error(SAXParseException exception) throws SAXException
{
out.println("error");
}
});
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
Element e1 = (Element) children.item(0);
Element e2 = (Element) children.item(1);
out.println(e1.getNodeName() + ", " + e1.getTextContent());
out.println(e2.getNodeName() + ", " + e2.getTextContent());
out.print(e1.getAttribute("tangrong"));// 居然没有抛异常
}
public static void main2(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File(workDir + "test1.xml");
Document doc = builder.parse(f);
// 通过匿名内部类来设置文档构建器 的错误处理器
builder.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
out.println("warning");
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
out.println("fatalError");
}
@Override
public void error(SAXParseException exception) throws SAXException
{
out.println("error");
}
});
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
Element e1 = (Element) children.item(0);
Element e2 = (Element) children.item(1);
out.println(e1.getNodeName() + ", " + e1.getTextContent());
out.println(e2.getNodeName() + ", " + e2.getTextContent());
out.print(e1.getAttribute("tangrong"));// 居然没有抛异常
}
public static void main1(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File(workDir + "test1.xml");
Document doc = builder.parse(f);
Element root = doc.getDocumentElement();
//out.print(root.getNodeName()); // font
out.println(root.getTagName() + " = " + root.getAttribute("name"));
out.println(root.getTagName() + " = " + root.getAttribute("size"));
// font = ab
// font = 36 pt
// false access to font start
NodeList children = root.getChildNodes();
Element e1 = (Element) children.item(0);
Element e2 = (Element) children.item(1);
out.println(e1.getNodeName() + ", " + e1.getTextContent());
out.println(e2.getNodeName() + ", " + e2.getTextContent());
// false over
/* true access to font start
for (int i = 0; i < children.getLength(); i++)
{
Node child = children.item(i);
if(child instanceof Element)
{
Element childElement = (Element)child;
out.println(childElement.getTagName() + " = " +
childElement.getTextContent());
// name = abcdefg
// size = 36
}
}
true over*/
}
}