2014-09-12 2 views
0

어떻게 DOM 파서를 사용하여 다음 XML을 구문 분석 - name 노드 내가 값을 얻기 위해 다음 코드를 사용DOM 파서를 사용하여 다음 XML을 구문 분석하는 방법은 무엇입니까?

<Services> 
    <service name ="qwerty" id=""> 
    <File rootProfile="abcd" extension="acd"> 
    <Columns> 
    <name id="0" profileName="DATE" type="java"></name> 
    <name id="1" profileName="DATE" type="java"></name> 
    . 
    . 
    . 
    <Columns> 
    </File> 
    <File rootProfile="efg" extension="ghi"> 
    <Columns> 
    <name id="a" profileName="DATE" type="java"></name> 
    <name id="b" profileName="DATE" type="java"></name> 
    . 
    . 
    . 
    <Columns> 
    </File> 
    </service> 
    </Services> 

N 수준까지 반복된다

public static void xmlEditor() throws SAXException, IOException { 
    try { 
     File xmlFile = new File("config/ServiceConfig.xml"); 
     DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder documentBuilder = null; 

     documentBuilder = documentFactory.newDocumentBuilder(); 

     org.w3c.dom.Document doc = documentBuilder.parse(xmlFile); 
     doc.getDocumentElement().normalize(); 
     NodeList nodeList0 = doc.getElementsByTagName("Service"); 
     NodeList nodeList1 = doc.getElementsByTagName("File"); 
     NodeList nodeList2 = doc.getElementsByTagName("name"); 
     System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 

     for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) { 
      Node node0 = nodeList0.item(temp0); 

      System.out.println("\nElement type :" + node0.getNodeName()); 
      Element Service = (Element) node0; 

      if (node0.getNodeType() == Node.ELEMENT_NODE) { 
       System.out.println("-----------------" + temp0 + "----------------------------------"); 
       System.out.println("name : " + Service.getAttribute("name")); 
       System.out.println("id : " + Service.getAttribute("id")); 


       for (int temp = 0; temp < nodeList1.getLength(); temp++) { 
        Node node1 = nodeList1.item(temp); 

        System.out.println("\nElement type :" + node1.getNodeName()); 

        Element File = (Element) node1; 

        if (node1.getNodeType() == Node.ELEMENT_NODE) { 

         System.out.println("rootProfile:" + File.getAttribute("rootProfile")); 
         System.out.println("extension : " + File.getAttribute("extension")); 


         for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) { 
          Node node2 = nodeList2.item(temp1); 

          System.out.println("\nElement type :" + node2.getNodeName()); 

          Element name = (Element) node2; 

          if (node2.getNodeType() == Node.ELEMENT_NODE) { 

           System.out.println("id:" + name.getAttribute("id")); 
           System.out.println("profileName : " + name.getAttribute("profileName")); 
           System.out.println("type : " + name.getAttribute("type")); 

          } 

         } 
        } 

       } 
      } 

     } 

    } 
} 

을 그리고 첫 번째 수준을 얻고있다 노드 File 그 자체가 name 다음 파일 노드 도움말을 포함하여 모든 valus를 얻고 있습니다. 출력이 올라가고 있습니다.

name:----- 
id :----- 

rootProfile:-------- 
extension:----------- 

id:o 
profileName: 
type: 
id:1 
profileName: 
type: 
id:a 
profileName: 
type: 
id:b 
profileName: 
rootProfile:-------- 
extension:----------- 

id:o 
profileName: 
type: 
id:1 
profileName: 
type: 
id:a 
profileName: 
type: 
id:b 
profileName: 

답변

1

전체 예제가 게시됩니다. 당신은 내가 사용했던 String 대신 File에서 XML을 읽어야 할 수도있다.

import java.io.ByteArrayInputStream; 
import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

public class MyTester { 

    public static void main(String[] args) throws IOException, SAXException, 
      ParserConfigurationException { 
     xmlEditor(); 
    } 

    public static void xmlEditor() throws SAXException, IOException, 
      ParserConfigurationException { 
     // File xmlFile = new File("ServiceConfig.xml"); 
     String xml = "<Services><service name=\"qwerty\" id=\"\"><File rootProfile=\"abcd\" extension=\"acd\"><Columns>" 
       + "<name id=\"0\" profileName=\"DATE\" type=\"java\"></name><name id=\"1\" profileName=\"DATE\" type=\"java\"></name>" 
       + "</Columns></File><File rootProfile=\"efg\" extension=\"ghi\"><Columns><name id=\"a\" profileName=\"DATE\" type=\"java\"></name>" 
       + "<name id=\"b\" profileName=\"DATE\" type=\"java\"></name></Columns></File></service></Services>"; 
     DocumentBuilderFactory documentFactory = DocumentBuilderFactory 
       .newInstance(); 
     DocumentBuilder documentBuilder = null; 

     documentBuilder = documentFactory.newDocumentBuilder(); 

     // org.w3c.dom.Document doc = documentBuilder.parse(xmlFile); 
     org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(
       new ByteArrayInputStream(xml.getBytes()))); 

     doc.getDocumentElement().normalize(); 
     NodeList nodeList0 = doc.getElementsByTagName("service"); 
     NodeList nodeList1 = null; 
     NodeList nodeList2 = null; 
     System.out.println("Root element :" 
       + doc.getDocumentElement().getNodeName()); 

     for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) { 
      Node node0 = nodeList0.item(temp0); 

      System.out.println("\nElement type :" + node0.getNodeName()); 
      Element Service = (Element) node0; 

      if (node0.getNodeType() == Node.ELEMENT_NODE) { 
       System.out.println("-----------------" + temp0 
         + "----------------------------------"); 
       System.out.println("name : " + Service.getAttribute("name")); 
       System.out.println("id : " + Service.getAttribute("id")); 
       nodeList1 = Service.getChildNodes(); 
       for (int temp = 0; temp < nodeList1.getLength(); temp++) { 
        Node node1 = nodeList1.item(temp); 

        System.out 
          .println("\nElement type :" + node1.getNodeName()); 

        Element File = (Element) node1; 

        if (node1.getNodeType() == Node.ELEMENT_NODE) { 

         System.out.println("rootProfile:" 
           + File.getAttribute("rootProfile")); 
         System.out.println("extension : " 
           + File.getAttribute("extension")); 

         nodeList2 = File.getChildNodes();// colums 
         for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) { 
          Element column = (Element) nodeList2.item(temp1); 
          NodeList nodeList4 = column.getChildNodes(); 
          for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) { 
           Element name = (Element) nodeList4.item(temp3); 
           if (name.getNodeType() == Node.ELEMENT_NODE) { 

            System.out.println("id:" 
              + name.getAttribute("id")); 
            System.out.println("profileName : " 
              + name.getAttribute("profileName")); 
            System.out.println("type : " 
              + name.getAttribute("type")); 

           } 
          } 
         } 

        } 

       } 
      } 

     } 
    } 

} 

출력 :

루트 요소 : 서비스

요소 유형 : 서비스 ----------------- 0 ---- ------------------------------ 이름 : qwerty id :

요소 유형 : 파일 rootProfile : abcd 확장명 : acd id : 0 profileName : 날짜 유형 : java id : 1 profileName : 날짜 유형 : java

요소 유형 : 파일 rootProfile : EFG 확장 : GHI ID : 프로파일 이름 : DATE 타입 : 자바 ID : 프로파일 이름 B : DATE 유형 : 자바

+0

작동하지 yaar – SUBZ

관련 문제