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">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.
Related
I don't know how to explain my situation, I can provide example below.
I have an XML file to be read in Java, something like this:
<Author AffiliationIDS="Aff1">
<AuthorName DisplayOrder="Western">
<GivenName>Wei</GivenName>
<GivenName>Long</GivenName>
<FamilyName>
<Value>Tan</Value>
</FamilyName>
</AuthorName>
</Author>
As you can see the <FamilyName> tag, inside the <FamilyName> tag is surrounded by a Value tag. This is because in the xsd it stated the element as maxOccurs="unbounded" which mean more than one value can be in that element tag. How should I modify the code so that it can read in the <FamilyName> tag and get Value tag element no matter how many occurrence of the Value exist?
Example:
<FamilyName>
<Value>Sarah</Value>
<Value>Johnson</Value>
</FamilyName>
The code look like this.
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("/fileaddress/test-1.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("AuthorName");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
System.out.println("Family Name : " + eElement.getElementsByTagName("FamilyName").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Get the FamilyName node by getElementsByTagName("FamilyName").item(0) and loop over its child nodes (.getChildNodes()) and get the value of the textNode
Or,
You can even getElementsByTagName("Value") if you are sure that value tag does not occur anywhere else other than inside FamilyName
Here is a code Sample
NodeList children = doc.getElementsByTagName("FamilyName").item(0).getChildNodes();
for(int i=0;i<children.getLength();i++) {
if(children.item(i).getNodeType()== Node.ELEMENT_NODE) {
Element child = (Element)children.item(i);
System.out.println(child.getTextContent());
}
}
I want to only get restaurants in beirut with dom parser please help
this is a part from my xml file :
city -> beirut->restaurants->restaurant->name->
tyr->restaurants->restaurant->name->
jbeil->restaurants->restaurant->name->
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
<restaurants>
<restaurant>
<name>
...
</restaurant>
</restaurants>
because this code give me all restaurants in all city's :
try {
File inputFile = new File("src/josephXml.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Restaurant");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
jTextArea1.append( "\n"+"Name : "+ eElement
.getElementsByTagName("name")
.item(0)
.getTextContent()+"\n "
, i only want restaurants in beirut please help
You use Restaurant, but your file uses restaurant. It's not OK, because case sensitive.
use XPath: you can select many things, it is very readable.
Replace this in your code:
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/city/beirut/restaurants/restaurant";
NodeList nList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
Hello this is my xml document :
<city>
<beirut>
<restaurant>
<name>sada</name>
</restaurant>
</beirut>
<jbeil>
<restaurant>
<name>toto</name>
<rating>4.3/5</rating>
</restaurant>
<restaurant>
<name>jojo</name>
<rating>4.3/5</rating>
</restaurant>
</jbeil>
<sour>
<restaurant>
<name>sada</name>
</restaurant>
</sour>
</city>
I want to update the rating of "jojo" restaurant in jbeil from 4.3/5 to 4.5/5 using xpath and netbeans please help ,
this code give the rating ,
try {
File inputFile = new File("src/xpath/josephXml.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/City/Jbeil/Restaurant";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node nNode = nodeList.item(i);
System.out.println("\nCurrent Element :"
+ nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("rating : "
+ eElement
.getElementsByTagName("rating")
.item(0)
.getTextContent());
and i want only to update the rating of restaurant in jbeil where name is "jojo" , please help
This is one possible XPath to find such restaurant named jojo in the city of jbeil and then return the corresponding rating element :
/city/jbeil/restaurant[name='jojo']/rating
Notice that XML & XPath are case-sensitive, so I used all lower-case characters in the above XPath to match the XML posted in this question.
I don't know much about Java, but quick searching over the internet* suggest something like this :
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/city/jbeil/restaurant[name='jojo']/rating";
Element e = (Element)xpath.evaluate(expression, doc, XPathConstant.NODE);
if (e != null)
e.setTextContent("4.5/5");
*) how to modify xml tag specific value in java?
I'm trying to read from xml by this code and its not working .. I need to read from id in the specific tag (getelementbyid)
what I need to change in my java code
THIS is my xml code
<book>
<person>
<name>
<first id=1 name="Kiran"/>
<first id=2 name="Pai"/>
</name>
<age>22</age>
</person>
<person>
<name>
<first id=1 name="Bill"/>
<first id=2 name="Gates"/>
</name>
<age>46</age>
</person>
<person>
<name>
<first id=1 name="Steve"/>
<first id=2 name="Jobs"/>
</name>
<age>40</age>
</person>
</book>
and this is my java code
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader {
public static void main(String argv[]) {
try {
File file = new File("c:\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("employee");
System.out.println("Information of all employees");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can take a look at JAXB. It is part of Java SE. You only need to map your XML schema to few POJO classes and describe them with JAXB annotations.
Possible code to map an XML to an object, and vice versa, will take ~5 lines.
You can read about it here: http://docs.oracle.com/javase/tutorial/jaxb/intro/
This should work:
public class XMLReader {
public static void main(String argv[]) {
try {
File file = new File("c:\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("name");
System.out.println("Information of all employees");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("first");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
System.out.println("First Name : " +fstNmElmnt.getAttributeNode("name").getNodeValue());
Element lstNmElmnt = (Element) fstNmElmntLst.item(1);
System.out.println("Last Name : " +lstNmElmnt.getAttributeNode("name").getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And correct your xml to quote the attributes:
<book>
<person>
<name>
<first id="1" name="Kiran"/>
<first id="2" name="Pai"/>
</name>
<age>22</age>
</person>
<person>
<name>
<first id="1" name="Bill"/>
<first id="2" name="Gates"/>
</name>
<age>46</age>
</person>
<person>
<name>
<first id="1" name="Steve"/>
<first id="2" name="Jobs"/>
</name>
<age>40</age>
</person>
</book>
One of the problems is that the built in Java XML libraries are only so-so. While meant primarily for for HTML, if you are not using xpaths, JSoup is a lot easier to work with.
String yourXml = ...
Document doc = Jsoup.parse(yourXml, "", Parser.xmlParser());
for (Element person : doc.getElementsByTag("person")) {
String firstName = person.getElementById("1").text();
String lastName = person.getElementById("2").text();
}
I have this XML:
<root>
<items>
<item1>
<tag1>1</tag1>
<sub>
<sub1>10 </sub1>
<sub2>20 </sub2>
</sub>
</item1>
<item2>
<tag1>1</tag1>
<sub>
<sub1> </sub1>
<sub2> </sub2>
</sub>
</item2>
</items>
</root>
I want to get the item1 element and the name and values of the child elements.
That is, i want to get: tag1 - 1,sub1-10,sub2-20.
How can i do this? so far i can only get elements without children.
Document doc = ...;
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/root/items/item1/*/text()");
Object o = expr.evaluate(doc, XPathConstants.NODESET);
NodeList list = (NodeList) o;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
/**
* File: Ex1.java #author ronda
*/
public class Ex1 {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = Factory.newDocumentBuilder();
Document doc = builder.parse("myxml.xml");
//creating an XPathFactory:
XPathFactory factory = XPathFactory.newInstance();
//using this factory to create an XPath object:
XPath xpath = factory.newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("//" + "item1" + "/*");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Element el = (Element) nodes.item(i);
System.out.println("tag: " + el.getNodeName());
// seach for the Text children
if (el.getFirstChild().getNodeType() == Node.TEXT_NODE)
System.out.println("inner value:" + el.getFirstChild().getNodeValue());
NodeList children = el.getChildNodes();
for (int k = 0; k < children.getLength(); k++) {
Node child = children.item(k);
if (child.getNodeType() != Node.TEXT_NODE) {
System.out.println("child tag: " + child.getNodeName());
if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
System.out.println("inner child value:" + child.getFirstChild().getNodeValue());;
}
}
}
}
}
I get this output loading the xml of your question in file named: myxml.xml:
run:
2
tag: tag1
inner value:1
tag: sub
inner value:
child tag: sub1
inner child value:10
child tag: sub2
inner child value:20
...a bit wordy, but allow us to understand how it works. PS: I found a good guide in here