JAXB unmarshaller throws exception if there is no package-info.java file. This code is called from another language and custom classloader can not load package-info properly.
I had already manually added the namespace parameter to all the #XmlElement annotations.
Xml root element has multiple xmlns attributes. Two of them (xmlns and xmlns:c) have the same value (I can not change xml, which is comeing from external service)
BUT: It works if I remove xmlns="urn:foo:bar" from document even without package-info
The question: how can I unmarshall without package-info (I can not modify custom classloader which can not load it) and without removing xmlns from xml ?
What should I change in my code?
Java 1.6_45 vJAXB 2.1.10
XML root element sample:
<?xml-stylesheet type="text/xsl" href="file1.xslt">
<?xml-stylesheet type="text/xsl" href="file1.xslt"?>
<c:ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:foo:ba" xmlns:c="urn:foo:bar" xmlns:std="urn:foo:std"
xsi:schemaLocation="urn:foo:bar file2.xsd">
Code:
String path = "D:\\data\\document.xml";
try {
JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
Object root = u.unmarshal(new File(path)); //exception w/o package-info.java
CDocument doc= (CDocument) JAXBIntrospector.getValue(root);
System.out.println(doc);
package-info.java
#javax.xml.bind.annotation.XmlSchema(namespace = "urn:foo:bar", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns={#XmlNs(namespaceURI = "urn:foo:bar", prefix = "")})
package test.model;
import javax.xml.bind.annotation.XmlNs;
Exception:
javax.xml.bind.UnmarshalException: Unable to create an instance of my.package.here.model.ANY
Any ideas will be greatly appreciated, I had spent so much time trying, but still have nothing.
BUT: It works if I remove xmlns="urn:foo:bar" from document even without package-info
This means that you have not added the namespace parameter everywhere you need to.
In the XML document below without the #XmlSchema annotation to map the namespace qualification, you would require the namespace parameter on the #XmlRootElement annotation for the Foo class and the #XmlElement annotation on the bar property.
<foo xmlns="http://www.example.com">
<bar>Hello World</bar>
</foo>
Related
I'm trying to get my JAXB marshaller to use the provided schemaLocation without using
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "some location");
I see that there is an option to provide a schema location in my
package descriptor
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://my.website.com/TheClass"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
, location = "http://my.website.com/TheClass TheClass.xsd"
)
package com.mypackage.beans;
but it won't print in the xml
I assume that your main issue is that the generated XML from your marshaller misses the required name space.
Have you tried adding the xmlns={#XmlNs(prefix="your_name_space", namespaceURI="http://my.website.com/TheClass")}
in your XmlSchema annotation in your package descriptor?
(Suggestion taken from: JAXB namespace prefixes missing)
i received the xml
<?xml version="1.0" encoding="UTF-8"?>
<ns1:paymentResponse xmlns:ns1="urn:Brifastservice">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tns:paymentAccount_CT_Result">
<message xsi:type="xsd:string">[ERROR] Reference Number Same </message>
<status xsi:type="xsd:string">2007</status>
</return>
</ns1:paymentResponse>
i want to unmarshal it , i am using below code
JAXBContext jaxbContext = JAXBContext.newInstance(PaymentResponse.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
StringReader resRreader = new StringReader(res);
final SAXParserFactory sax = SAXParserFactory.newInstance();
sax.setNamespaceAware(true);
final XMLReader reader = sax.newSAXParser().getXMLReader();
final Source er = new SAXSource(reader, new InputSource(resRreader));
PaymentResponse response = (PaymentResponse)um.unmarshal(er);
return response.getReturnResponse().getMessage();
but I got the exception
prefix xsd is not bound to a namespace
If I use
sax.setNamespaceAware(false);
then I get
javax.xml.bind.UnmarshalException:
unexpected element (uri:"", local:"ns1:paymentResponse").
Expected elements are <{urn:someService}paymentResponse>
please suggest how to unmarshal? I have package info
#javax.xml.bind.annotation.XmlSchema(
namespace = "urn:someService",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
The XML document uses XSI to declare the XSD type of the elements, but the XSD namespace is not defined anywhere inside this document.
A quick and dirty solution would be to modify the string with the received XML document and forcefully add a xmlns:xsd="http://www.w3.org/2001/XMLSchema" declaration in the root element before it is parsed.
The proper soluction is to create a custom ContentHandler that intercepts the startDocument, endDocument, startPrefixMapping and endPrefixMapping events and makes uses of NamespaceSupport object to keep track of the declared namespaced and also inject the missing XSD namespace. An example of this (minus the injected namespace) can be found in the chapter Receiving Namespace Mappings of Processing XML with Java, available online at http://www.cafeconleche.org/books/xmljava/chapters/ch06s09.html.
I am attempting to Unmarshall XML from a file using JAXB. I also have a web service.
I want to use no namespace, not the namespace defined by the web service.
I am getting the following error:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"CustomerName"). Expected elements are <{http://www.ws.NET}CustomerName>
I think that the solution is to change the package-info.java file to use no namespace. Is this the correct approach? and what changes should I make to this file?
The package-info.java file looks as follows:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ws.NET", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package mynewpackage;
Thanks for your help
Changing QUALIFIED to UNQUALIFIED will help if the top-level element (corresponding to the #XmlRootElement annotated class) is in the target namespace but its children aren't, e.g.
<ns:Response xmlns:ns="http://www.ws.NET">
<CustomerName>Ian</CustomerName>
</ns:Response>
but this won't help if the top level (in this case Response) element is also unqualified.
When you have a JAXB model that is annotated to be namespace qualified and you want to unmarshal XML that isn't there are a couple of options.
If You Can Change the Model
You may just need to remove the #XmlSchema annotation from the package-info class. If there are no other package level annotations you could remove the whole class. Note that namespace information may also be specified on other annotations such as #XmlElement.
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
If You Can't Change The Model
If you can't change the JAXB model to remove the namespace qualification then you can leverage a SAX XMLFilter to apply a namespace to the XML as it's being read.
http://www.stackoverflow.com/questions/14609597/how-flexible-is-jaxb/14610836#14610836
This might be a related to JAXB Marshaller - How do I suppress xmlns namespace attributes?
But my problem is a little different.
I do the regular java marshalling and my xsd has no namespaces.The generated xml is without namespaces as well, except for the root element.
<?xml version="1.0" encoding="UTF-8"?><rootElement xmlns:ns2="unwanted namespace">
The unwanted namespace is from another schema from the same project and I am not sure why that is being picked up at this stage.
My rootElement.java generated by jaxb2-maven-plugin looks like :
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"feed"
})
#XmlRootElement(name = "rootElement", namespace = "")
public class RootElement{
....
}
At this point all I want is to get rid of the xmlns:ns2="unwanted namespace" attribute from the generated xml and I am struggling with it.
I looked at my package-info.java and it looks like:
#javax.xml.bind.annotation.XmlSchema(namespace = "unwanted namespace", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mypackage;
I tried adding he -npa but it wont work on jaxb2-maven-plugin for some reason. I tried the NamespaceMapper but that works for changing prefixes. I could not get it to remove the namespace altogether. This is bothering me for a day now.
I have similar requirements at the moment. The only solution that worked for me is implementing a wrapper for XMLStreamWriter.
Please, take a look at my answer here. I've also described there other solutions I tried out.
Serialization using code from the link above looks like this:
XMLOutputFactory factory = XMLOutputFactory.newFactory();
StringWriter writer = new StringWriter(XML_BUFFER_INITIAL_SIZE);
XMLStreamWriter xmlWriter = null;
try {
xmlWriter = factory.createXMLStreamWriter(writer);
JAXBContext context = JAXBContext.newInstance(MyJAXBGeneratedClass.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(reportContainer, new NamespaceStrippingXMLStreamWriter(xmlWriter));
xmlWriter.flush();
}
finally {
if (xmlWriter != null)
xmlWriter.close();
}
return writer.toString();
I am doing this,
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {
mine.beans.ObjectFactory.class });
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
orderhistory = (OrderHistory) unmarshaller.unmarshal(new StreamSource(
new StringReader(responseXML)));`
I am getting javax.xml.bind.UnmarshalException: Unexpected element "OrderHistory". Expected elements are "{_http://orderhistory.shc.com/common/domain}OrderHistory". but i checked my OrderHistory.java i have the
#XmlRootElement(name = "OrderHistory")
public class OrderHistory{
What am i missing???
Even the package-info.java file is also present
Here is my response xml,
<?xml version="1.0" encoding="UTF-8"?>
<OrderHistory>
<guid>5555</guid>
<syNumber xsi:nil="true"></syNumber>
<email xsi:nil="true"></email>
<totalPages>0</totalPages>
</OrderHistory>
Still i am facing the same issue???
I ve made changes to my package-info.java i have removed the namespace attribute but still i am seeing the same issue,
#javax.xml.bind.annotation.XmlSchema()
package mine.beans;
It appears as though your input document is not namespace qualified.
You have:
<OrderHistory>...</OrderHistory>
And your JAXB (JSR-222) implementation is expecting:
<OrderHistory xmlns="_http://orderhistory.shc.com/common/domain">...</OrderHistory>
Related
If you are unmarshalling from a DOM, make sure to call setNamespaceAware(true) on the instance of DocumentBuilderFactory.
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
As hint. Try to marshal the document from your object, and see if the tags are written as expected.
Did you try to modify your XML? Your UNmarshaller is expecting the OrderHistory-Element to be part of the "http://orderhistory.shc.com/common/domain" namespace, and yet it isnt. You could give this a try:
<?xml version="1.0" encoding="UTF-8"?>
<OrderHistory xmlns="_http://orderhistory.shc.com/common/domain">
<guid>5555</guid>
<syNumber xsi:nil="true"></syNumber>
<email xsi:nil="true"></email>
<totalPages>0</totalPages>
</OrderHistory>