-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDOM.java
176 lines (152 loc) · 5.5 KB
/
MyDOM.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
165
166
167
168
169
170
171
172
173
174
175
176
/* Parser skeleton for processing item-???.xml files. Must be compiled in
* JDK 1.5 or above.
*
* Instructions:
*
* This program processes all files passed on the command line (to parse
* an entire diectory, type "java MyParser myFiles/*.xml" at the shell).
*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
// import org.w3c.dom.Element;
// import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class MyDOM {
static final String columnSeparator = "|*|";
static DocumentBuilder builder;
static final String[] typeName = {
"none",
"Element",
"Attr",
"Text",
"CDATA",
"EntityRef",
"Entity",
"ProcInstr",
"Comment",
"Document",
"DocType",
"DocFragment",
"Notation",
};
static class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException exception)
throws SAXException {
fatalError(exception);
}
public void error(SAXParseException exception)
throws SAXException {
fatalError(exception);
}
public void fatalError(SAXParseException exception)
throws SAXException {
exception.printStackTrace();
System.out.println("There should be no errors " +
"in the supplied XML files.");
System.exit(3);
}
}
/*
* Returns the amount (in XXXXX.xx format) denoted by a money-string
* like $3,453.23. Returns the input if the input is an empty string.
*/
static String strip(String money) {
if (money.equals(""))
return money;
else {
double am = 0.0;
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
try {
am = nf.parse(money).doubleValue();
} catch (ParseException e) {
System.out.println("This method should work for all " +
"money values you find in our data.");
System.exit(20);
}
nf.setGroupingUsed(false);
return nf.format(am).substring(1);
}
}
/*
* Process one items-???.xml file.
*/
static void processFile(File xmlFile) {
Document doc = null;
try {
doc = builder.parse(xmlFile);
} catch (IOException e) {
e.printStackTrace();
System.exit(3);
} catch (SAXException e) {
System.out.println("Parsing error on file " + xmlFile);
System.out.println(" (not supposed to happen with supplied XML files)");
e.printStackTrace();
System.exit(3);
}
/*
* At this point 'doc' contains a DOM representation of an 'Items' XML
* file. Use doc.getDocumentElement() to get the root Element.
*/
System.out.println("Successfully parsed - " + xmlFile);
/*
* Fill in code here (you will probably need to write auxiliary
* methods).
*/
/**************************************************************/
recursiveDescent(doc, 0);
}
public static void recursiveDescent(Node n, int level) {
// adjust indentation according to level
for (int i = 0; i < 4 * level; i++)
System.out.print(" ");
// dump out node name, type, and value
String ntype = typeName[n.getNodeType()];
String nname = n.getNodeName();
String nvalue = n.getNodeValue();
System.out.println("Type = " + ntype + ", Name = " + nname + ", Value = " + nvalue);
// dump out attributes if any
org.w3c.dom.NamedNodeMap nattrib = n.getAttributes();
if (nattrib != null && nattrib.getLength() > 0)
for (int i = 0; i < nattrib.getLength(); i++)
recursiveDescent(nattrib.item(i), level + 1);
// now walk through its children list
org.w3c.dom.NodeList nlist = n.getChildNodes();
for (int i = 0; i < nlist.getLength(); i++)
recursiveDescent(nlist.item(i), level + 1);
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java MyParser [file] [file] ...");
System.exit(1);
}
/* Initialize parser. */
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
builder = factory.newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler());
} catch (FactoryConfigurationError e) {
System.out.println("unable to get a document builder factory");
System.exit(2);
} catch (ParserConfigurationException e) {
System.out.println("parser was unable to be configured");
System.exit(2);
}
/* Process all files listed on command line. */
for (int i = 0; i < args.length; i++) {
File currentFile = new File(args[i]);
processFile(currentFile);
}
}
}