Would like unknown attributes to error when Jaxb unmarshalling - java

I'm using Jaxb to unmarshal XML into a java object. I need to know when new attributes/elements are in the XML and fail. However by default the unmarshaller is ignoring new elements/attributes.
Is there a configuration I can set to fail when new attributes/elements exist in the XML that are not specified in the POJO?
My POJO:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "ROOT")
public class Model {
private A a;
public A getA() {
return a;
}
#XmlElement(name = "A")
public void setA(A a) {
this.a = a;
}
static class A {
String country;
public String getCountry() {
return country;
}
#XmlAttribute(name = "Country")
public void setCountry(String country) {
this.country = country;
}
}
}
Code to unmarshal:
JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
String sample = "<ROOT>" +
"<A Country=\"0\" NewAttribute=\"0\"></A>" +
"<NEWELEMENT> </NEWELEMENT>" +
"</ROOT>";
InputStream stream = new ByteArrayInputStream(sample.getBytes(StandardCharsets.UTF_8));
Object unmarshal = jaxbUnmarshaller.unmarshal(stream);

You need to call Unmarshaller.setEventHandler() to make invalid XML content fail.

You can block unexpected content by enabling XML Schema validation on the Unmarshaller. If you don't already have an XML Schema for your POJO, you can generate one at runtime from the JAXBContext, build a Schema object and then set it on the Unmarshaller. By default the Unmarshaller will throw an exception if the XML document isn't valid with respect to the schema.
Here's an example of how to do that:
JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
// Generate XML Schema from the JAXBContext
final List<ByteArrayOutputStream> schemaDocs = new ArrayList<ByteArrayOutputStream>();
jaxbContext.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(String namespaceUri, String suggestedFileName)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(baos);
schemaDocs.add(baos);
sr.setSystemId(suggestedFileName);
return sr;
}
});
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
int size = schemaDocs.size();
Source[] schemaSources = new Source[size];
for (int i = 0; i < size; ++i) {
schemaSources[i] = new StreamSource(
new ByteArrayInputStream(schemaDocs.get(i).toByteArray()));
}
Schema s = sf.newSchema(schemaSources);
// Set the JAXP Schema object on your Unmarshaller.
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(s);
String sample = "<ROOT>" +
"<A Country=\"0\" NewAttribute=\"0\"></A>" +
"<NEWELEMENT> </NEWELEMENT>" +
"</ROOT>";
InputStream stream = new ByteArrayInputStream(sample.getBytes("UTF-8"));
Object unmarshal = jaxbUnmarshaller.unmarshal(stream);
Combine this with the ValidationEventHandler (set through Unmarshaller.setEventHandler()) suggested in the previous answer if you wish to be notified about multiple errors or filter out validation errors that you want to tolerate.

Related

Convert Java JAXB object to JSON using Jackson or MOXy dependency

I am working on an API that will return an JSON response from an XML source. I have used RestTemplate and JAXB to get the XML string from the source and then used StringReader and Unmarshaller to create the Java object. The object looks like this;
#XmlRootElement(name="ItemSearchResponse", namespace="http://webservices.amazon.com/AWSECommerceService/2011-08-01") //
#XmlAccessorType(XmlAccessType.FIELD)
public class SampleXML {
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType
public static class OperationRequest {
#XmlTransient
private String RequestId;
#XmlElement(name="RequestId")
public void setRequestId(String id) {
this.RequestId = id;
}
public String getRequestId() {
return RequestId;
}
...
This is the code that should return the JSON string to the browser;
#RequestMapping("/samplexml2")
public SampleXML CreateXMLFile2 () throws EncoderException, FileNotFoundException, SAXException {
try {
String requestUrl = null;
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ResponseErrorHandler());
String decodedUrl = "http://localhost:8080/sample.xml";
String response = restTemplate.getForObject(decodedUrl, String.class);
//Prepare JAXB objects
JAXBContext jaxbContext = JAXBContext.newInstance(SampleXML.class);
Unmarshaller u = jaxbContext.createUnmarshaller();
//Create an XMLReader to use with our filter
XMLReader reader = XMLReaderFactory.createXMLReader();
//Create the filter (to add namespace) and set the xmlReader as its parent.
NamespaceFilter inFilter = new NamespaceFilter("http://webservices.amazon.com/AWSECommerceService/2011-08-01", true);
inFilter.setParent(reader);
//Prepare the input, in this case a java.io.File (output)
InputSource is = new InputSource(new StringReader(response));
//Create a SAXSource specifying the filter
SAXSource source = new SAXSource(inFilter, is);
//Do unmarshalling
SampleXML myJaxbObject = (SampleXML) u.unmarshal(source);
//Convert to myJaxbObject to JSON string here;
return myJaxbObject;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
I want to write the conversation at this line; //Convert to myJaxbObject to JSON string here;
I have read many articles that point to this library; https://github.com/FasterXML/jackson-module-jaxb-annotations But I have not been able to use it successfully.
I would like an example that uses Jackson or MOXy dependencies
Have you tried simply changing your RequestMapping to #RequestMapping(value = "/samplexml2", produces = MediaType.APPLICATION_JSON_VALUE)?

Unmarshal JSON String to some "Object" using MOXy

I'm trying to write a method to pretty print JSON Strings, using MOXy. So what I want is to have a method like this
public String formatJson(String input) { ... }
I think the way to go is to parse the String to a generic Object (Something like a SAX-Document, or the kind), and then marshal this Object back to JSON using some formatting properties (which is not the problem :-) ).
The Problem is, when reading the JSON-String-Input, I don't have a Class to unmarshal to (as I want the method to be as generic as possible).
[edited] GSON and Jackson examples removed, as only MOXy is the question.
I tried this:
public static String toFormattedJson(final String jsonString) {
String formatted;
try {
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] { JAXBElement.class }, null);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setProperty(MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(JSON_INCLUDE_ROOT, true);
StringReader reader = new StringReader(jsonString);
Object element = unmarshaller.unmarshal(reader); // Exception is thrown here
formatted = toFormattedJson(element);
} catch (final JAXBException e) {
formatted = jsonString;
}
return formatted;
}
but I get an this Exception
javax.xml.bind.UnmarshalException
- with linked exception:
[java.lang.ClassCastException: org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler cannot be cast to org.eclipse.persistence.internal.oxm.record.UnmarshalRecord]
So how can I read an arbitrary JSON String in to a Java Object, if I don't have any Class for that specific String?
Update:
This is the method used to format an Object into a JSON String:
private static String toFormattedJson(Object obj) {
String result;
try (StringWriter writer = new StringWriter()) {
final JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] { obj.getClass() }, null);
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, false);
marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, false);
marshaller.setProperty(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true);
marshaller.marshal(obj, writer);
writer.flush();
result = writer.toString();
} catch (JAXBException | IOException e) {
result = obj.toString();
}
return result;
}
And using now the code from below (Martin Vojtek), when I try to format
String jsonString = "{\"p\" : [ 1, 2, 3]}";
I get:
{
"p" : "1"
}
You can specify String as the unmarshal target:
public static void main(String[] args) {
System.out.println(toFormattedJson("[{\"test\":true}, \"ok\", [\"inner\",1]]"));
}
public static String toFormattedJson(final String jsonString) {
String formatted;
try {
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] { JAXBElement.class }, null);
System.out.println("jaxbContext="+jaxbContext);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true);
StringReader reader = new StringReader(jsonString);
Object element = unmarshaller.unmarshal(new StreamSource(reader), String.class);
formatted = toFormattedJsonElement(element);
} catch (final JAXBException e) {
e.printStackTrace();
formatted = jsonString;
}
return formatted;
}
private static String toFormattedJsonElement(Object element) {
return "formatted: " + element;
}

How to make xml schema relate(link) to JTable,xml java?

Here you can see my application:
So what i need to do:
I dont know how I can through code link xml schema with JTable with all type(int,string,float). Say like Year is type int and in schema is int and how i can link it? I dont know how will be in english link or relate.
In this application i write all data to xml file, and when application loaded it loads all data from xml file.
Here i creating xml schema:
public void CreateSchema(String FileName){
file=FileName;
JAXBContext jc;
try {
jc = JAXBContext.newInstance(XmlSchemaType.class);
jc.generateSchema(new SchemaOutputResolver() {
#Override
public javax.xml.transform.Result createOutput(String namespaceURI, String suggestedFileName)throws IOException {
suggestedFileName=file+".xsd";
return new StreamResult(suggestedFileName);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
Here is all types:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Auto")
public class XmlSchemaType {
row[] Row;
}
class row {
#XmlAttribute
byte ID;
#XmlElement
String VIN;
#XmlElement
String Make;
#XmlElement
String Model;
#XmlElement
int Year;
#XmlElement
String Description;
#XmlElement
float Cost;
}
Here is writing to xml file:
public void CreateXml(JTable tb,JTable tb2,String FileName){
try {
file=FileName;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Auto");
doc.appendChild(rootElement);
int i=0,j=0,k=0;
while (i<tb.getRowCount()){
j=0;
Element rows = doc.createElement("Row");
rootElement.appendChild(rows);
Attr attr = doc.createAttribute("id");
attr.setValue((i+1)+"");
rows.setAttributeNode(attr);
//Pirma lentele
while (j<tb.getColumnCount()-1){
Element element = doc.createElement(tb.getTableHeader().getColumnModel().getColumn(j).getHeaderValue()+"");
element.appendChild(doc.createTextNode(tb.getModel().getValueAt(i, j)+""));
rows.appendChild(element);
j++;
}
//Antra lentele
j=2;//pirmu lauku nereikia
while (j<tb2.getColumnCount()-1){
Element element2 = doc.createElement(tb2.getTableHeader().getColumnModel().getColumn(j).getHeaderValue()+"");
element2.appendChild(doc.createTextNode(tb2.getModel().getValueAt(i, j)+""));
rows.appendChild(element2);
if (j==2){
tb2.getModel().setValueAt(tb.getModel().getValueAt(i, 0),i,1);
}
j++;
}
i++;
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result;
try {
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(
new File(file+".xml"));
result = new StreamResult(fileOutputStream);//new FileOutputStream(file+".xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");//new line... kad butu naujoje eiluteje
transformer.transform(source, result);
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
//file.renameTo(FileName+".xml");
}
And here loading my xml file:
public void PopulateDataSet(JTable tb,JTable tb2,String FileName){
file=FileName;
File f= new File(file+".xml");
if (f.exists()){
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(f);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Row");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
DefaultTableModel model = (DefaultTableModel) tb.getModel();
model.addRow(new Object[] { "", "","","","Delete" });
tb.getModel().setValueAt(eElement.getElementsByTagName("VIN").item(0).getTextContent(),temp, 0);
tb.getModel().setValueAt(eElement.getElementsByTagName("Make").item(0).getTextContent(),temp, 1);
tb.getModel().setValueAt(eElement.getElementsByTagName("Make").item(0).getTextContent(),temp, 2);
tb.getModel().setValueAt(eElement.getElementsByTagName("Year").item(0).getTextContent(),temp, 3);
tb.getModel().setValueAt("Delete",temp, 4);
DefaultTableModel model2 = (DefaultTableModel) tb2.getModel();
model2.addRow(new Object[] { (tb2.getRowCount()+1), "","","","Delete" });
tb2.getModel().setValueAt(eElement.getElementsByTagName("VIN").item(0).getTextContent(),temp, 1);
tb2.getModel().setValueAt(eElement.getElementsByTagName("Description").item(0).getTextContent(),temp, 2);
tb2.getModel().setValueAt(eElement.getElementsByTagName("Cost").item(0).getTextContent(),temp, 3);
tb2.getModel().setValueAt("Delete",temp, 4);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (!f.exists()){
CreateXml(tb,tb2,file);
CreateSchema(file);
}
}
But how to use xml shema with JTable, xml?
"Here is writing to xml file:" ---- "And here loading my xml file:"
Why are you using DOM to read and write the xml, when you are already using JAXB Mapping. If you are doing the mapping correctly 1, it's just a matter of using the Marshaller and Unmarshaller to write and read, respectively. Make sure to look at those API links yo see example usage. It's only around 5 lines of code to handle each operaion.
(1) Please see the JAXB tutorial for more info about JAXB mapping.
Also, you can just create your own AbstractTableModel and unmarshal and marshal straight to and from the table model. This is probably the most effective way to keep everything sync. Create a class Auto to represent each row, and a class AutoModel, that will be the root element in the xml document, as well as the TableModel for the JTable. Something like:
Auto class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "Auto", propOrder = {
"id", "VIN", "Make", "Model", "Year", "Description", "Cost"
})
public class Auto {
#XmlElement(name = "id")
Integer id;
#XmlElement(name = "VIN")
String VIN;
#XmlElement(name = "Make")
String Make;
#XmlElement(name = "Model")
String Model;
#XmlElement(name = "Year")
Integer Year;
#XmlElement(name = "Description")
String Description;
#XmlElement(name = "Cost")
Float Cost;
// === DON'T FORGET YOUR GETTERS and SETTERS
}
AutoModel class
#XmlRootElement(name = "AutoList")
public class AutoModel extends AbstractTableModel {
String[] columnNames = {"VIN", "Make", "Model", "Year"};
#XmlElement(name = "Auto")
protected List<Auto> autos;
public AutoModel() {
autos = new ArrayList<Auto>();
}
#Override
public int getRowCount() {
return autos.size();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
#Override
public boolean isCellEditable(int row, int col) {
return false;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Auto auto = autos.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0 : value = auto.getVIN(); break;
case 1 : value = auto.getMake(); break;
case 2 : value = auto.getModel(); break;
case 3 : value = auto.getYear(); break;
}
return value;
}
}
Test, using this xml file
<?xml version="1.0" encoding="UTF-8"?>
<AutoList>
<Auto>
<id>1</id>
<VIN>123456788910FASDE</VIN>
<Make>Mercedes</Make>
<Model>CL 550</Model>
<Year>2012</Year>
<Description>Hello World</Description>
<Cost>80000.00</Cost>
</Auto>
</AutoList>
import java.awt.Dimension;
import java.io.File;
import javax.swing.*;
import javax.xml.bind.*;
public class TestTableMarshall {
private static final String INPUT_FILE = "src/table/autos.xml";
private static final String OUTPUT_FILE = "src/table/autos1.xml";
public static void main(String[] args) throws Exception {
AutoModel model = unmarshal(INPUT_FILE);
JTable table = new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
};
JOptionPane.showMessageDialog(null, new JScrollPane(table));
marshal(model, OUTPUT_FILE);
}
private static void marshal(AutoModel model, String file) throws Exception {
JAXBContext context = JAXBContext.newInstance(AutoModel.class);
Marshaller marshaller = context.createMarshaller();
File f= new File(file);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(model, f);
}
private static AutoModel unmarshal(String file) throws Exception {
JAXBContext context = JAXBContext.newInstance(AutoModel.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
AutoModel model = (AutoModel)unmarshaller.unmarshal(new File(file));
return model;
}
}
As far the the AutoModel goes, it only works for your first table. You will need to create another model for your repairs table. Also, the model, currently only offered read-only. You will need to add other functionality to say add a row and set individual values.
Here are some resources to look at:
How to Use Tables: Creating a Table Model
JAXB Specification toturial
NOTE: With the JAXB annotations above you can create the schema, and you you want to validate the xml against it, you could just set the schema when you unmarshal. Something like:
private static AutoModel unmarshal(String file) throws Exception {
JAXBContext context = JAXBContext.newInstance(AutoModel.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("src/table/autos.xsd"));
unmarshaller.setSchema(schema);
AutoModel model = (AutoModel)unmarshaller.unmarshal(new File(file));
return model;
}

Generate a XSD from a JAXB-annotated class without using File

I am trying to generate XSD from Java Annotated classes by following code mentioned in this post Is it possible to generate a XSD from a JAXB-annotated class
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
SchemaOutputResolver sor = new MySchemaOutputResolver();
jaxbContext.generateSchema(sor);
public class MySchemaOutputResolver extends SchemaOutputResolver {
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
File file = new File(suggestedFileName);
StreamResult result = new StreamResult(file);
result.setSystemId(file.toURI().toURL().toString());
return result;
}
}
This technique is using File system, My requirement is to get the XML as String without using file system.
Is there any possibility the Implementation of SchemaOutputResolver may not write file to disk and return or set some instance variable with the String value.
You can write the StreamResult on a StringWriter and get the string from that.
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
MySchemaOutputResolver sor = new MySchemaOutputResolver();
jaxbContext.generateSchema(sor);
String schema = sor.getSchema();
public class MySchemaOutputResolver extends SchemaOutputResolver {
private StringWriter stringWriter = new StringWriter();
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(stringWriter);
result.setSystemId(suggestedFileName);
return result;
}
public String getSchema() {
return stringWriter.toString();
}
}

How to add suffix to XmlRootElement

I've tried this:
package-info.java
#XmlSchema(xmlns=
{
#XmlNs(prefix="Person", namespaceURI="sample.url.something"),
#XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance")
})
Java
#XmlRootElement(name = "Person:sampleData")
public class Person {
private static String path = "files/test.xml";
#XmlElement()
public String Name;
#XmlElement()
public int Age;
public Person(){}
public Person(String name, int age){
this.Name = name;
this.Age = age;
}
public static String PersonToXMLString(Person person) throws JAXBException
{
JAXBContext jc = JAXBContext.newInstance(Person.class);
StringWriter sw = new StringWriter();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "somelocation.xsd");
marshaller.marshal(person, sw);
return sw.toString();
}
public static Person XMLStringToPerson() throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Person Person = (Person) unmarshaller.unmarshal(new File(path));
return Person;
}
public static void WriteXMLStringFile(String xml) throws IOException
{
File file = new File(path);
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
byte[] contentInBytes = xml.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String ReadXmlStringFromFile() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line.trim());
}
return sb.toString();
}
public static void main(String[] args) throws JAXBException, IOException
{
Person user = new Person("User",23);
String xml = user.PersonToXMLString(user);
System.out.println(xml);
user.WriteXMLStringFile(xml);
xml = user.ReadXmlStringFromFile();
//used to unmarshall xml to Person object
Person person = user.XMLStringToPerson();
System.out.println(person.Name);
}
}
XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Person:sampleData xmlns:Person="sample.url.something" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>User</Name>
<Age>23</Age>
</Person:sampleData>
If i do something like this i get exception on unmarshalling:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"sample.url.something", local:"sampleData").
Expected elements are <{}Person:sampleData>`
FYI: I can not modify the XML.
Any help gratefully received!
You can do the following:
package-info.java
Your #XmlSchema annotation should look something like the following on your package-info class. Since elementFormDefault is specified as UNQUALIFIED the namespace will only be applied to global elements (in JAXB those corresponding to the #XmlRootElement). Note a JAXB impl is not required to use the prefix included in the #XmlSchema when marshalling XML.
#XmlSchema(
elementFormDefault=XmlNsForm.UNQUALIFIED,
namespace="sample.url.something",
xmlns={
#XmlNs(prefix="Person", namespaceURI="sample.url.something")
}
)
package com.example;
import javax.xml.bind.annotation.*;
Person
The #XmlRootElement annotation should not include the prefix.
package com.example;
import javax.xml.bind.annotation.*;
#XmlRootElement(name="sampleData")
public class Person {
For More Information
You can read more about controlling namespace prefixes in JAXB on my blog:
http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html

Categories

Resources