File tree 4 files changed +80
-14
lines changed
java/feature1/parse-xml/parse-xml/src
4 files changed +80
-14
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- package java - private - feature ;
1
+ package example ;
2
2
3
3
import junit .framework .Test ;
4
4
import junit .framework .TestCase ;
You can’t perform that action at this time.
0 commit comments