2011-06-29 192 views
0

我是新來的xml解析,我正在嘗試使用java解析下面的xml文件。xml用java解析

<a> 
<e class="object"> 
    <amenities class="array"> 
     <e class="object"> 
      <id type="number">31</id> 
      <name type="string">Internet access available</name> 
     </e> 
     <e class="object"> 
      <id type="number">9</id> 
      <name type="string">Business center</name> 
     </e> 

</amenities> 
<brands class="array"> 
     <e class="object"> 
      <code type="number">291</code> 
      <name type="string">Utell</name> 
     </e> 
     <e class="object"> 
      <code type="number">72</code> 
      <name type="string">Best Western International</name> 
     </e> 


</brands> 
<hotels class="array"> 
     <e class="object"> 
      <addressLine1 type="string">4 Rue du Mont-Thabor</addressLine1>    
      <city type="string">Paris</city>     
      <name type="string">Renaissance Paris Vendome Hotel</name> 
      <starRating type="string">5</starRating>     
     </e> 
     <e class="object"> 
      <addressLine1 type="string">35 Rue de Berri</addressLine1>    
      <city type="string">Paris</city>     
      <name type="string">Crowne Plaza Hotel PARIS-CHAMPS ELYSÉES</name> 
      <starRating type="string">5</starRating>     
     </e> 
</hotels>  

</e> 
</a> 

我只需要列出名稱的標籤信息(這將是酒店名稱),我用下面的代碼,但它導致我不僅酒店信息,但也應有盡有,任何人都可以請幫我分析這個???

非常感謝!

這裏是Java代碼我用

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import org.w3c.dom.Element; 
import java.io.File; 


public class ReadXMLFile { 

public static void main(String argv[]) { 

try { 

    File fXmlFile = new File("c:\\file.xml"); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(fXmlFile); 
    doc.getDocumentElement().normalize(); 

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
    NodeList nList = doc.getElementsByTagName("e"); 
    System.out.println("-----------------------"); 

    for (int temp = 0; temp < nList.getLength(); temp++) { 

     Node nNode = nList.item(temp);  
     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

      Element eElement = (Element) nNode; 

      System.out.println("Hotel Name : " + getTagValue("name",eElement)); 


     } 
    } 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 

private static String getTagValue(String sTag, Element eElement){ 
    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); 
    Node nValue = (Node) nlList.item(0); 

    return nValue.getNodeValue();  
} 

} 
+3

您應該查找更方便的XPath。 – fyr

+0

你是否錯過了一個閉幕標記 – bingjie2680

+0

@ bingjie2680是的,我是,我糾正了它:) –

回答

3

Pavithira您可以使用XPath只得到酒店,下面是主要的方法,你可以簡單的複製/粘貼在你的代碼。

public static void main(String argv[]) { 
     try { 

       File fXmlFile = new File("c:\\file.xml"); 

     DocumentBuilderFactory dbFactory = DocumentBuilderFactory 
       .newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(fXmlFile); 
     doc.getDocumentElement().normalize(); 

     System.out.println("Root element :" 
       + doc.getDocumentElement().getNodeName()); 
     XPathFactory factory = XPathFactory.newInstance(); 
     XPath xpath = factory.newXPath(); 
     XPathExpression expr = xpath.compile("//hotels/e"); 
     NodeList nList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 
     System.out.println("-----------------------"); 
     for (int temp = 0; temp < nList.getLength(); temp++) { 
      Node nNode = nList.item(temp); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
       Element eElement = (Element) nNode; 
       System.out.println("Hotel Name : " 
         + getTagValue("name", eElement)); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 

    } 
+0

謝謝Zemzela,現在它工作正常...再次感謝您的幫助:) –

+0

歡迎:) – Zemzela

1

您遍歷「E」的節點,所以你的循環會打印出任何「e」的節點中的每個節點(包括(子)根節點!)。如果您只想檢索並打印這些節點,請將您的getElementsByTagName參數更改爲「name」。

4

您可以使用XPath來獲取所有name節點:

XPath xpath = XPathFactory.newInstance().newXPath(); 
XPathExpression expr = xpath.compile("/hotels//name"); 
Object result = expr.evaluate(doc, XPathConstants.NODESET); 
NodeList nodes = (NodeList) result; 
+0

非常感謝:)我不那麼熟悉與Xpath,我會檢查它... –