자바로 만든 XML 파일 읽어서 필요한 정보 PARSING 하기 :: JAVA 공작소[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

JAVA 공작소
[1]
등록일:2008-11-26 22:25:46 (0%)
작성자:
제목:자바로 만든 XML 파일 읽어서 필요한 정보 PARSING 하기
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.SAXException;
import  org.xml.sax.SAXParseException;

class  xmlFileRead  {

        public  void  xmlFileRead()  {                
                
                int  totalItems=0;
                NodeList  listOfItem=null;
                Node  firstItemNode=null;
                Element  firstItemElement=null;

                try  {
                        
                        String  url  =  "http://ssiso.net/index.xml";
                        
                        DocumentBuilderFactory  docBuilderFactory  =  DocumentBuilderFactory
                                        .newInstance();
                        DocumentBuilder  docBuilder  =  docBuilderFactory.newDocumentBuilder();
                        Document  doc  =  docBuilder.parse(url);

                        //  normalize  text  representation
                        doc.getDocumentElement().normalize();
                        System.out.println("Root  element  of  the  doc  is  "
                                        +  doc.getDocumentElement().getNodeName());

                        listOfItem  =  doc.getElementsByTagName("item");
                        totalItems  =  listOfItem.getLength();
                        System.out.println("Total  no  of  Article  :  "  +  totalItems);

                        for  (int  s  =  0;  s  <  listOfItem.getLength();  s++)  {

                                firstItemNode  =  listOfItem.item(s);
                                if  (firstItemNode.getNodeType()  ==  Node.ELEMENT_NODE)  {

                                        firstItemElement  =  (Element)  firstItemNode;

                                        printElement(firstItemElement,  "title");
                                        printElement(firstItemElement,  "link");
                                        printElement(firstItemElement,  "pubDate");

                                }//  end  of  if  clause

                        }//  end  of  for  loop  with  s  var

                }  catch  (SAXParseException  err)  {
                        System.out.println("**  Parsing  error"  +  ",  line  "
                                        +  err.getLineNumber()  +  ",  uri  "  +  err.getSystemId());
                        System.out.println("  "  +  err.getMessage());

                }  catch  (SAXException  e)  {
                        Exception  x  =  e.getException();
                        ((x  ==  null)  ?  e  :  x).printStackTrace();

                }  catch  (Throwable  t)  {
                        t.printStackTrace();
                }
        }
        
        private  void  printElement(Element  firstItemElement,  String  nodeName){
                
                NodeList  elementList=null;
                Element  lastNameElement=null;
                NodeList  textLNList=null;
                
                try  {
                        
                        elementList  =  firstItemElement.getElementsByTagName(nodeName);
                        lastNameElement  =  (Element)  elementList.item(0);

                        textLNList  =  lastNameElement.getChildNodes();
                        System.out.println(nodeName.toUpperCase()  +  "  :  "
                                                        +  ((Node)  textLNList.item(0))
                                                                        .getNodeValue().trim());
                        
                }  catch  (Exception  e)  {
                        e.printStackTrace();
                }
                
        }
}

public  class  xmlReader  {

        public  static  void  main(String[]  args)  {

                xmlFileRead  xmlFileRead  =  new  xmlFileRead();
                xmlFileRead.xmlFileRead();

        }
}
[본문링크] 자바로 만든 XML 파일 읽어서 필요한 정보 PARSING 하기
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=31418
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.