-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLPrinter.java
63 lines (50 loc) · 1.59 KB
/
XMLPrinter.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
package xml;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class XMLPrinter {
static Document doc = null;
public static void main(String[] args) {
}
public static void print(String xml_string, int num) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new ByteArrayInputStream(xml_string.getBytes("UTF-8")));
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("*");
String name = "";
for(int temp = 0; temp < nList.getLength(); temp++) {
Node node = nList.item(temp);
Element e = (Element) node;
name = e.getNodeName();
if(name.equals("feed")) {
System.out.println("");
System.out.println(name + " " + num + " from ContentServer " + e.getAttribute("id"));
System.out.println("-------");
} else if (name.equals("entry")) {
System.out.println("");
System.out.println(name);
} else {
System.out.println(name + ": " + e.getTextContent());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Document parse_string(String xml_string) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new ByteArrayInputStream(xml_string.getBytes("UTF-8")));
}
catch (Exception e) {
e.printStackTrace();
}
finally {
return doc;
}
}
}