XML to java object using jaxb Unmarshalling namespace
create package-info.java in your package and remove
namespace="http://schema.cs.csg.com/cs/ib/cpm"
from @XmlRootElement.
package-info.java
@XmlSchema(
namespace="http://schema.cs.csg.com/cs/ib/cpm",
elementFormDefault=XmlNsForm.QUALIFIED
)
package com.cs.xmlparser;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Use JAXB to create Object from XML String
To pass XML content, you need to wrap the content in a Reader
, and unmarshal that instead:
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Person person = (Person) unmarshaller.unmarshal(reader);
Unable to Unmarshall a xml in java using JAXB without using any annotation- No error but Unmarshalling is incorrect
In the xml you don't have to specify the class name (Customer
) as the child of your list (customerList
), just use the list name directly for every element.
So, change the xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<basePojo>
<customerId>C001</customerId>
<customerList>
<name>Ram</name>
<phoneNo>123445</phoneNo>
</customerList>
<customerList>
<name>Tom</name>
<phoneNo>2332322</phoneNo>
</customerList>
</basePojo>
Java jaxb unmarshalling from xml to java object returns null value
The problem is that your POJOS + annotations do not correctly map to the provided xml. Specifically in "SupplierRootDto" field "supplierDtos" needs to change or use annotation to override as below:
@XmlRootElement(name = "suppliers")
@XmlAccessorType(XmlAccessType.FIELD)
public class SupplierRootDto {
// ADD NAME TO MATCH THE XML ELEMENT
@XmlElement(name = "supplier")
private List<SupplierDto> supplierDtos;
public SupplierRootDto() {
}
public List<SupplierDto> getSupplierDtos() {
return supplierDtos;
}
public void setSupplierDtos(List<SupplierDto> supplierDtos) {
this.supplierDtos = supplierDtos;
}
}
Then it will unmarshal properly.
Why is jaxb not unmarshalling this XML document into a Java object?
You need to specify the namespace
at the element level.
For example:
@XmlElement(name = "id", namespace = "http://purl.org/atom/ns#")
private String id;
To set a default namespace you can do it at the package level, creating the package-info.java file in the package folder with a content like this:
@XmlSchema(
namespace = "http://purl.org/atom/ns#",
elementFormDefault = XmlNsForm.QUALIFIED)
package your.model.package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Besides, as you are explicitly adding @XmlElement
to all your fields, you can remove the @XmlAccessorType(XmlAccessType.FIELD)
annotation, as it's purpose it's to map by default all fields to elements.
Related Topics
Count Int Occurrences with Java8
Blank PDF Even with the Simplest Jasperreport Jrxml
Differencebetween String and Stringbuffer in Java
Convert Between Localdate and SQL.Date
Byte Array to Short Array and Back Again in Java
Are Two Java Objects with Same Hashcodes Not Necessarily Equal
Apache Httpclient Making Multipart Form Post
Problems Resteasy 3.09 Corsfilter
Generating 8-Character Only Uuids
How to Change the Color of Specific Words in a Jtextpane
Using Ntlm Authentication in Java Applications