xml用java解析

问题描述:

我是新来的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是的,我是,我纠正了它:) –

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

您遍历“E”的节点,所以你的循环会打印出任何“e”的节点中的每个节点(包括(子)根节点!)。如果您只想检索并打印这些节点,请将您的getElementsByTagName参数更改为“name”。

您可以使用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,我会检查它... –