2016-05-12 53 views
0

使用DOM-XML兩個元素是否有使用「的getElementsByTagName」查詢在JAVA

我試圖讓我的程序解析和XML從兩個不同的標記「學校DOM來查詢兩個標籤名AA方式「&」醫院「,並顯示每個的」ID「和」文本「屬性

這可能嗎?

我的代碼是:

import java.io.IOException; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

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; 

public class DomParse { 

    public static void main(String[] args) { 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

    try { 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document doc = builder.parse("input2.xml"); 

    NodeList School = doc.getElementsByTagName("School"); 
    for(int i=0;i<School.getLength();i++){ 
     Node p = School.item(i); 
     if(p.getNodeType()==Node.ELEMENT_NODE){ 
      Element person = (Element) p; 
      String id = person.getAttribute("id"); 
      NodeList nameList = person.getChildNodes(); 
      for(int j=0;j<nameList.getLength();j++){ 
       Node n = nameList.item(j); 
       if(n.getNodeType()==Node.ELEMENT_NODE){ 
        Element name = (Element) n; 
        System.out.println("School " + id + ": " + "\n" + "\n" + name.getTextContent()); 

        NodeList Hospital = doc.getElementsByTagName("Hospitals"); 
        for(int z=0;z<Hospital.getLength();z++){ 
         Node k = Hospital.item(z); 
         if(k.getNodeType()==Node.ELEMENT_NODE){ 
         Element uc = (Element) k; 
         NodeList uniqueList = uc.getChildNodes(); 
         for(int l=0;l<uniqueList.getLength();l++){ 
         Node a = uniqueList.item(l); 
          if(a.getNodeType()==Node.ELEMENT_NODE){ 
           Element uniqcon = (Element) a; 
           System.out.println("Hospitals " + id + ": " + "\n" + "\n" + uniqcon.getTextContent() + "\n"); 

          } 
         } 
         } 
        } 
       } 
      } 
     } 
    } 

    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
} 
} 

} 

輸入文件是:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
<school> 
<Hospitals size="standard"> 
     <Hospital codeSet="base" included="true" analysis="nI" definedAt="scan" id="0"> 
      <text>New England Medical. Services only urgent procedures.</text> 
      <scanPatterns> 
       <scanPattern occurs="1" scanPatternId="45" definedAt="scan" id="0"/> 
       <scanPattern occurs="1" scanPatternId="46" definedAt="scan" id="1"/> 
      </scanPatterns> 
     </Hospital> 

     <Hospital codeSet="base" included="true" analysis="I" SchoolId="4" definedAt="scan" id="1"> 
      <text>Gibson Memorial. 20 miles away.</text> 
      <scanPatterns> 
       <scanPattern occurs="1" scanPatternId="42" definedAt="scan" id="0"/> 
       <scanPattern occurs="1" scanPatternId="39" definedAt="scan" id="1"/> 
      </scanPatterns> 
     </Hospital> 
     </Hospitals> 
<Schools> 
     <School definedAt="scan" id="0"> 
      <text>Craven County Middle School</text> 
     </School> 
     <School definedAt="scan" id="1"> 
      <text>WestLake Middle School</text> 
     </School> 
     </Schools> 
</school> 
+1

在DOM導航是痛苦的,看看XPath的一個更簡單的方法訪問文檔中的值http://stackoverflow.com/a/2811101/3215527 – wero

+0

您可以創建一個方法獲取'NodeList'作爲輸入,循環遍歷元素並打印所需的值。假設該方法的名字是'printElements',你可以使用'printElements(doc.getElementsByTagName(「School」)''和'printElements(doc.getElementsByTagName(「Hospital」)''來調用它。另外:爲什麼你打印所有的醫院學校? –

回答

0

如果您使用XPath,這個工作會容易得多。

XPath xPath = XPathFactory.newInstance().newXPath(); 
String schoolExpression = "//School"; 
String hospitalExpression = "//Hospital"; 

獲取的NodeList使用XPath表達式,然後遍歷節點並獲得ID和文本內容:

NodeList nodeList = (NodeList) xPath.compile(schoolExpression).evaluate(xmlDocument, XPathConstants.NODESET); 
for (int i = 0; i < nodeList.getLength(); i++) { 
     System.out.println(nodeList.item(i).getAttributes().getNamedItem("id").getNodeValue()); // gets you your id 
     System.out.println(nodeList.item(i).getTextContent()); // gets you the context   
}