본문 바로가기

Project 자료수집

[java.core] 간단한 String XML 파싱 예제

import java.io.File;
import java.io.FileInputStream;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
 
/*
 * Simple demo of JDOM
 */
public final class Xml_Test_0
{
    public static void main(String[] args)
    {
        String t_xmlStr =
            "<root>" +
            "<item value="\"돈까스\"">" +
            "<item value="\"순대국\"">" +
            "<item value="\"짜장면\"">" +
            "</item></item></item></root>";
 
        DocumentBuilderFactory t_dbf = null;
        DocumentBuilder t_db = null;
        Document t_doc = null;
        NodeList t_nodes = null;
        Node t_node = null;
        Element t_element = null;
        InputSource t_is = new InputSource();
 
        try
        {
            t_dbf = DocumentBuilderFactory.newInstance();
            t_db = t_dbf.newDocumentBuilder();
            t_is = new InputSource();
            t_is.setCharacterStream(new StringReader(t_xmlStr));
            t_doc = t_db.parse(t_is);
            t_nodes = t_doc.getElementsByTagName("item");
 
            for (int i = 0, t_len = t_nodes.getLength();
                    i < t_len; i ++)
            {
                t_element = (Element)t_nodes.item(i);
 
                System.out.println(t_element.getAttribute("value"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


'Project 자료수집' 카테고리의 다른 글

RAP 개발 관련 링크  (0) 2013.10.07
SWT 레이아웃 정리  (0) 2013.09.23
IMDB XML 가져오기  (0) 2013.09.23
API 및 RAP  (0) 2013.09.17
CEA-2014 관련 링크  (0) 2013.09.03