【发布时间】:2014-02-04 12:16:36
【问题描述】:
我有一个业务需求,即从类(在 Schema 中定义)以尽可能轻量级的形式生成 XML。为此,我从 xml 中删除了所有命名空间信息。
示例架构:
<xs:schema xmlns="http://aschema/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://aschema/"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Test" type="TestType"/>
<xs:complexType name="TestType">
<xs:sequence>
<xs:element name="test" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
生成Test 类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TestType", propOrder = {
"test"
})
@XmlRootElement(name = "Test")
public class Test {
@XmlElement(required = true)
protected String test;
/**
* Gets the value of the test property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTest() {
return test;
}
/**
* Sets the value of the test property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTest(String value) {
this.test = value;
}
}
Marshals to(如果您愿意,我可以解释一下,但我认为这只会使问题变得混乱 - 如果需要,很乐意添加):
<Test><test>Hello World!</test></Test>
现在我想解组它。我明白了(并不奇怪):
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Test"). Expected elements are <{http://aschema/}Test>
有什么方法可以定义我的解组过程遇到这个问题时使用什么?
我无法更改架构 - 这是行业标准。
我无法更改 Test 对象 - 它是从架构生成的。
我能找到的对这个错误的所有引用似乎要么指向 java 类的变化,要么指向架构的变化——我不能做这两件事。
请注意,我的 Test 类是在尝试创建 Minimal, Complete, Tested and Readable example。我要解组的真实对象要复杂得多。
【问题讨论】:
标签: java xml jaxb unmarshalling