forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson44.java
More file actions
219 lines (116 loc) · 5.33 KB
/
JavaLesson44.java
File metadata and controls
219 lines (116 loc) · 5.33 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Simple API for XML only allows you to read from an XML
// file. SAX doesn't require you to read the document into memory
import org.xml.sax.*;
// Used to read an XML file into memory where the doc is
// stored as a bunch of objects. You can write back to the file
import org.w3c.dom.*;
// Used to gather XML elements into DOM objects
import javax.xml.parsers.*;
public class JavaLesson44{
public static void main(String[] args){
// Creates a DOM object in memory. Now you can access
// data in the xml file
Document xmlDoc = getDocument("./src/tvshow5.xml");
// Get the name of the root element
xmlDoc.getDocumentElement();
System.out.println("Root element is " +
xmlDoc.getDocumentElement().getNodeName());
// The nodelist contains all of the nodes in the xml file
NodeList listOfShows = xmlDoc.getElementsByTagName("show");
// Get the number of children in the root element
System.out.println("Number of shows " + listOfShows.getLength());
// The element you want to print out
String elementName = "network";
// The attribute to search for
String attrName = "country";
// Send the NodeList for processing
getElementAndAttrib(listOfShows, elementName, attrName);
// Additional Code not in the Video -------------------
// Returns an Element object represented by the root element
// of the xml file
Element theRoot = xmlDoc.getDocumentElement();
// Get the first child or show element in this situation
Element showElement = (Element)theRoot.getFirstChild();
// Object that will contain all of the show data
tvShow tvs;
// Cycle through all of the show elements until the end
while(showElement != null){
// Go get the first element in show which is name
tvs = getShowData(showElement);
// Print out the value of showName
System.out.println(tvs.showName);
// Get the next show
showElement = (Element)showElement.getNextSibling();
}
}
// Reads an XML file into a DOM document
private static Document getDocument(String docString) {
try {
// API used to convert XML into a DOM object tree
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Ignore all of the comments
factory.setIgnoringComments(true);
// Ignore white space in elements
factory.setIgnoringElementContentWhitespace(true);
// Validate the XML as it is parsed
factory.setValidating(true);
// Provides access to the documents data
DocumentBuilder builder = factory.newDocumentBuilder();
// Takes the document
return builder.parse(new InputSource(docString));
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
return null;
}
private static tvShow getShowData(Element ele){
// Returns the first child element in the xml file.
// That element is name
Element nameElement = (Element)ele.getFirstChild();
// Save the value in this element as a String
String showName = getTextValue(nameElement).trim();
// Create a tvShow object that holds the values stored
// in the show elements
return new tvShow(showName);
}
private static String getTextValue(Node theNode) {
// Get the value of the first child element
return theNode.getFirstChild().getNodeValue();
}
private static void getElementAndAttrib(NodeList listOfShows, String elementName, String attrName){
try{
// Cycle through the number of shows
for(int i=0; i < listOfShows.getLength(); i++){
// Get the first show node
Node showNode = listOfShows.item(i);
// Convert into an element to gain access to element methods
Element showElement = (Element)showNode;
// Create a list of nodes that have the name defined in elementName
NodeList networkList = showElement.getElementsByTagName(elementName);
// Get the first and only element in this situation
Element networkElement = (Element)networkList.item(0);
// Returns a list of node elements
// Each value is in a node in side of the network node
// That's why you have to get the child nodes for network
NodeList elementList = networkElement.getChildNodes();
// Check if the element has the attribute set
if(networkElement.hasAttribute(attrName)){
System.out.println(elementName + " : " + ((Node)elementList.item(0)).getNodeValue().trim() +
" has attribute " + networkElement.getAttribute(attrName));
} else {
System.out.println(((Node)elementList.item(0)).getNodeValue().trim());
}
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
private static class tvShow{
public String showName, showNetwork;
public tvShow(String showName){
this.showName = showName;
}
}
}