xml parsing with java
I'm new to xml parsing, I am trying to parse the following xml file using 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"&开发者_如何学运维gt;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>
I only need to list the name tag info(which will be the Hotel name) for that I used following code but it resulted me not only the hotel info but also everything, can anyone please help me parsing this???
Thanks a lot!!!
Here is the java code I used
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();
}
}
You could use xpath to get all the name
nodes:
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/hotels//name");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
Pavithira you can use xpath to get only the hotels, below is the main method which you can simple copy/paste at your code.
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();
}
You are iterating over the 'e' nodes, so your loop will print out every node inside any 'e' node (which includes the (sub)root node!). Change your getElementsByTagName paramater to "name" if you only want to retrieve and print those nodes.
精彩评论