Java – JAXB unmarshalling error: expected element is < {} root >
•
Java
I'm reusing existing objects generated elsewhere to ungroup XML data in the form of string types
Object:
/* 3: */ import java.util.ArrayList;
/* 4: */ import java.util.List;
/* 5: */ import javax.xml.bind.annotation.XmlAccessType;
/* 6: */ import javax.xml.bind.annotation.XmlAccessorType;
/* 7: */ import javax.xml.bind.annotation.XmlElement;
/* 8: */ import javax.xml.bind.annotation.XmlRootElement;
/* 9: */ import javax.xml.bind.annotation.XmlType;
/* 10: */
/* 11: */ @XmlAccessorType(XmlAccessType.FIELD)
/* 12: */ @XmlType(name="",propOrder={"policy"})
/* 13: */ @XmlRootElement(name="MyNodeResponse")
/* 14: */ public class MyNodeResponse
/* 15: */ {
/* 16: */ @XmlElement(name="Policy")
/* 17: */ protected List<Policy> policy;
/* 18: */
/* 19: */ public List<Policy> getPolicy()
/* 20: */ {
/* 21:65 */ if (this.policy == null) {
/* 22:66 */ this.policy = new ArrayList();
/* 23: */ }
/* 24:68 */ return this.policy;
/* 25: */ }
/* 26: */ }
My unmarshalling Code:
JAXBContext jc = JAXBContext.newInstance(MyNodeResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyNodeResponse myNodeResponse = (MyNodeResponse)unmarshaller.unmarshal(new InputSource(new ByteArrayInputStream(xmlStringInput.getBytes("utf-8"))));
My input XML:
<ns2:MyNodeResponse
xmlns:ns2="mynamespace/2010/10">
<ns2:Policy>
....more data....
<ns2:Policy/>
<ns2:MyNodeResponse />
I received the following error when ungrouping:
unexpected element (uri:"mynamespace/2010/10",local:"MyNodeResponse"). Expected elements are <{}MyNodeResponse>
What exactly does "{}" refer to in the error and how to ungroup in a way that matches what exists in the input XML and what the object expects?
Solution
What does the error message say
In {} mynoderespons, the {} section references a qualified name that does not set the namespace URI section
How to solve it
You need to use the package level @ XMLSchema annotation to map namespace qualifications:
package-info. java
@XmlSchema(
namespace = "mynamespace/2010/10",elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For more information
> http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
