Skip to content

Commit 9d98f25

Browse files
committed
jdom
1 parent 89ceed8 commit 9d98f25

7 files changed

Lines changed: 187 additions & 1 deletion

File tree

java-xml/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<maven.compiler.target>11</maven.compiler.target>
1919
<java.version>11</java.version>
2020
<junit.version>5.4.0</junit.version>
21+
<jdom2.version>2.0.6</jdom2.version>
2122
</properties>
2223

2324
<dependencies>
@@ -29,6 +30,12 @@
2930
<scope>test</scope>
3031
</dependency>
3132

33+
<dependency>
34+
<groupId>org.jdom</groupId>
35+
<artifactId>jdom2</artifactId>
36+
<version>${jdom2.version}</version>
37+
</dependency>
38+
3239
</dependencies>
3340

3441
<build>

java-xml/src/main/java/com/mkyong/xml/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public class App {
55
public static void main(String[] args) {
66

77
System.out.println("Welcome to Java XML");
8-
8+
99
}
1010
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.mkyong.xml.jdom;
2+
3+
import org.jdom2.Document;
4+
import org.jdom2.Element;
5+
import org.jdom2.JDOMException;
6+
import org.jdom2.input.SAXBuilder;
7+
8+
import java.io.IOException;
9+
import java.net.URL;
10+
import java.util.List;
11+
12+
public class ReadXmlAlexaApiJDomParser {
13+
14+
private static final String REMOTE_URL = "https://data.alexa.com/data?cli=10&url=mkyong.com";
15+
16+
public static void main(String[] args) {
17+
18+
try {
19+
20+
SAXBuilder sax = new SAXBuilder();
21+
// XML is in a web-based location
22+
Document doc = sax.build(new URL(REMOTE_URL));
23+
24+
Element rootNode = doc.getRootElement();
25+
List<Element> list = rootNode.getChildren("SD");
26+
27+
for (Element target : list) {
28+
29+
Element popularity = target.getChild("POPULARITY");
30+
31+
String url = popularity.getAttributeValue("URL");
32+
String rank = popularity.getAttributeValue("TEXT");
33+
34+
System.out.printf("URL : %s%n", url);
35+
System.out.printf("Alexa Rank : %s%n", rank);
36+
37+
}
38+
39+
} catch (IOException | JDOMException e) {
40+
e.printStackTrace();
41+
}
42+
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mkyong.xml.jdom;
2+
3+
import org.jdom2.Document;
4+
import org.jdom2.Element;
5+
import org.jdom2.JDOMException;
6+
import org.jdom2.input.SAXBuilder;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.List;
11+
12+
// https://mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
13+
public class ReadXmlJDomParser {
14+
15+
private static final String FILENAME = "src/main/resources/staff.xml";
16+
17+
// https://github.com/hunterhacker/jdom/wiki/JDOM2-A-Primer
18+
public static void main(String[] args) {
19+
20+
try {
21+
22+
SAXBuilder sax = new SAXBuilder();
23+
// XML is a local file
24+
Document doc = sax.build(new File(FILENAME));
25+
26+
Element rootNode = doc.getRootElement();
27+
List<Element> list = rootNode.getChildren("staff");
28+
29+
for (Element target : list) {
30+
31+
String id = target.getAttributeValue("id");
32+
String name = target.getChildText("name");
33+
String role = target.getChildText("role");
34+
String salary = target.getChildText("salary");
35+
String currency = "";
36+
if (salary != null && salary.length() > 1) {
37+
// access attribute
38+
currency = target.getChild("salary").getAttributeValue("currency");
39+
}
40+
String bio = target.getChildText("bio");
41+
42+
System.out.printf("Staff id : %s%n", id);
43+
System.out.printf("Name : %s%n", name);
44+
System.out.printf("Role : %s%n", role);
45+
System.out.printf("Salary [Currency] : %,.2f [%s]%n", Float.parseFloat(salary), currency);
46+
System.out.printf("Bio : %s%n%n", bio);
47+
48+
}
49+
50+
} catch (IOException | JDOMException e) {
51+
e.printStackTrace();
52+
}
53+
54+
}
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mkyong.xml.jdom;
2+
3+
import org.jdom2.Document;
4+
import org.jdom2.Element;
5+
import org.jdom2.JDOMException;
6+
import org.jdom2.input.SAXBuilder;
7+
8+
import java.io.IOException;
9+
import java.io.StringReader;
10+
11+
public class ReadXmlStringJDomParser {
12+
13+
public static void main(String[] args) {
14+
15+
String XML = "<root><url>mkyong</url></root>";
16+
17+
try {
18+
19+
SAXBuilder sax = new SAXBuilder();
20+
// String that contains XML
21+
Document doc = sax.build(new StringReader(XML));
22+
23+
Element rootNode = doc.getRootElement();
24+
System.out.println(rootNode.getChildText("url"));
25+
26+
} catch (IOException | JDOMException e) {
27+
e.printStackTrace();
28+
}
29+
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mkyong.xml.sax;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
9+
public class CopyAndAddBomToXmlFile {
10+
11+
public static void main(String[] args) {
12+
Path src = Paths.get("src/main/resources/staff.xml");
13+
Path dest = Paths.get("src/main/resources/staff-bom.xml");
14+
writeBomFile(src, dest);
15+
}
16+
17+
private static void writeBomFile(Path src, Path dest) {
18+
19+
try (FileOutputStream fos = new FileOutputStream(dest.toFile())) {
20+
21+
byte[] BOM = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
22+
fos.write(BOM);
23+
24+
Files.copy(src, fos);
25+
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
30+
31+
}
32+
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Company>
3+
<staff id="1001">
4+
<name>mkyong</name>
5+
<role>support</role>
6+
<salary currency="USD">5000</salary>
7+
<!-- for special characters like < &, need CDATA -->
8+
<bio><![CDATA[HTML tag <code>testing</code>]]></bio>
9+
</staff>
10+
<staff id="1002">
11+
<name>yflow</name>
12+
<role>admin</role>
13+
<salary currency="EUR">8000</salary>
14+
<bio><![CDATA[a & b]]></bio>
15+
</staff>
16+
</Company>

0 commit comments

Comments
 (0)