Skip to content

Commit dce29a6

Browse files
tanakaryotanakaryo
tanakaryo
authored and
tanakaryo
committedJan 19, 2024
add java source.
1 parent 93d0473 commit dce29a6

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package example;
2+
3+
import java.io.StringReader;
4+
5+
import javax.xml.parsers.DocumentBuilder;
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
import org.w3c.dom.Document;
8+
import org.w3c.dom.Element;
9+
import org.w3c.dom.NodeList;
10+
import org.xml.sax.InputSource;
11+
12+
/**
13+
* Hello world!
14+
*
15+
*/
16+
public class App
17+
{
18+
public static void main( String[] args ) throws Exception {
19+
20+
StringBuilder sb =new StringBuilder();
21+
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
22+
sb.append("<ProgramingList>");
23+
sb.append("<Language id=\"001\" name=\"Java\">Javaは標準的なプログラミング言語です</Language>");
24+
sb.append("<Language id=\"002\" name=\"Python\">Pythonは標準的なプログラミング言語です</Language>");
25+
sb.append("</ProgramingList>");
26+
27+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
28+
DocumentBuilder builder = factory.newDocumentBuilder();
29+
InputSource is = new InputSource(new StringReader(sb.toString()));
30+
Document doc = builder.parse(is);
31+
Element elm = doc.getDocumentElement();
32+
NodeList list = elm.getElementsByTagName("Language");
33+
34+
for (int i=0; i<list.getLength(); i++) {
35+
Element e = (Element)list.item(i);
36+
System.out.println(e.getAttribute("id"));
37+
System.out.println(e.getAttribute("name"));
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package example;
2+
3+
import java.io.FileInputStream;
4+
import java.io.StringReader;
5+
6+
import javax.xml.parsers.DocumentBuilder;
7+
import javax.xml.parsers.DocumentBuilderFactory;
8+
import javax.xml.parsers.SAXParser;
9+
import javax.xml.parsers.SAXParserFactory;
10+
11+
import org.w3c.dom.Document;
12+
import org.w3c.dom.Element;
13+
import org.w3c.dom.NodeList;
14+
import org.xml.sax.InputSource;
15+
16+
/**
17+
* Hello world!
18+
*
19+
*/
20+
public class AppFail {
21+
public static void main( String[] args ) throws Exception {
22+
23+
StringBuilder sb =new StringBuilder();
24+
sb.append("{\"Language\": \"Java\"}");
25+
26+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27+
DocumentBuilder builder = factory.newDocumentBuilder();
28+
InputSource is = new InputSource(new StringReader(sb.toString()));
29+
Document doc = builder.parse(is);
30+
Element elm = doc.getDocumentElement();
31+
NodeList list = elm.getElementsByTagName("Language");
32+
33+
for (int i=0; i<list.getLength(); i++) {
34+
Element e = (Element)list.item(i);
35+
System.out.println(e.getAttribute("id"));
36+
System.out.println(e.getAttribute("name"));
37+
}
38+
}
39+
}

‎java/feature1/parse-xml/parse-xml/src/main/java/java-private-feature/App.java

-13
This file was deleted.

‎java/feature1/parse-xml/parse-xml/src/test/java/java-private-feature/AppTest.java renamed to ‎java/feature1/parse-xml/parse-xml/src/test/java/example/AppTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package java-private-feature;
1+
package example;
22

33
import junit.framework.Test;
44
import junit.framework.TestCase;

0 commit comments

Comments
 (0)
Please sign in to comment.