Java: DocumentBuilderFactory 호출 XML 메소드 인스턴스

먼저 제공: DOM 해석기를 받은 공장 인스턴스 DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
그리고 DOM 공장에서 DOM 해상도를 가져옵니다.
 DocumentBuilder dombuilder=domfac.newDocumentBuilder();
 
) 해석할 XML 문서를 DOM 해석기가 해석할 수 있도록 입력 흐름으로 변환
InputStream is= new  FileInputStream("test1.xml");        
(4) XML 문서의 입력 흐름을 분석하여 Document를 가져옵니다.
 Document doc=dombuilder.parse(is);
(5) XML 문서의 루트 노드 가져오기
Element root=doc.getDocumentElement();
(6) 노드의 하위 노드 얻기
  NodeList books=root.getChildNodes();

package com.st.demo;  

import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  

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;  

public class XmlReader {  
    public static void main(String[] args) {  
        XmlReader reader = new XmlReader();  
    }  
    public XmlReader(){  
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();  
        try {  
            DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
            InputStream is = new FileInputStream(new File("D:/test1.xml"));  
            Document doc = domBuilder.parse(is);  
            Element root = doc.getDocumentElement();  
            NodeList books = root.getChildNodes();  
            if(books!=null){  
                for (int i = 0; i < books.getLength(); i++) {  
                    Node book = books.item(i);  
                     if(book.getNodeType()==Node.ELEMENT_NODE) {  
                            //(7)   
                            String email=book.getAttributes().getNamedItem("email").getNodeValue();  
                            System.out.println(email);  
                            // , 。 Node.ELEMENT_NODE  
                            //(8)   
                            for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {  
                                if(node.getNodeType()==Node.ELEMENT_NODE) {  
                                    if(node.getNodeName().equals("name")) {  
                                        String name=node.getNodeValue();  
                                        String name1=node.getFirstChild().getNodeValue();  
                                        System.out.println(name);  
                                        System.out.println(name1);  
                                    }  
                                    if(node.getNodeName().equals("price")) {  
                                        String price=node.getFirstChild().getNodeValue();  
                                        System.out.println(price);  
                                    }  
                                }  
                            }  
                     }  
                }  
            }  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  

    }  

좋은 웹페이지 즐겨찾기