|
@@ -0,0 +1,62 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.dom4j;
|
|
2
|
+
|
|
3
|
+import java.io.InputStream;
|
|
4
|
+import java.net.MalformedURLException;
|
|
5
|
+
|
|
6
|
+import org.dom4j.Document;
|
|
7
|
+import org.dom4j.DocumentException;
|
|
8
|
+import org.dom4j.DocumentHelper;
|
|
9
|
+import org.dom4j.Element;
|
|
10
|
+import org.dom4j.io.SAXReader;
|
|
11
|
+
|
|
12
|
+/**
|
|
13
|
+ * <pre>
|
|
14
|
+ * kr.co.swh.lecture.opensource.dom4j
|
|
15
|
+ * Dom4jMain.java
|
|
16
|
+ *
|
|
17
|
+ * 설명 : Dom4j
|
|
18
|
+ * </pre>
|
|
19
|
+ *
|
|
20
|
+ * @since : 2019. 4. 2.
|
|
21
|
+ * @author : tobby48
|
|
22
|
+ * @version : v1.0
|
|
23
|
+ */
|
|
24
|
+public class Dom4jMain {
|
|
25
|
+
|
|
26
|
+ public Document createDocument() {
|
|
27
|
+ Document doc = DocumentHelper.createDocument();
|
|
28
|
+ Element root = doc.addElement("students");
|
|
29
|
+ root.addElement("student")
|
|
30
|
+ .addAttribute("name", "yuki")
|
|
31
|
+ .addAttribute("age", "18")
|
|
32
|
+ .addText("cat");
|
|
33
|
+
|
|
34
|
+ root.addElement("student")
|
|
35
|
+ .addAttribute("name", "swh")
|
|
36
|
+ .addAttribute("age", "5")
|
|
37
|
+ .addText("academy");
|
|
38
|
+
|
|
39
|
+ return doc;
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public Document parse(InputStream is) throws DocumentException {
|
|
43
|
+ SAXReader reader = new SAXReader();
|
|
44
|
+ Document doc = reader.read(is);
|
|
45
|
+ return doc;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public static void main(String[] args) throws MalformedURLException, DocumentException {
|
|
49
|
+ Dom4jMain xml = new Dom4jMain();
|
|
50
|
+ @SuppressWarnings("static-access")
|
|
51
|
+ InputStream is = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("xml.xml");
|
|
52
|
+ Document doc = xml.parse(is);
|
|
53
|
+ String text = doc.asXML(); // Xml -> String
|
|
54
|
+ System.out.println(text);
|
|
55
|
+ doc = DocumentHelper.parseText(text); // String -> Xml
|
|
56
|
+ text = doc.asXML();
|
|
57
|
+ System.out.println(text);
|
|
58
|
+ doc = xml.createDocument();
|
|
59
|
+ text = doc.asXML();
|
|
60
|
+ System.out.println(text);
|
|
61
|
+ }
|
|
62
|
+}
|