Parsing xml data in java - java

i have one requirement to get the data from the xml.
String res;
the data will be in the string res as follows.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<id>QZhx_w1eEJ</id>
<first-name>pratap</first-name>
<last-name>murukutla</last-name>
</person>
i have to get the id and the first-name and last-name from this data and has to be stored in the variables id,first-name,last-name
how to access the xml to get those details.

You could use JAXB (JSR-222) and do the following. An implementation is included in Java SE 6.
Demo
package forum10520757;
import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader xml = new StringReader("<person><id>QZhx_w1eEJ</id><first-name>pratap</first-name><last-name>murukutla</last-name></person>");
Person person = (Person) unmarshaller.unmarshal(xml);
System.out.println(person.id);
System.out.println(person.firstName);
System.out.println(person.lastName);
}
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
static class Person {
String id;
#XmlElement(name="first-name")
String firstName;
#XmlElement(name="last-name")
String lastName;
}
}
Output
QZhx_w1eEJ
pratap
murukutla

You can start with:
ByteArrayInputStream inputStream =
new ByteArrayInputStream(response.getBody().getBytes("UTF-8"));
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(new InputSource(inputStream));
You can see an example in http://www.java2s.com/Code/Java/XML/XMLDocumentinformationbyDOM.htm

Use a SAX or DOM parser that's built into Java. Parse the String into a DOM tree, walk the tree, get your values.
http://java.sun.com/xml/tutorial_intro.html

Related

Can't unmarshal marshalled document when default namespace used

I set up a repo which shows my problem: https://github.com/Waxolunist/stackoverflow.34392476
I try to unmarshal a simple xml document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<Export xmlns="urn:adcubum:Syrius">
<ExportInhalt/>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</Export>
</for:document>
This is the document I get from following code:
Document document = new Document();
Export export = new Export();
ExportKopf exportKopf = new ExportKopf();
exportKopf.setQuelle("lokal");
export.setExportKopf(exportKopf);
ExportInhalt exportInhalt = new ExportInhalt();
export.setExportInhalt(exportInhalt);
export.setSchemaVersion("bec811a9807a8c8da403d70b9b5e22ad");
document.setExport(export);
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(document, System.out);
Document looks as follows:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "document", namespace = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx")
public class Document {
#XmlElement(name = "Export", namespace = "urn:adcubum:Syrius")
private vo.dom.common_service.modul_bl.syrius.Export export;
}
package-info.java
#XmlSchema(
namespace = "urn:adcubum:Syrius",
xmlns = {
#XmlNs(prefix = "for", namespaceURI = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx"),
#XmlNs(prefix = "", namespaceURI = "urn:adcubum:Syrius")
},
elementFormDefault = XmlNsForm.UNQUALIFIED)
When I try to unmarshal it, I don't get the data mapped:
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InputStream is = this.getClass().getResourceAsStream("/requests/document_simple3.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlsr = factory.createXMLStreamReader(is);
Document document = unmarshaller.unmarshal(xmlsr, Document.class).getValue();
ExportKopf and ExportInhalt are returning null.
Instead following xml works. The only difference is the namespace prefix:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns3:Export xmlns:ns3="urn:adcubum:Syrius">
<ExportInhalt/>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</ns3:Export>
</for:document>
I am using eclipselink moxy.
What do I have to change, so that unmarshaling the marshaled document works.
I think it's always a good idea to see the actual XSD schema of your mapping whenever something strange like this is happening in JAXB. You could easily do that with the following code.
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
jaxbContext.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult streamResult = new StreamResult(new PrintWriter(System.err) {
#Override
public void close() {
}
});
streamResult.setSystemId(suggestedFileName);
return streamResult;
}
});
That will print the schema(s) that should reflect your JAXB model (you can use another writer to write them to a file). The XSD files are usually very revealing about these kind of issues. I think the problem in your case is the #XmlSchema mapping. You should try to use elementFormDefault = XmlNsForm.QUALIFIED instead. It's always a good idea to use QUALIFIED whenever you're working with multiple namespaces in your mapping.
EDIT: while the main problem with your JAXB mapping was the wrong and/or missing value for the elementFormDefault there were other things that had to be fixed for the code in your repo to work.
the Export element in the Document was missing a namespace declaration (from your example, the Document and Export elements are parts of different namespaces)
missing elementFormDefault = XmlNsForm.QUALIFIED from the export package
wrong namespace value for your main package #XmlSchema annotation (was urn:stackoverflow:exportnamespace instead of urn:stackoverflow:documentnamespace in which the Document element should be)
wrong import for jdk_jaxb/UnmarshallerTest.java - it was importing the model.eclipselink.Document instead of model.sun.Document
EDIT: Here's the updated code https://github.com/MojoJojo/stackoverflow.34392476
Okay, here's the working version with all moxy_jaxb test passing. Because you said you are using moxy, I left out the changes for model.sun.* packages. If you understand the concept below, you should be able to fix it easily on your own.
First, I cleaned up namespace declarations in your mode.* packages. Most of the times, the declarations and bindings inside package-info.java suffice. Declaring them over and over again on package, entities, fields will add to complexity and unwanted behavior. Check out this link for details. There is no need to re-declare them on the individual Models/Entities themselves, unless there is a strong reason to do otherwise. Next, the test xml itself was a little broken. Fixed the test xml with proper prefixes wherever necessary:
First, model.eclipselink.Document.java
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "document")
public class Document {
#XmlElement(name = "Export", namespace="urn:adcubum:Syrius")
private Export export;
public Export getExport() {
return export;
}
public void setExport(Export export) {
this.export = export;
}
}
model.eclipselink.package-info.java:
#XmlSchema(namespace = "http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx",
elementFormDefault = XmlNsForm.QUALIFIED)
package model.eclipselink;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Similar refactoring on model.eclipselink.export.packageinfo.java:
#XmlSchema(namespace = "urn:adcubum:Syrius",
elementFormDefault = XmlNsForm.QUALIFIED)
package model.eclipselink.export;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.Xml
And on Export.java:
package model.eclipselink.export;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlElementNillable;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Export")
#XmlType(name = "Export", propOrder = {
"exportInhalt",
"exportKopf",
"schemaVersion"
})
public class Export {
#XmlElement(name = "ExportKopf", required = true)
private ExportKopf exportKopf;
#XmlElement(name = "ExportInhalt", required = true)
private ExportInhalt exportInhalt;
#XmlElement(name = "SchemaVersion", required = true)
private String schemaVersion;
public ExportKopf getExportKopf() {
return exportKopf;
}
public void setExportKopf(ExportKopf exportKopf) {
this.exportKopf = exportKopf;
}
public ExportInhalt getExportInhalt() {
return exportInhalt;
}
public void setExportInhalt(ExportInhalt exportInhalt) {
this.exportInhalt = exportInhalt;
}
public String getSchemaVersion() {
return schemaVersion;
}
public void setSchemaVersion(String schemaVersion) {
this.schemaVersion = schemaVersion;
}
}
And the few tweaks to your xml files for prefixes.Here's document_prefix.xml
<?xml version="1.0" encoding="UTF-8"?>
<for:document xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns1:Export xmlns:ns1="urn:adcubum:Syrius">
<ns1:ExportKopf>
<ns1:Quelle>lokal</ns1:Quelle>
</ns1:ExportKopf>
<ns1:ExportInhalt/>
<ns1:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns1:SchemaVersion>
</ns1:Export>
</for:document>
document.xml:
<?xml version="1.0" encoding="UTF-8"?>
<for:document
xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx" xmlns="urn:adcubum:Syrius">
<Export>
<ExportKopf>
<Quelle>lokal</Quelle>
</ExportKopf>
<ExportInhalt />
<SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</SchemaVersion>
</Export>
</for:document>
and document_realnamespace.xml (Not sure what the purpose of this file is):
<?xml version="1.0" encoding="UTF-8"?>
<for:document xmlns:ns1="urn:adcubum:Syrius" xmlns:for="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrenderer/forwktbx">
<ns1:Export>
<ns1:ExportKopf>
<ns1:Quelle>lokal</ns1:Quelle>
</ns1:ExportKopf>
<ns1:ExportInhalt/>
<ns1:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns1:SchemaVersion>
</ns1:Export>
</for:document>
And you run mvn clean test:
Running moxy_jaxb.MarshallerTest
Context class: class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://www.adcubum.com/wsdl/global/callout/syrius/modul_bl/doc/service/documentrend
erer/forwktbx" xmlns:ns0="urn:adcubum:Syrius">
<ns0:Export>
<ns0:ExportInhalt/>
<ns0:ExportKopf>
<ns0:Quelle>lokal</ns0:Quelle>
</ns0:ExportKopf>
<ns0:SchemaVersion>bec811a9807a8c8da403d70b9b5e22ad</ns0:SchemaVersion>
</ns0:Export>
</document>
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
Running moxy_jaxb.UnmarshallerTest
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Context class: class org.eclipse.persistence.jaxb.JAXBContext
lokal
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec

java xml annotation get field with namespace, <aaa:bbb>value</aaa:bbb>

I'm working on a project that has no schema and I have to parsing the xml response manually.
My problem is i can't get some value using the xml annotation.
For example , the xml is like:
<?xml version='1.0' encoding='UTF-8' ?>
<autnresponse>
<action>QUERY</action>
<response>SUCCESS</response>
<responsedata>
<autn:numhits>7</autn:numhits>
</responsedata>
</autnresponse>
And the java class is :
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "autnresponse")
public class AutonomyResponse {
private String action;
private String response;
private ResponseData responsedata;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "responsedata")
public class ResponseData {
#XmlElement(name = "numhits",namespace = "autn")
private String numhits;
#XmlElement(name = "totalhits")
private String totalhits;
}
I can get the action and the response, but can't get the numhits in the responsedata,
Can anyone tell me how to handle the <autn:numhits> using annotation?
Too much Thanks !!!
Another issue is : I have multi <autn:numhits> in the responsedata....how can i get all the value in the Java code.
--> I solve this multi same tags, just set List and the annotation will automatically generate the list
The fact is autn - is only prefix, not namespace. For correct processing of the XML document, namespace must be declared.
Right namespace declaration:
<?xml version='1.0' encoding='UTF-8' ?>
<autnresponse xmlns:autn="http://namespace.here">
<action>QUERY</action>
<response>SUCCESS</response>
<responsedata>
<autn:numhits>7</autn:numhits>
</responsedata>
</autnresponse>
You also need to change the annotation:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "responsedata")
public class ResponseData {
#XmlElement(name = "numhits",namespace = "http://namespace.here")
private String numhits;
#XmlElement(name = "totalhits")
private String totalhits;
}
And finnaly advice for you. If you have a xsd scheme for this xml document, use the XJC utilit for java code generation.
http://docs.oracle.com/javaee/5/tutorial/doc/bnbah.html
JAXB and other XML processors that are capable of processing XML Schema are going to treat everything before a : as a namespace prefix. If the colon is then you can do the following.
Java Model
You need to specify that your element name contains the : character.
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class ResponseData {
#XmlElement(name = "autn:numhits")
private String numhits;
private String totalhits;
}
Demo
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.XMLReader;
public class Demo {
public static void main(String[] args) throws Exception {
// Create a SAXParser that is not namespace aware
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// Create the JAXBContext
JAXBContext jc = JAXBContext.newInstance(AutonomyResponse.class);
// Instead of Unmarshaller we will use an UnmarshallerHandler
Unmarshaller unmarshaller = jc.createUnmarshaller();
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
// Do a SAX parse with the UnmarshallerHanlder as the ContentHandler
xr.setContentHandler(unmarshallerHandler);
xr.parse("src/forum20062536/input.xml");
// Get the result of the unmarshal
AutonomyResponse autonomyResponse = (AutonomyResponse) unmarshallerHandler.getResult();
// Marshal the object back to XML
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(autonomyResponse, System.out);
}
}

JAXB Unmarshalling: List of objects

I have
#XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "content")
#XmlType(name = "course")
public class Course implements Resource
...
#XmlElementWrapper(name="subcourses")
#XmlElement(name="course")
List<Xlink> subcourses; //!?
and Xlink class, which works fine in inline variable.
public class Xlink
{
private String href;
private String value;
#XmlAttribute(namespace = "http://www.w3.org/1999/xlink")
public String getHref()
{
return href;
}
public void setHref(String href)
{
this.href = href;
}
#XmlValue
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
}
for an XML input of
<atom:content atom:type="xml" xsi:type="course">
...
<subcourses>
<course xlink:href="course1">Some course</course>
<course xlink:href="course2">other course</course>
And subcourses refuses to be unmarshalled (without any exception being thrown).
Note: MOXy is unfortunately not an option.
Edit: new marshalled object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:content xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.w3.org/2005/Atom">
<code>SOME CODE</code>
<name>name</name>
<subcourses>
<course ns2:href="Some href">some value</course>
<course ns2:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</ns3:content>
Edit2: after some experimentation with unmarshalling and marshalling a test object I have discovered that I need to define xmlns namespace in the header of the content to match xlink:href= like xmlns:xlink="http://www.w3.org/1999/xlink" the problem is that I am getting the Course element from inside a wrapper class that is parsed out by resteasy. Thus the resulting class does not carry over the namespace information.
I somehow need to force JAXB to understand that xmlns:xlink="http://www.w3.org/1999/xlink" applies to the course elements, but after an hour of google I am at a loss.
Edit3: I am getting my object from
https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Entry.java
which is used on the server counterpart. Which in turn is a part of
https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Feed.java
Relevant parts of my unmarshalling code are:
Feed f = r.readEntity(Feed.class);
out.addAll(unmarshaller.Unmarshal(f.getEntries(), clazz));
where r is a javax.ws.rs.core.Response. And the unmarshaller
public List<T> Unmarshal(List<Entry> entries, Class clazz)
{
List<T> out = new ArrayList<T>();
T instance;
for (Entry e : entries)
{
try
{
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarsh = context.createUnmarshaller();
instance = (T) unmarsh.unmarshal((Node) e.getContent());
Since this is my first tangle with this technology, it is entirely possible that this code is 'wtf'.
When you annotate the field (instance variable) be sure to put #XmlAccessorType(XmlAccessType.FIELD) on your class.
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "content")
#XmlAccessorType(XmlAccessType.FIELD)
public class Course implements Resource {
#XmlElementWrapper(name = "subcourses")
#XmlElement(name = "course")
List<Xlink> subcourses;
}
Then make sure your XML input is correctly namespace qualified. Your input document should look something like the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:content xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
<code>SOME CODE</code>
<name>name</name>
<subcourses>
<course xlink:href="Some href">some value</course>
<course xlink:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</atom:content>
With my updated Course class, your Xlink class and a properly namespace qualified XML document the following demo code worked perfectly for me.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Course.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17766166/input.xml");
Course course = (Course) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(course, System.out);
}
}
UPDATE #1
Edit2: after some experimentation with unmarshalling and marshalling a
test object I have discovered that I need to define xmlns namespace in
the header of the content to match xlink:href= like
xmlns:xlink="http://www.w3.org/1999/xlink" the problem is that I am
getting the Course element from inside a wrapper class that is parsed
out by resteasy. Thus the resulting class does not carry over the
namespace information.
The best place to fix your issue is where you extract the fragment you wish to unmarhal. Below is a strategy you can use with StAX.
input.xml
Below is a sample XML document where the namespace information is defined above the fragment you wish to unmarshal.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
<bar>
<atom:content>
<code>SOME CODE</code>
<name>name</name>
<subcourses>
<course xlink:href="Some href">some value</course>
<course xlink:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</atom:content>
</bar>
</foo>
Demo
Below we will use a StAX XMLStreamReader to navigate to the target fragment. We will have our JAXB implementation unmarshal this fragment. This way all the namespace information is preserved.
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Course.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource source = new StreamSource("src/forum17766166/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(source);
while(xsr.hasNext()) {
if(xsr.isStartElement() && "content".equals(xsr.getLocalName())) {
break;
}
xsr.next();
}
Unmarshaller unmarshaller = jc.createUnmarshaller();
Course course = (Course) unmarshaller.unmarshal(xsr);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(course, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:content xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.w3.org/2005/Atom">
<subcourses>
<course ns2:href="Some href">some value</course>
<course ns2:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</ns3:content>
UPDATE #2
If you can't produce a better XML fragment as described in UPDATE #1, below is how you could fix the XML fragment you currently have.
NamespaceFilter
You could use a SAX XMLFilter to fix up your XML document.
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class NamespaceFilter extends XMLFilterImpl {
private static final String ATOM_URI = "http://www.w3.org/2005/Atom";
private static final String XLINK_URI = "http://www.w3.org/1999/xlink";
#Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if("atom:content".equals(qName)) {
super.startElement(ATOM_URI, "content", qName, atts);
} else if("course".equals(qName)) {
AttributesImpl modifiedAtts = new AttributesImpl();
modifiedAtts.addAttribute(XLINK_URI, "href", "xlink:href", null, atts.getValue(0));
super.startElement(uri, localName, qName, modifiedAtts);
} else {
super.startElement(uri, localName, qName, atts);
}
}
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("atom:content".equals(qName)) {
super.endElement(ATOM_URI, "content", qName);
} else {
super.endElement(uri, localName, qName);
}
}
}
Demo
Below is how you can leverage the XmlFilter with JAXB:
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Course.class);
// Create the XMLFilter
XMLFilter filter = new NamespaceFilter();
// Set the parent XMLReader on the XMLFilter
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
filter.setParent(xr);
// Set UnmarshallerHandler as ContentHandler on XMLFilter
Unmarshaller unmarshaller = jc.createUnmarshaller();
UnmarshallerHandler unmarshallerHandler = unmarshaller
.getUnmarshallerHandler();
filter.setContentHandler(unmarshallerHandler);
// Parse the XML
InputSource xml = new InputSource("src/forum17766166/input.xml");
filter.parse(xml);
Course course = (Course) unmarshallerHandler.getResult();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(course, System.out);
}
}
For More Information
http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html
UPDATE #3
Below is a simplified version of your example code where everything works. Maybe there is something different in your code it will help you find.
Entry
import javax.xml.bind.annotation.*;
#XmlRootElement(namespace="http://www.w3.org/2005/Atom")
#XmlAccessorType(XmlAccessType.FIELD)
public class Entry<T> {
#XmlElement(namespace = "http://www.w3.org/2005/Atom")
#XmlSchemaType(name = "atomInlineOtherContent")
private T content;
public T getContent() {
return content;
}
}
input.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:entry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
<atom:content>
<code>SOME CODE</code>
<name>name</name>
<subcourses>
<course xlink:href="Some href">some value</course>
<course xlink:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</atom:content>
</atom:entry>
Demo
import java.io.File;
import javax.xml.bind.*;
import org.w3c.dom.Node;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Entry.class, Course.class);
// Unmarshal Entry
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17766166/input.xml");
Entry entry = (Entry) unmarshaller.unmarshal(xml);
// Unmarshal Course
Node contentNode = (Node) entry.getContent();
Course course = (Course) unmarshaller.unmarshal(contentNode);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(course, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<ns0:content xmlns:ns1="http://www.w3.org/1999/xlink" xmlns:ns0="http://www.w3.org/2005/Atom">
<subcourses>
<course ns1:href="Some href">some value</course>
<course ns1:href="sdsdg">sdfhdfhdhdh</course>
</subcourses>
</ns0:content>

Unmarshalling an XML using Xpath expression and jaxb

I am new to JAXB and would like to know if there is a way by which I can unmarshall an XML to my response object but using xpath expressions. The issue is that I am calling a third party webservice and the response which I receive has a lot of details. I do not wish to map all the details in the XML to my response object. I just wish to map few details from the xml using which I can get using specific XPath expressions and map those to my response object. Is there an annotation which can help me achieve this?
For example consider the following response
<root>
<record>
<id>1</id>
<name>Ian</name>
<AddressDetails>
<street> M G Road </street>
</AddressDetails>
</record>
</root>
I am only intrested in retrieving the street name so I want to use xpath expression to get value of street using 'root/record/AddressDetails/street' and map it to my response object
public class Response{
// How do i map this in jaxb, I do not wish to map record,id or name elements
String street;
//getter and setters
....
}
Thanks
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JST-222) expert group.
You could use MOXy's #XmlPath extension for this use case.
Response
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="root")
#XmlAccessorType(XmlAccessType.FIELD)
public class Response{
#XmlPath("record/AddressDetails/street/text()")
String street;
//getter and setters
}
jaxb.properties
To use MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17141154/input.xml");
Response response = (Response) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(response, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<record>
<AddressDetails>
<street> M G Road </street>
</AddressDetails>
</record>
</root>
For More Information
http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
If all you want is the street name, just use an XPath expression to get it as a string, and forget about JAXB - the complex JAXB machinery is not adding any value.
import javax.xml.xpath.*;
import org.xml.sax.InputSource;
public class XPathDemo {
public static void main(String[] args) throws Exception {
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
InputSource xml = new InputSource("src/forum17141154/input.xml");
String result = (String) xpath.evaluate("/root/record/AddressDetails/street", xml, XPathConstants.STRING);
System.out.println(result);
}
}
If you want to map to the middle of an XML document you could do the following:
Demo Code
You could use a StAX parser to advance to the part of the document you wish to unmarshal, and then unmarshal the XMLStreamReader from there,.
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("src/forum17141154/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
while(!(xsr.isStartElement() && xsr.getLocalName().equals("AddressDetails"))) {
xsr.next();
}
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Response> response = unmarshaller.unmarshal(xsr, Response.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(response, System.out);
}
}
Response
The mappings on the domain object are made relative to the fragment of the XML document you are mapping to.
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class Response{
String street;
//getter and setters
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<AddressDetails>
<street> M G Road </street>
</AddressDetails>
For More Information
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
#Xpath of Moxy works like a charm. But I got only null values in the beginning. Seems that it works a bit different with java 11 and above:
For me this post was helpful: #XmlPath has no impact during the JAXB Marshalling .
It finally worked with using these dependencies:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.0.1</version>
</dependency>
This in de jaxb.properties file (notice 'jakarta', this is different for older versions of java: jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Put this jaxb.properties file in the exact same location as the class files
In my pom.xml also the underneath, to copy de jaxb.properties file to the exact same location. (check in you target folder if it worked, the properties file should be in the exact same folder.
org.apache.maven.plugins
maven-compiler-plugin
src/main/java
**/*.java
Check in your imports that for jakarta is used for the Unmarshaller etc.
So your import statements should be like this:
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;
Good luck!

How to get a particular element through JAXB xml parsing?

I have used JAXB to parse an XML.How to get a particular element(ie a child node) through JAXB xml parsing without parsing that element as node.
<?xml version="1.0" encoding="UTF-8"?>
<Header>
<From>
<Credential
domain="NetworkId"><Identity>ANXXXNNN</Identity>
</Credential>
</From>
<To>
<Credential
domain="NetworkId"><Identity>ANNNXXXXXT</Identity>
</Credential>
</To>
<To>
<Credential
domain="NetworkId"><Identity>BNNXXXT</Identity>
</Credential>
</To>
</Header>
I have done unmarshalling like this,It works fine.For performance,I dont want the elements as node.Is there anyother way to do?
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
doc = db.parse(file);
NodeList node = (NodeList)doc.getElementsByTagName("TO");
JAXBElement<ToType> element = jaxbUnmarshaller.unmarshal(node.item(0),ToType.class);
Object model is like
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "ToType", propOrder = {
"credential"
})
public class ToType {
#XmlElement(name = "Credential", required = true)
protected CredentialType credential;
public CredentialType getCredential() {
return credential;
}
public void setCredential(CredentialType value) {
this.credential = value;
}
}
A StAX (JSR-173) parser (included in the JDK/JRE starting with Java SE 6) can be used. Then you can advance the XMLStreamReader to the child node and unmarshal from there.
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("src/forum14358769/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
// Advance to the "To" element.
while(xsr.hasNext()) {
if(xsr.isStartElement() && "To".equals(xsr.getLocalName())) {
break;
}
xsr.next();
}
// Unmarshal from the XMLStreamReader that has been advanced
JAXBContext jc = JAXBContext.newInstance(ToType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
ToType toType = unmarshaller.unmarshal(xsr, ToType.class).getValue();
}
}
For More Information
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
You will have to do following code for doing JAXB Unmarshalling:
unmarshaler.unmarshal(source);
This will return you the instance of class which you would have generated using xjc command.
Then you can access any property using bean methods.

Categories