read from XML in java - 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();
}

Related

How to read a specific XML node with a given value in Java

Given an XML content like below.
Is there an API that can return the whole student node when I pass just the value of rollno. For e.g., If I pass 493 then I should get the student info for 493 with all child elements like firstname, lastname,.. marks etc
<?xml version = "1.0"?>
<class>
<old>
<student rollno = "393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
</old>
<current>
<student rollno = "593">
<firstname>jasvir</firstname>
<lastname>singn</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
<student rollno = "125">
<firstname>abcd</firstname>
<lastname>defc</lastname>
<nickname>dumy</nickname>
<marks>90</marks>
</student>
<student rollno = "965">
<firstname>xyz</firstname>
<lastname>defc</lastname>
<nickname>sample</nickname>
<marks>90</marks>
</student>
</current>
</class>
Not sure about getting the whole set with one call, but you can get the value for each tag...something like:
File fXmlFile = new File("student.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("student");
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("Student rollno : "
+ eElement.getAttribute("rollno"));
System.out.println("Firstname : "
+ eElement.getElementsByTagName("firstname")
.item(0).getTextContent());
System.out.println("Last Name : "
+ eElement.getElementsByTagName("lastname")
.item(0).getTextContent());
System.out.println("Nickname : "
+ eElement.getElementsByTagName("nickname")
.item(0).getTextContent());
System.out.println("Marks : "
+ eElement.getElementsByTagName("marks")
.item(0).getTextContent());
}
}

parse xml using dom java

I have the bellow xml:
<modelingOutput>
<listOfTopics>
<topic id="1">
<token id="354">wish</token>
</topic>
</listOfTopics>
<rankedDocs>
<topic id="1">
<documents>
<document id="1" numWords="0"/>
<document id="2" numWords="1"/>
<document id="3" numWords="2"/>
</documents>
</topic>
</rankedDocs>
<listOfDocs>
<documents>
<document id="1">
<topic id="1" percentage="4.790644689978203%"/>
<topic id="2" percentage="11.427632949428334%"/>
<topic id="3" percentage="17.86913349249596%"/>
</document>
</documents>
</listOfDocs>
</modelingOutput>
Ι Want to parse this xml file and get the topic id and percentage from ListofDocs
The first way is to get all document element from xml and then I check if grandfather node is ListofDocs.
But the element document exist in rankedDocs and in listOfDocs, so I have a very large list.
So I wonder if exist better solution to parse this xml avoiding if statement?
My code:
public void parse(){
Document dom = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
dom = db.parse(is);
Element doc = dom.getDocumentElement();
NodeList documentnl = doc.getElementsByTagName("document");
for (int i = 1; i <= documentnl.getLength(); i++) {
Node item = documentnl.item(i);
Node parentNode = item.getParentNode();
Node grandpNode = parentNode.getParentNode();
if(grandpNode.getNodeName() == "listOfDocs"{
//get value
}
}
}
First, when checking the node name you shouldn't compare Strings using ==. Always use the equals method instead.
You can use XPath to evaluate only the document topic elements under listOfDocs:
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = xPath.compile("//listOfDocs//document/topic");
NodeList topicnl = (NodeList) xPathExpression.evaluate(dom, XPathConstants.NODESET);
for(int i = 0; i < topicnl.getLength(); i++) {
...
If you do not want to use the if statement you can use XPath to get the element you need directly.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("source.xml");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/*/listOfDocs/documents/document/topic");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getAttributes().getNamedItem("id"));
System.out.println(nodes.item(i).getAttributes().getNamedItem("percentage"));
}
Please check GitHub project here.
Hope this helps.
I like to use XMLBeam for such tasks:
public class Answer {
#XBDocURL("resource://data.xml")
public interface DataProjection {
public interface Topic {
#XBRead("./#id")
int getID();
#XBRead("./#percentage")
String getPercentage();
}
#XBRead("/modelingOutput/listOfDocs//document/topic")
List<Topic> getTopics();
}
public static void main(final String[] args) throws IOException {
final DataProjection dataProjection = new XBProjector().io().fromURLAnnotation(DataProjection.class);
for (Topic topic : dataProjection.getTopics()) {
System.out.println(topic.getID() + ": " + topic.getPercentage());
}
}
}
There is even a convenient way to convert the percentage to float or double. Tell me if you like to have an example.

Java XML search error

Hello I am getting an unexpected error, please help me out!?
I want to search for the name of a person and display all the available information about him.
In the following code I am trying to find the person with first name Ivan and this "translate" is copied from other xml topic in stackoverflow as a incase-sensitive option.
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
Document doc = factory.newDocumentBuilder().parse(
new File("staff.xml"));
XPathFactory xFactory = XPathFactory.newInstance();
XPath xPath = xFactory.newXPath();
XPathExpression exp = xPath
.compile("/staff/person/name/firstName[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'Ivan')]");
NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(),
XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
System.out.println(node.getTextContent());
}
} catch (Exception ex) {
Logger.getLogger(TestXML05.class.getName()).log(Level.SEVERE, null,
ex);
}
}
And this is my XML example file:
<?xml version="1.0" encoding="utf-8"?>
<staff>
<person id="1" role="chief">
<name>
<firstName>Ivan</firstName>
<lastName>Popov</lastName>
</name>
<phone>
<phoneOne>0273090909</phoneOne>
<phoneTwo>0878123456</phoneTwo>
</phone>
<email>i.popov#fdiba.tu-sofia.bg</email>
<room>10202</room>
<title>Dr.Ing.</title>
</person>
<person id="2" role="dozent">
<name>
<firstName>Georgi</firstName>
<lastName>Ivanov</lastName>
</name>
<phone>
<phoneOne>029988115</phoneOne>
<phoneTwo>0888123333</phoneTwo>
</phone>
<email>g.ivanov#fdiba.tu-sofia.bg</email>
<room>10203</room>
<title>Dr.Ing.</title>
</person>
<person id="3" role="assistent">
<name>
<firstName>Petur</firstName>
<lastName>Kirilov</lastName>
</name>
<phone>
<phoneOne>028773455</phoneOne>
<phoneTwo>0898448576</phoneTwo>
</phone>
<email>p.kirilov#fdiba.tu-sofia.bg</email>
<room>10308</room>
<title>Ing.</title>
</person>
</staff>
Your xpath expression seems to be incorrect. You need to change the xpath expression to /staff/person/name/firstName[contains(text(),'Georgi')]/../... This selects person node corresponding to the person with the first name Georgi.
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
Document doc = factory.newDocumentBuilder().parse(
new File("src/resources/staff.xml"));
XPathFactory xFactory = XPathFactory.newInstance();
XPath xPath = xFactory.newXPath();
XPathExpression exp = xPath
.compile("/staff/person/name/firstName[contains(text(),'Georgi')]/../..");
NodeList nl = (NodeList) exp.evaluate(doc,
XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
if (node.hasAttributes()) {
Attr attr = (Attr) node.getAttributes().getNamedItem("role");
if (attr != null) {
String attribute= attr.getValue();
System.out.println("Person role : " + attribute);
}
}
System.out.println(node.getTextContent());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

How to read a generic XML file in java

I'm looking for ways to read a generic xml file
Here is an example of a normal xml file
<?xml version="1.0"?>
<students>
<student>
<name>John</name>
<grade>B</grade>
<age>12</age>
</student>
<student>
<name>Mary</name>
<grade>A</grade>
<age>11</age>
</student>
<student>
<name>Simon</name>
<grade>A</grade>
<age>18</age>
</student>
</students>
and here is example of a typical xml parser that would read that code and print it out
public class XMLParser {
public void getAllUserNames(String fileName) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(fileName);
if (file.exists()) {
Document doc = db.parse(file);
Element docEle = doc.getDocumentElement();
// Print root element of the document
System.out.println("Root element of the document: "
+ docEle.getNodeName());
NodeList studentList = docEle.getElementsByTagName("student");
// Print total student elements in document
System.out
.println("Total students: " + studentList.getLength());
if (studentList != null && studentList.getLength() > 0) {
for (int i = 0; i < studentList.getLength(); i++) {
Node node = studentList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out
.println("=====================");
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("name");
System.out.println("Name: "
+ nodeList.item(0).getChildNodes().item(0)
.getNodeValue());
nodeList = e.getElementsByTagName("grade");
System.out.println("Grade: "
+ nodeList.item(0).getChildNodes().item(0)
.getNodeValue());
nodeList = e.getElementsByTagName("age");
System.out.println("Age: "
+ nodeList.item(0).getChildNodes().item(0)
.getNodeValue());
}
}
} else {
System.exit(1);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
XMLParser parser = new XMLParser();
parser.getAllUserNames("c:\\test.xml");
}
}
This code needs lines like this
NodeList studentList = docEle.getElementsByTagName("student");
NodeList nodeList = e.getElementsByTagName("name");
In order to work correctly.
My questions comes from how would I make that generic. Is there any way where I could read that same XML file without having to get specific elements by tagNames and yet still print it out in a view able format.
In the above example you are using Dom parser. By using Jaxb Context unmarshaller you can convert the xml to java object, then you can achive your task.
You need to have a generic function to handle.
Generic function is as follows:
/* Prints the Node Value */
public void PrintNodeValue(Element element, String tagName, String msg)
{
NodeList nodeList = element.getElementsByTagName(tagName);
System.out.println(msg + nodeList.item(0).getChildNodes().item(0).getNodeValue());
}
Function is called as below:
PrintNodeValue(e, "name", "Name: ");
PrintNodeValue(e, "grade", "Grade: ");

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">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.

Categories