2013-02-03 81 views
1

我需要解析具有相同標記名稱的XML文檔。我給你這個代碼的樣本,看看有什麼我想要做的..使用DOM解析器解析XML文檔,每個標記具有多個元素

<SystemData> 
    <SystemName>xmlexample</SystemName> 
    <Schools> 
     <School> 
      <SchoolName>SCHOOL1</SchoolName> 
      <Classes> 
       <Class> 
        <ClassName>ACLASS</ClassName> 
       </Class> 
      </Classes> 
      <Classes> 
       <Class> 
        <ClassName>BCLASS</ClassName> 
       </Class> 
      </Classes> 
     </School> 

     <School> 
      <SchoolName>SCHOOL2</SchoolName> 
      <Classes> 
       <Class> 
        <ClassName>CCLASS</ClassName> 
       </Class> 
      </Classes> 
     </School> 
    </Schools> 
</SystemData> 

我想要的結果是:

SCHOOL1 
ACLASS 
BCLASS 

SCHOOL2 
CCLASS 

我與循環嘗試,但它讓我對每所學校的所有類..

到目前爲止我的代碼:

NodeList schoolist = doc.getElementsByTagName("School"); 
int num = schoolist.getLength(); 

for (int temp = 0; temp < num; temp++) { 
    Node nNode = schoolist.item(temp); 
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
     Element eElement = (Element) schoolist.item(temp); 
     Node schoolname = eElement.getFirstChild(); 

     System.out.println("schoolname: " + eElement.getElementsByTagName("ClassName").item(0).getTextContent()); 
    } 

    NodeList Classlist = doc.getElementsByTagName("Method"); 
    int num1 = Classlist.getLength(); 

    for (int i = 0; i < num1; i++) { 

     Node nNode1 = Classlist.item(i); 

     if (nNode1.getNodeType() == Node.ELEMENT_NODE) { 
      Element eElement1 = (Element) Classlist.item(i); 
      Node Classname = eElement1.getFirstChild(); 
      System.out.println("Classname: " + eElement1.getElementsByTagName("ClassName").item(0).getTextContent()); 

     } 
    } 
} 
+1

那麼你的代碼目前是什麼樣子? – JLRishe

+0

順便說一句 - 你的XML存在錯誤,你的第二個學校名稱的結束標記仍然是錯誤的。 –

+0

確定我加入我的代碼..(這就是我創建,但無論如何,我編輯的錯誤XML的一例) –

回答

0

標記名稱一些明顯的問題:

System.out.println("schoolname: " + eElement.getElementsByTagName("ClassName").item(0).getTextContent()); 

應該

System.out.println("schoolname: " + eElement.getElementsByTagName("SchoolName").item(0).getTextContent()); 

NodeList Classlist = doc.getElementsByTagName("Method"); 

應該

NodeList Classlist = doc.getElementsByTagName("Class"); 

然後,你不要想從文檔,但一切的一切Class元素Class文件那是目前的學校的孩子。

+0

是的,但我怎麼能做到這一點? (抱歉的錯誤,但我有另一個主要的代碼,我正在努力,我爲這個問題做了這個) –

0

所以finaly我設法解決code..This是如何運作..

 NodeList listOfSchool = doc.getElementsByTagName("School"); 


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


       Node firstSchoolNode = listOfSchool.item(s); 
       if(firstSchoolNode .getNodeType() == Node.ELEMENT_NODE){ 


        Element SchoolElement = (Element)firstSchoolNode ; 

        //------- 
        NodeList SchoolNameList = SchoolElement.getElementsByTagName("SchoolName"); 
        Element SchoolNameElement = (Element)SchoolNameList .item(0); 

        NodeList textFNList = SchoolNameElement.getChildNodes(); 
        System.out.println("School Name : " + 
          ((Node)textFNList.item(0)).getNodeValue().trim()); 

         //---- 

        NodeList listOfSchoolNames = SchoolElement.getElementsByTagName("Class"); 

        for(int i=0; i<listOfSchoolNames.getLength() ; i++){ 


       Node firstClassNode = listOfSchoolNames.item(i); 
       if(firstClassNode .getNodeType() == Node.ELEMENT_NODE){ 

        Element classElement = (Element)firstClassNode; 
        //---- 

         NodeList ClassNameList = classElement .getElementsByTagName("ClassName"); 


         Element ClassNameElement = (Element)ClassNameList.item(0); 

         NodeList textCLSNMList = ClassNameElement.getChildNodes(); 
          System.out.println("class Name : " + 
          ((Node)textCLSNMList .item(0)).getNodeValue().trim()); 

          } 
} 
0

我會建議使用javax.xml.xpath的API在Java SE 5及以上的,而不是getElementsByTagName讓你的代碼更可讀性更強,更易於維護。

import javax.xml.xpath.*; 
import org.w3c.dom.*; 
import org.xml.sax.InputSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     XPathFactory xpf = XPathFactory.newInstance(); 
     XPath xPath = xpf.newXPath(); 
     XPathExpression schoolNameExpression = xPath.compile("SchoolName"); 
     XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName"); 

     InputSource inputSource = new InputSource("src/forum14671896/input.xml"); 
     NodeList schoolNodes = (NodeList) xPath.evaluate("/SystemData/Schools/School", inputSource, XPathConstants.NODESET); 
     for(int x=0; x<schoolNodes.getLength(); x++) { 
      Node schoolElement = schoolNodes.item(x); 

      // School Name 
      System.out.print("School Name : "); 
      System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING)); 

      // Class Names 
      NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET); 
      for(int y=0; y<classNames.getLength(); y++) { 
       System.out.print("Class Name : "); 
       System.out.println(classNames.item(y).getTextContent()); 
      } 
      System.out.println(); 
     } 
    } 

} 

輸出

School Name : SCHOOL1 
Class Name : ACLASS 
Class Name : BCLASS 

School Name : SCHOOL2 
Class Name : CCLASS