Java - Reading XML file - java

I am trying to read in some data from an XML file and having some trouble, the XML I have is as follows:
<xml version="1.0" encoding="UTF-8"?>
<EmailSettings>
<recipient>test#test.com</recipient>
<sender>test2#test.com</sender>
<subject>Sales Query</subject>
<description>email body message</description>
</EmailSettings>
I am trying to read these values as strings into my Java program, I have written this code so far:
private static Document getDocument (String filename){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(filename));
}
catch (Exception e){
System.out.println("Error reading configuration file:");
System.out.println(e.getMessage());
}
return null;
}
Document doc = getDocument(configFileName);
Element config = doc.getDocumentElement();
I am struggling with reading in the actual string values though.

One of the possible implementations:
File file = new File("userdata.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
String usr = document.getElementsByTagName("user").item(0).getTextContent();
String pwd = document.getElementsByTagName("password").item(0).getTextContent();
when used with the XML content:
<credentials>
<user>testusr</user>
<password>testpwd</password>
</credentials>
results in "testusr" and "testpwd" getting assigned to the usr and pwd references above.

Reading xml the easy way:
http://www.mkyong.com/java/jaxb-hello-world-example/
package com.mkyong.core;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
#XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
#XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
#XmlAttribute
public void setId(int id) {
this.id = id;
}
}
.
package com.mkyong.core;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class JAXBExample {
public static void main(String[] args) {
Customer customer = new Customer();
customer.setId(100);
customer.setName("mkyong");
customer.setAge(29);
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}

If using another library is an option, the following may be easier:
package for_so;
import java.io.File;
import rasmus_torkel.xml_basic.read.TagNode;
import rasmus_torkel.xml_basic.read.XmlReadOptions;
import rasmus_torkel.xml_basic.read.impl.XmlReader;
public class Q7704827_SimpleRead
{
public static void
main(String[] args)
{
String fileName = args[0];
TagNode emailNode = XmlReader.xmlFileToRoot(new File(fileName), "EmailSettings", XmlReadOptions.DEFAULT);
String recipient = emailNode.nextTextFieldE("recipient");
String sender = emailNode.nextTextFieldE("sender");
String subject = emailNode.nextTextFieldE("subject");
String description = emailNode.nextTextFieldE("description");
emailNode.verifyNoMoreChildren();
System.out.println("recipient = " + recipient);
System.out.println("sender = " + sender);
System.out.println("subject = " + subject);
System.out.println("desciption = " + description);
}
}
The library and its documentation are at rasmustorkel.com

Avoid hardcoding try making the code that is dynamic below is the code it will work for any xml I have used SAX Parser you can use dom,xpath it's upto you
I am storing all the tags name and values in the map after that it becomes easy to retrieve any values you want I hope this helps
SAMPLE XML:
<parent>
<child >
<child1> value 1 </child1>
<child2> value 2 </child2>
<child3> value 3 </child3>
</child>
<child >
<child4> value 4 </child4>
<child5> value 5</child5>
<child6> value 6 </child6>
</child>
</parent>
JAVA CODE:
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class saxParser {
static Map<String,String> tmpAtrb=null;
static Map<String,String> xmlVal= new LinkedHashMap<String, String>();
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, VerifyError {
/**
* We can pass the class name of the XML parser
* to the SAXParserFactory.newInstance().
*/
//SAXParserFactory saxDoc = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", null);
SAXParserFactory saxDoc = SAXParserFactory.newInstance();
SAXParser saxParser = saxDoc.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
String tmpElementName = null;
String tmpElementValue = null;
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tmpElementValue = "";
tmpElementName = qName;
tmpAtrb=new HashMap();
//System.out.println("Start Element :" + qName);
/**
* Store attributes in HashMap
*/
for (int i=0; i<attributes.getLength(); i++) {
String aname = attributes.getLocalName(i);
String value = attributes.getValue(i);
tmpAtrb.put(aname, value);
}
}
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(tmpElementName.equals(qName)){
System.out.println("Element Name :"+tmpElementName);
/**
* Retrive attributes from HashMap
*/ for (Map.Entry<String, String> entrySet : tmpAtrb.entrySet()) {
System.out.println("Attribute Name :"+ entrySet.getKey() + "Attribute Value :"+ entrySet.getValue());
}
System.out.println("Element Value :"+tmpElementValue);
xmlVal.put(tmpElementName, tmpElementValue);
System.out.println(xmlVal);
//Fetching The Values From The Map
String getKeyValues=xmlVal.get(tmpElementName);
System.out.println("XmlTag:"+tmpElementName+":::::"+"ValueFetchedFromTheMap:"+getKeyValues);
}
}
#Override
public void characters(char ch[], int start, int length) throws SAXException {
tmpElementValue = new String(ch, start, length) ;
}
};
/**
* Below two line used if we use SAX 2.0
* Then last line not needed.
*/
//saxParser.setContentHandler(handler);
//saxParser.parse(new InputSource("c:/file.xml"));
saxParser.parse(new File("D:/Test _ XML/file.xml"), handler);
}
}
OUTPUT:
Element Name :child1
Element Value : value 1
XmlTag:<child1>:::::ValueFetchedFromTheMap: value 1
Element Name :child2
Element Value : value 2
XmlTag:<child2>:::::ValueFetchedFromTheMap: value 2
Element Name :child3
Element Value : value 3
XmlTag:<child3>:::::ValueFetchedFromTheMap: value 3
Element Name :child4
Element Value : value 4
XmlTag:<child4>:::::ValueFetchedFromTheMap: value 4
Element Name :child5
Element Value : value 5
XmlTag:<child5>:::::ValueFetchedFromTheMap: value 5
Element Name :child6
Element Value : value 6
XmlTag:<child6>:::::ValueFetchedFromTheMap: value 6
Values Inside The Map:{child1= value 1 , child2= value 2 , child3= value 3 , child4= value 4 , child5= value 5, child6= value 6 }

Related

How to get all of elements from String(xml) with Parser<ObjectNode>

I get soap xml response and convert to String. String (xml) Example:
<Details>
<row>
<item>
<name>account</name>
<value>45687447</value>
</item>
<item>
<name>number</name>
<value>896541</value>
</item>
</row>
<row>
<item>
<name>account</name>
<value>2669874</value>
</item>
<item>
<name>number</name>
<value>063641</value>
</item>
</row>
</Details>
Now i parsing the String like this:
public ObjectNode ParseXml(String xml) {
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(xml);
Parser<ObjectNode> parser = parsing.obj("//Details")
.attribute("row", parsing.obj("//row")
.attribute("account", "//item[name/text() = 'account']/value")
.attribute("number", "//item[name/text() = 'number']/value")).build();
ObjectNode result = parser.apply(document);
return result;
}
But problem is the, i take only one row, like this:
{
"row": {
"account": "2669874",
"number": "063641",
}
}
If i have 10 rows, but i get only one row. How i can get all rows?
I'm parsing from .xml file which you can easily change and have log on this files too.
package java_testiranja;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* #author blac
*/
public class Java_testiranja {
/**
* #param xml
* #param args the command line arguments
*/
private List<Employee> myEmpls = new ArrayList<Employee>();
public static void main(String[] args) throws IOException, ParserConfigurationException {
Java_testiranja domParser = new Java_testiranja();
domParser.parseXmlFile();
}
private void parseXmlFile() throws IOException, ParserConfigurationException {
// get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
// parse using builder to get DOM representation of the XML file
Document dom = db.parse("employee.xml");
parseDocument(dom);
printData();
} catch (SAXException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void parseDocument(Document dom) {
// get the root element
Element docEle = dom.getDocumentElement();
// get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("item");
int stevilor = nl.getLength();
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
// get the employee element
Element el = (Element) nl.item(i);
// get the Employee object
Employee e = getEmployee(el);
// add it to list
myEmpls.add(e);
}
}
}
/**
* I take an employee element and read the values in, create an Employee object and return it
*/
private Employee getEmployee(Element empEl) {
// for each <employee> element get text or int values of
// name ,id, age and name
String name = getTextValue(empEl, "name");
int id = getIntValue(empEl, "value");
//int age = getIntValue(empEl, "Age");
//String type = empEl.getAttribute("type");
// Create a new Employee with the value read from the xml nodes
Employee e = new Employee(name, id);
return e;
}
private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
private int getIntValue(Element ele, String tagName) {
// in production application you would catch the exception
return Integer.parseInt(getTextValue(ele, tagName));
}
private void printData() {
Iterator<Employee> it = myEmpls.iterator();
while (it.hasNext()) {
System.out.println("{" + "\n" + "\"row\": {");
System.out.println(" "+it.next().toString());
System.out.println(" "+it.next().toString());
System.out.println(" }" + "\n" + "}");
}
}
}
And the Employee class is like:
package java_testiranja;
/**
*
* #author blac
*/
public class Employee {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String toString() {
return "\""+name+"\": " + "\""+id+"\""+",";
}
}
I tried your .xml file and get the result:
{
"row": {
"account": "45687447",
"number": "896541",
}
}
{
"row": {
"account": "2669874",
"number": "63641",
}
}
I just saved .xml file as Employee.xml and called it. You can simply save this results...
Regards,
Blaz

How could I get the total number inside a java xml tap?

For xml file view:
<?xml version="1.0"?>
<EXAMPLE DATE="20160830">
<SUB NUM="1">
<NAME>Peter</NAME>
</SUB>
<SUB NUM="2">
<NAME>Mary</NAME>
</SUB>
</EXAMPLE>
After I setup a NodeList for check the document,
I want it can be count the "NAME" Tap in each "SUB NUM="[x]""
For the code that I set for it:
NodeList nList= doc.getElementsByTagName("NUM"); // doc has been set correct and get successful
The nList.length will return "2" due to xml having 2 of the tap which is named as: "NUM", but I want to check each of the group only.
Is any Idea how could I get the length like:
SUB NUM [1] Found: [1] Length with tap name: [NAME]
SUB NUM [2] Found: [1] Length with tap name: [NAME]
This can be done as follows. Just the hashmap printing using a JAVA8 syntax. You should be able to iterate normally and print if you are not on 8.
import java.io.StringReader;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class NumCountHandler extends DefaultHandler {
private HashMap<String, Integer> countOfNum = new HashMap<String, Integer>();
#Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("SUB")) {
String attributeNum = attributes.getValue("NUM");
// System.out.println("Here" + qName +"" + );
if (countOfNum.containsKey(attributeNum)) {
Integer count = countOfNum.get(attributeNum);
countOfNum.put(attributeNum, new Integer(count.intValue() + 1));
} else {
countOfNum.put(attributeNum, new Integer(1));
}
}
}
public static void main(String[] args) {
try {
String xml = "<EXAMPLE DATE=\"20160830\"> <SUB NUM=\"1\"> <NAME>Peter</NAME> </SUB> <SUB NUM=\"2\"> <NAME>Mary</NAME> </SUB></EXAMPLE>";
InputSource is = new InputSource(new StringReader(xml));
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
NumCountHandler userhandler = new NumCountHandler();
saxParser.parse(is, userhandler);
userhandler.countOfNum
.forEach((k, v) -> System.out.println("SUB NUM [" + k + "]" + "Length with tap name :[" + v + "]"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Prints
SUB NUM [1]Length with tap name :[1]
SUB NUM [2]Length with tap name :[1]

how to extract values from below xml code using java?

how to extract values from below xml code using java?
Here I want to extract to,from,body,thread values using java.
Here total code is condider as string.
<message to="-105608156545#chat.facebook.com/Smack"
from="-105465454665906545#chat.facebook.com"
type="chat">
<body>sai</body>
<thread>NNLWF1</thread>
<active xmlns="http://jabber.org/protocol/chatstates" />
</message>
One possibility would be to use Java's in built XPath capabailities, for example...
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("Test.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xPath.compile("/message[#from]");
Node node = (Node)expression.evaluate(document, XPathConstants.NODE);
System.out.println("From: " + node.getAttributes().getNamedItem("from").getNodeValue());
expression = xPath.compile("/message/body");
node = (Node)expression.evaluate(document, XPathConstants.NODE);
System.out.println("Body: " + node.getTextContent());
expression = xPath.compile("/message/thread");
node = (Node)expression.evaluate(document, XPathConstants.NODE);
System.out.println("Thread: " + node.getTextContent());
} catch (ParserConfigurationException | SAXException | IOException | DOMException | XPathExpressionException exp) {
exp.printStackTrace();
}
Which outputs...
From: -105465454665906545#chat.facebook.com
Body: sai
Thread: NNLWF1
Take a look at:
Java API for XML Processing (JAXP)
XPath Tutorial
How To Parse XML File Using XPath In Java as an example
For more details
String xmlString="<message>....</message>";//above xml code
JAXBContext jc = JAXBContext.newInstance( message.class );
Unmarshaller u = jc.createUnmarshaller();
message o =(message) u.unmarshal( new StreamSource( new StringReader(xmlString ) ) );
System.out.println("------getTo-------"+o.getTo());
System.out.println("------getFrom-------"+o.getFrom());
System.out.println("------getBody-------"+o.getBody());
System.out.println("------getThread-------"+o.getThread());
And Bean class(message) code.
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class message {
public message(){}
private String to;
#XmlAttribute
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
#XmlAttribute
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
#XmlElement
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
#XmlElement
public String getThread() {
return thread;
}
public void setThread(String thread) {this.thread = thread;
}
private String from;
private String body;
private String thread;
public message(String to, String from, String body,String thread ){
super();
this.to = to;
this.from = from;
this.body = body;
this.thread = thread;
}
}
One way you can read your XML in java is:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Demo {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("yourFile.xml"));
NodeList nodeList = document.getElementsByTagName("message");
for(int x=0,size= nodeList.getLength(); x<size; x++) {
System.out.println(nodeList.item(x).getAttributes().getNamedItem("to").getNodeValue());
}
}
}
Hope it helps.
Here a complete example using a data projection library:
public class DataProjection {
public interface Message {
#XBRead("/message/#to")
String getTo();
#XBRead("/message/#from")
String getFrom();
#XBRead("/message/body")
String getBody();
#XBRead("/message/thread")
String getThread();
}
public static void main(String[] args) {
String xml="<message to=\"-105608156545#chat.facebook.com/Smack\" \n" +
"from=\"-105465454665906545#chat.facebook.com\" \n" +
"type=\"chat\">\n" +
"<body>sai</body>\n" +
"<thread>NNLWF1</thread>\n" +
"<active xmlns=\"http://jabber.org/protocol/chatstates\" />\n" +
"</message>";
Message message = new XBProjector().projectXMLString(xml, Message.class);
System.out.println(message.getFrom());
System.out.println(message.getTo());
System.out.println(message.getBody());
System.out.println(message.getThread());
}
This program prints out:
-105465454665906545#chat.facebook.com
-105608156545#chat.facebook.com/Smack
sai
NNLWF1

Parsing xml and making object java

Looking for someone to look over my simple code. I am rather new to what I am doing and know that I am probably just making a simple mistake somewhere.
I am parsing an xml file that is over http and trying to print out the text associated with the elements to the screen, and make an object that is populated by the text in those elements.
I can get all of the elements and associated text printed but the objects all have a null value in their fields. Let me know if I anything needs to be explained better.
Code is found below:
Object Class:
package com.entities;
public class StageOfLife
{
private String endDate;
private String offerData;
private String offerType;
private String redemption;
private String startDate;
private String termsConditions;
private String title;
private String merchantDescription;
private String merchantLogo;
private String merchantName;
public StageOfLife() {
}
public StageOfLife(String endDate, String offerData, String offerType,
String redemption, String startDate, String termsConditions,
String title, String merchantDescription, String merchantLogo,
String merchantName)
{
// Getters Setters HEre
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(endDate);
buffer.append("\n");
buffer.append(offerData);
buffer.append("\n");
buffer.append(offerType);
buffer.append("\n");
buffer.append(redemption);
buffer.append("\n");
buffer.append(startDate);
buffer.append("\n");
buffer.append(termsConditions);
buffer.append("\n");
buffer.append(title);
buffer.append("\n");
buffer.append(merchantDescription);
buffer.append("\n");
buffer.append(merchantLogo);
buffer.append("\n");
buffer.append(merchantName);
return buffer.toString();
}
}
And here is is the class with the methods and main:
package com.xmlparse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.entities.StageOfLife;
public class XmlParserStage
{
Document dom;
DocumentBuilder db;
List<StageOfLife> myStageList;
public XmlParserStage(){
//create a list to hold the StageOfLife objects
myStageList = new ArrayList<StageOfLife>();
}
private void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//Using factory get an instance of document builder
db = dbf.newDocumentBuilder();
//parse to get DOM representation of the XML file
dom = db.parse("http:/url/goes/here");
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch(SAXException se) {
se.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
private void parseDocument() {
//get the root element
Element docEle = dom.getDocumentElement();
//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("Offer");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength(); i++) {
//get the elements
Element solEl = (Element)nl.item(i);
//get the StageOfLife object
StageOfLife sol = getStageOfLife(solEl);
//add it to list
myStageList.add(sol);
}
}
}
/*
take an <offer> element and read the values in, create
an StageOfLife object and return it
*/
private StageOfLife getStageOfLife(Element solEl) {
/*
for each <offer> element get the values
*/
String endDate = getTextValue(solEl,"EndDate");
String offerData = getTextValue(solEl,"OfferData");
String offerType = getTextValue(solEl,"OfferType");
String redemption = getTextValue(solEl,"Redemption");
String startDate = getTextValue(solEl,"StartDate");
String termsConditions = getTextValue(solEl,"TermsConditions");
String title = getTextValue(solEl,"Title");
String merchantDescription = getTextValue(solEl,"MerchantDescription");
String merchantLogo = getTextValue(solEl,"MerchantLogo");
String merchantName = getTextValue(solEl,"MerchantName");
//Create a new StageOfLife object with the value read from the xml nodes
StageOfLife sol = new StageOfLife(endDate, offerData, offerType,
redemption, startDate, termsConditions,
title, merchantDescription, merchantLogo,
merchantName);
return sol;
}
/*
take a xml element and the tag name, look for the tag and get
the text content
*/
private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0) {
Element el = (Element)nl.item(0);
textVal = el.getFirstChild().getNodeValue();
System.out.print(el + ":" + textVal);
System.out.println();
}
return textVal;
}
/*
Calls getTextValue and returns a int value
*/
private int getIntValue(Element ele, String tagName) {
return Integer.parseInt(getTextValue(ele,tagName));
}
private void printData(){
System.out.println("Number of Offers: '" + myStageList.size() + "'.");
Iterator it = myStageList.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}
public void run() {
//parse the xml file and get the dom object
parseXmlFile();
//get each stageoflife element and create a StageOfLife object
parseDocument();
//Iterate through the list and print the data
printData();
}
public static void main(String [] args) throws ClientProtocolException, IOException {
XmlParserStage xmlParser = new XmlParserStage();
xmlParser.httpClient();
xmlParser.run();
}
}
your constructor is not doing anything!
public StageOfLife(String endDate, String offerData, String offerType,
String redemption, String startDate, String termsConditions,
String title, String merchantDescription, String merchantLogo,
String merchantName)
{
// set the data
this.endDate = endDate;
...
}
But much better is to use Java XML Binding jaxb. it automatically maps java classes to xml
Take a look at Jaxb library. It can do all the heavy lifting for you.
JAXB Homepage
Vogella Tutorial
Mkyong Tutorial
Set the values you are passing to the StageOfLife constructor to the variables.
Try this
public class Tester {
String getString() throws IOException, ParserConfigurationException, SAXException {
InputStream inputStream = //your stream from http
String sa = "";
int cc;
while((cc = inputStream.read()) != -1) {
sa += (char) cc;
}
ByteArrayInputStream sr = new ByteArrayInputStream(sa.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(sr);
Node node=doc.getDocumentElement().getFirstChild();
String data=node.getNodeName();
return data;
}
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
Tester t = new Tester();
System.out.println(t.getString());
}

Get line number from xml node - java

I have parsed an XML file and have gotten a Node that I am interested in. How can I now find the line number in the source XML file where this node occurs?
EDIT:
Currently I am using the SAXParser to parse my XML. However I will be happy with a solution using any parser.
Along with the Node, I also have the XPath expression for the node.
I need to get the line number because I am displaying the XML file in a textbox, and need to highlight the line where the node occured. Assume that the XML file is nicely formatted with sufficient line breaks.
I have got this working by following this example:
http://eyalsch.wordpress.com/2010/11/30/xml-dom-2/
This solution follows the method suggested by Michael Kay. Here is how you use it:
// XmlTest.java
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class XmlTest {
public static void main(final String[] args) throws Exception {
String xmlString = "<foo>\n"
+ " <bar>\n"
+ " <moo>Hello World!</moo>\n"
+ " </bar>\n"
+ "</foo>";
InputStream is = new ByteArrayInputStream(xmlString.getBytes());
Document doc = PositionalXMLReader.readXML(is);
is.close();
Node node = doc.getElementsByTagName("moo").item(0);
System.out.println("Line number: " + node.getUserData("lineNumber"));
}
}
If you run this program, it will out put: "Line number: 3"
PositionalXMLReader is a slightly modified version of the example linked above.
// PositionalXMLReader.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class PositionalXMLReader {
final static String LINE_NUMBER_KEY_NAME = "lineNumber";
public static Document readXML(final InputStream is) throws IOException, SAXException {
final Document doc;
SAXParser parser;
try {
final SAXParserFactory factory = SAXParserFactory.newInstance();
parser = factory.newSAXParser();
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
} catch (final ParserConfigurationException e) {
throw new RuntimeException("Can't create SAX parser / DOM builder.", e);
}
final Stack<Element> elementStack = new Stack<Element>();
final StringBuilder textBuffer = new StringBuilder();
final DefaultHandler handler = new DefaultHandler() {
private Locator locator;
#Override
public void setDocumentLocator(final Locator locator) {
this.locator = locator; // Save the locator, so that it can be used later for line tracking when traversing nodes.
}
#Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
throws SAXException {
addTextIfNeeded();
final Element el = doc.createElement(qName);
for (int i = 0; i < attributes.getLength(); i++) {
el.setAttribute(attributes.getQName(i), attributes.getValue(i));
}
el.setUserData(LINE_NUMBER_KEY_NAME, String.valueOf(this.locator.getLineNumber()), null);
elementStack.push(el);
}
#Override
public void endElement(final String uri, final String localName, final String qName) {
addTextIfNeeded();
final Element closedEl = elementStack.pop();
if (elementStack.isEmpty()) { // Is this the root element?
doc.appendChild(closedEl);
} else {
final Element parentEl = elementStack.peek();
parentEl.appendChild(closedEl);
}
}
#Override
public void characters(final char ch[], final int start, final int length) throws SAXException {
textBuffer.append(ch, start, length);
}
// Outputs text accumulated under the current node
private void addTextIfNeeded() {
if (textBuffer.length() > 0) {
final Element el = elementStack.peek();
final Node textNode = doc.createTextNode(textBuffer.toString());
el.appendChild(textNode);
textBuffer.delete(0, textBuffer.length());
}
}
};
parser.parse(is, handler);
return doc;
}
}
If you are using a SAX parser then the line number of an event can be obtained using the Locator object, which is notified to the ContentHandler via the setDocumentLocator() callback. This is called at the start of parsing, and you need to save the Locator; then after any event (such as startElement()), you can call methods such as getLineNumber() to obtain the current position in the source file. (After startElement(), the callback is defined to give you the line number on which the ">" of the start tag appears.)
Note that according to the spec (of Locator.getLineNumber()) the method returns the line number where the SAX-event ends!
In the case of "startElement()" this means:
Here the line number for Element is 1:
<Element></Element>
Here the line number for Element is 3:
<Element
attribute1="X"
attribute2="Y">
</Element>
priomsrb's answer is great and works. For my usecase i need to integrate it to an existing framework where e.g. the encoding is also covered. Therefore the following refactoring was applied to have a separate LineNumberHandler class.
Then the code will also work with a Sax InputSource where the encoding can be modified like this:
// read in the xml document
org.xml.sax.InputSource is=new org.xml.sax.InputSource();
is.setByteStream(instream);
if (encoding!=null) {
is.setEncoding(encoding);
if (Debug.CORE)
Debug.log("setting XML encoding to - "+is.getEncoding());
}
Separate LineNumberHandler
/**
* LineNumber Handler
* #author wf
*
*/
public static class LineNumberHandler extends DefaultHandler {
final Stack<Element> elementStack = new Stack<Element>();
final StringBuilder textBuffer = new StringBuilder();
private Locator locator;
private Document doc;
/**
* create a line number Handler for the given document
* #param doc
*/
public LineNumberHandler(Document doc) {
this.doc=doc;
}
#Override
public void setDocumentLocator(final Locator locator) {
this.locator = locator; // Save the locator, so that it can be used
// later for line tracking when traversing
// nodes.
}
#Override
public void startElement(final String uri, final String localName,
final String qName, final Attributes attributes) throws SAXException {
addTextIfNeeded();
final Element el = doc.createElement(qName);
for (int i = 0; i < attributes.getLength(); i++) {
el.setAttribute(attributes.getQName(i), attributes.getValue(i));
}
el.setUserData(LINE_NUMBER_KEY_NAME,
String.valueOf(this.locator.getLineNumber()), null);
elementStack.push(el);
}
#Override
public void endElement(final String uri, final String localName,
final String qName) {
addTextIfNeeded();
final Element closedEl = elementStack.pop();
if (elementStack.isEmpty()) { // Is this the root element?
doc.appendChild(closedEl);
} else {
final Element parentEl = elementStack.peek();
parentEl.appendChild(closedEl);
}
}
#Override
public void characters(final char ch[], final int start, final int length)
throws SAXException {
textBuffer.append(ch, start, length);
}
// Outputs text accumulated under the current node
private void addTextIfNeeded() {
if (textBuffer.length() > 0) {
final Element el = elementStack.peek();
final Node textNode = doc.createTextNode(textBuffer.toString());
el.appendChild(textNode);
textBuffer.delete(0, textBuffer.length());
}
}
};
PositionalXMLReader
public class PositionalXMLReader {
final static String LINE_NUMBER_KEY_NAME = "lineNumber";
/**
* read a document from the given input strem
*
* #param is
* - the input stream
* #return - the Document
* #throws IOException
* #throws SAXException
*/
public static Document readXML(final InputStream is)
throws IOException, SAXException {
final Document doc;
SAXParser parser;
try {
final SAXParserFactory factory = SAXParserFactory.newInstance();
parser = factory.newSAXParser();
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
} catch (final ParserConfigurationException e) {
throw new RuntimeException("Can't create SAX parser / DOM builder.", e);
}
LineNumberHandler handler = new LineNumberHandler(doc);
parser.parse(is, handler);
return doc;
}
}
JUnit Testcase
package com.bitplan.common.impl;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.bitplan.bobase.PositionalXMLReader;
public class TestXMLWithLineNumbers {
/**
* get an Example XML Stream
* #return the example stream
*/
public InputStream getExampleXMLStream() {
String xmlString = "<foo>\n" + " <bar>\n"
+ " <moo>Hello World!</moo>\n" + " </bar>\n" + "</foo>";
InputStream is = new ByteArrayInputStream(xmlString.getBytes());
return is;
}
#Test
public void testXMLWithLineNumbers() throws Exception {
InputStream is = this.getExampleXMLStream();
Document doc = PositionalXMLReader.readXML(is);
is.close();
Node node = doc.getElementsByTagName("moo").item(0);
assertEquals("3", node.getUserData("lineNumber"));
}
}

Categories

Resources