Reputation: 346
Say my valid xml looks something like below:
<A xmlns="something">
<B>4</B>
</A>
My code is like this:
@XmlRootElement(name = "A")
@XmlAccessorType (XmlAccessType.FIELD)
@Getter //from lombok
public class Data{
@XmlElement(name = "B")
private String b;
public Data(String b) {
this.b = b;
}
public Data() {
}
// Getter/setter generated by lombok.
}
How do I get this to work? If my xml tag has some associated elements like 'xmlns' above, the JAXB unmarshaller throws exceptions.
I'm doing this for the first time, so I really don't have a clue about JAXB, unmarshaling, etc. I'm floundering. :) Any help would be good.
EDIT StackTrace:
javax.xml.bind.UnmarshalException: unexpected element (uri:"something", local:"A"). Expected elements are <{}B>,<{}A>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at ...
Upvotes: 0
Views: 1425
Reputation: 2121
All you need is to add an @XmlAttribute to A (alogn with the corresponding getter/setter)
@XmlRootElement(name = "A")
@XmlAccessorType (XmlAccessType.FIELD)
@Getter //from lombok
public class Data{
@XmlAttribute(name="xmln")
String attr;
@XmlElement(name = "B")
private String b;
public Data(String b) {
this.b = b;
}
public Data() {
}
// Getter/setter generated by lombok.
public String getAttr(){
return attr;
}
public void setAttr(String attr){
this.attr = attr;
}
}
EDIT: This does not answer the question because the question is about xml namespaces and not attributes.
Upvotes: 0
Reputation: 6419
From XmlRootElement
's sources:
public @interface XmlRootElement { /** * namespace name of the XML element. * <p> * If the value is "##default", then the XML namespace name is derived * from the package of the class ( {@link XmlSchema} ). If the * package is unnamed, then the XML namespace is the default empty * namespace. */ String namespace() default "##default"; // else of file
You should also keep in mind this rule also apply to most JAXB annotations, so you need to specify the root element and also other elements with namespace explicitly given, like this:
@XmlRootElement(namespace="something", name = "A")
@XmlAccessorType (XmlAccessType.FIELD)
@Getter //from lombok
public class Data{
@XmlElement(namespace="something", name = "B") // <-- mind this
private String b;
public Data(String b) {
this.b = b;
}
public Data() {
}
// Getter/setter generated by lombok.
}
If you don't, it will expect, supposing your Data
is in org.example
package, some elements within namespace org.example
as long as you have @XmlSchema(elementFormDefault=QUALIFIED)
, or it will expect elements in the default namespace.
Upvotes: 3
Reputation: 5568
If you have a namespace in your xml document, you need to have that in your jaxb annotations as well.
E.g. like this:
@XmlRootElement(name = "A", namespace="something")
@XmlAccessorType (XmlAccessType.FIELD)
public class Data{
Upvotes: 2