2018년 3월 20일 화요일

[Java] Xml Parsing Examples


1. Parse Xml String with DocumentBuilder

 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
package javaxml;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class JavaXml {
    
    static String xml_data1 = 
        "<root>" +
        "<item value=\"level1\">Text1" +
        "<item value=\"level2\">Text2" +
        "<item value=\"level3\">Text3" +
        "</item></item></item></root>";
    
    static String xml_data2 = 
        "<root>" +
        "<item value=\"level1\">Text1</item>" +
        "<item value=\"level2\">Text2</item>" +
        "<item value=\"level3\">Text3</item>" +
        "</root>";
    
    
    void parseString(String s) {
        System.out.println("----------");
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder t_db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(s));
            ///t_is.setCharacterStream(new StringReader(s));
            Document doc = t_db.parse(is); //t_db.pase(filename);
            NodeList nodes = doc.getElementsByTagName("item");
            for (int i = 0, t_len = nodes.getLength(); i < t_len; i ++) {
                Element elem = (Element)nodes.item(i);
                System.out.println( "name: " + elem.getNodeName());
                System.out.println( "attr: value= " + elem.getAttribute("value"));
                System.out.println( "text: " + elem.getTextContent());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
        
    public static void main(String[] args) {
        JavaXml x = new JavaXml();
        x.parseString(xml_data1);
        x.parseString(xml_data2);
    }
}

Result:
----------
name: item
attr: value= level1
text: Text1Text2Text3
name: item
attr: value= level2
text: Text2Text3
name: item
attr: value= level3
text: Text3
----------
name: item
attr: value= level1
text: Text1
name: item
attr: value= level2
text: Text2
name: item
attr: value= level3
text: Text3

2. Parse Xml String with DocumentBuilder and XPath

 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
64
65
66
67
68
69
package javaxml;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class JavaXml {
    
    static String xml_data1 = 
        "<root>" +
        "<item value=\"level1\">Text1" +
        "<item value=\"level2\">Text2" +
        "<item value=\"level3\">Text3" +
        "</item></item></item></root>";
    
    static String xml_data2 = 
        "<root>" +
        "<item value=\"level1\">Text1</item>" +
        "<item value=\"level2\">Text2</item>" +
        "<item value=\"level3\">Text3</item>" +
        "</root>";

    void parseStringXpath(String s) {
        System.out.println("----------");
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder t_db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(s));
            ///t_is.setCharacterStream(new StringReader(s));
            XPath xpath = XPathFactory.newInstance().newXPath();
            Document doc = t_db.parse(is); //t_db.pase(filename);
            NodeList nodes = doc.getElementsByTagName("item");

            Element elem = (Element) xpath.evaluate( "/root/item", doc, XPathConstants.NODE);
            System.out.println( "name: " + elem.getNodeName());
            System.out.println( "attr: value= " + elem.getAttribute("value"));
            System.out.println( "text: " + elem.getTextContent());                

            Element elem2 = (Element)elem.getNextSibling();
            if( elem2 != null ) {
                System.out.println( "name: " + elem2.getNodeName());
                System.out.println( "attr: value= " + elem2.getAttribute("value"));
                System.out.println( "text: " + elem2.getTextContent());                
            }
            elem = (Element) xpath.evaluate( "/root/item/item", doc, XPathConstants.NODE);
            if( elem != null ) {
                System.out.println( "name: " + elem.getNodeName());
                System.out.println( "attr: value= " + elem.getAttribute("value"));
                System.out.println( "text: " + elem.getTextContent());                
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JavaXml x = new JavaXml();
        x.parseStringXpath(xml_data1);
        x.parseStringXpath(xml_data2);
    }
}

Result:
----------
name: item
attr: value= level1
text: Text1Text2Text3
name: item
attr: value= level2
text: Text2Text3
----------
name: item
attr: value= level1
text: Text1
name: item
attr: value= level2
text: Text2





댓글 없음:

댓글 쓰기