BBS > xml

java parsing IXMLElement

작성자 : 이창우 (x1wins) | 등록일 : 2011-10-24 | 목록
첨부 파일이 없습니다.
첨부 이미지가 없습니다.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<result>
<rtcode>1</rtcode>
<rtmsg>SUCCESS</rtmsg>
</result>
<list>
<vlu_cd>Ticket counter</vlu_cd>
<yn_hndl>3</yn_hndl>
</list>
<list>
<vlu_cd>Airplane</vlu_cd>
<yn_hndl>3</yn_hndl>
</list>
<list>
<vlu_cd>Immigration</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Beggage Claim</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Currency Money Exchange</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Taxi and Bus Station</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Food court</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Hospital</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Hotel</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>Department Store</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>U.S.A. Culture Center</vlu_cd>
<yn_hndl/>
</list>
<list>
<vlu_cd>U.K. Culture Center</vlu_cd>
<yn_hndl/>
</list>
</response>
package com.englishvillage;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import net.n3.nanoxml.IXMLElement;
import net.n3.nanoxml.IXMLParser;
import net.n3.nanoxml.IXMLReader;
import net.n3.nanoxml.StdXMLReader;
import net.n3.nanoxml.XMLElement;
import net.n3.nanoxml.XMLParserFactory;


public class XmlParser {

	public XmlParser(){
		System.out.println("-------- test -----------");
		test(); 
	}
	
	FileInputStream fis = null;
	BufferedReader br = null;
	String urlXmlData = "";
	
	public static void getElement(String xmlData){
		
		try{
			IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
			IXMLReader reader = StdXMLReader.stringReader(xmlData);
			parser.setReader(reader);


			IXMLElement response = (IXMLElement) parser.parse();
			System.out.println("response.getChildrenCount() = "+response.getChildrenCount());
			
			Vector vec = (Vector)response.getChildren();
			System.out.println("vec cnt : " + vec.size());
			
			for(int i=1; i<vec.size(); i++){
				XMLElement elem1= (XMLElement)vec.elementAt(i);
				System.out.println("vlu_cd "+elem1.getFirstChildNamed("vlu_cd").getContent());
				System.out.println("yn_hndl "+elem1.getFirstChildNamed("yn_hndl").getContent());
			}

		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public void test(){
		try{			
			fis = new FileInputStream("test.xml");			
			br  = new BufferedReader(new InputStreamReader(fis, "utf-8"));
						
			while(br.ready()){
				urlXmlData = urlXmlData +br.readLine() +"\n";				
			}			
			
		}catch(Exception ex){
			ex.printStackTrace();

		}finally{
			try{	
				if(br != null) 			br.close();
				if(fis != null) 			fis.close();
			} catch(Exception e){}
		}

		try{
			System.out.println("urlXmlData : " + urlXmlData);
			IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
			IXMLReader reader = StdXMLReader.stringReader(urlXmlData);
			parser.setReader(reader);


			IXMLElement xml = (IXMLElement) parser.parse();
			System.out.println(xml.getChildrenCount());
			
			XMLElement elem_context = (XMLElement)xml.getChildrenNamed("CONTEXT").elementAt(0);			
			System.out.println(elem_context.getChildrenCount());
						
			Vector vec = elem_context.getChildrenNamed("item");
			System.out.println("由ъ???cnt : " + vec.size());
			for(int i = 0; i < vec.size(); i++){
				XMLElement elem1= (XMLElement)vec.elementAt(i);				 
				System.out.println(elem1.getFirstChildNamed("Fahrenheit").getContent());
				System.out.println(elem1.getFirstChildNamed("centigrade").getContent());			
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
}
59:6:609|@0x00000A3C|JAVA|response.getChildrenCount() = 13
59:6:609|@0x00000A3C|JAVA|vec cnt : 13
59:6:609|@0x00000A3C|JAVA|vlu_cd Ticket counter
59:6:609|@0x00000A3C|JAVA|yn_hndl 3
59:6:609|@0x00000A3C|JAVA|vlu_cd Airplane
59:6:609|@0x00000A3C|JAVA|yn_hndl 3
59:6:609|@0x00000A3C|JAVA|vlu_cd Immigration
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Beggage Claim
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Currency Money Exchange
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Taxi and Bus Station
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Food court
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Hospital
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Hotel
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd Department Store
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd U.S.A. Culture Center
59:6:609|@0x00000A3C|JAVA|yn_hndl null
59:6:609|@0x00000A3C|JAVA|vlu_cd U.K. Culture Center
59:6:609|@0x00000A3C|JAVA|yn_hndl null
IXMLElement 사용법

nanoxml 의
IXMLElement 을 이용하여 자바에서 파싱을 해보자

참 쉽죠잉?ㅋㅋㅋ

다운로드 http://nanoxml.sourceforge.net/orig/download.html
로그인 하셔야 댓글을 사용하실수 있습니다.
댓글쓰기를 누르면 로그인 페이지로 이동 후 돌아옵니다.
총 댓글 겟수 0
번호 제목 등록일 작성자 조회수
351 HttpConnect (0) 2011-10-24 이창우 2653
-> java parsing IXMLElement (0) 2011-10-24 이창우 3397
109 xstream (0) 2011-05-18 이창우 2663
< 1 >
글쓰기 검색