没有 URI javax.xml.bind.UnmarshalException:意外元素(uri:"", local:"SearchAndList").预期元素为(无) [英] No URI javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none)
问题描述
我无法解组我的数据.我收到以下错误:
I am having trouble to unmarshall my data. I got the following error:
ERROR FsceClient - getDataInMatches 中的错误:意外元素(uri:",本地:SearchAndList").预期元素为(无)请求参数:+COUNTRY=US+YR=2016+DIV=Ford+WB=122.0javax.xml.bind.UnmarshalException: 意外元素 (uri:"", local:"SearchAndList").预期元素为(无)在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
ERROR FsceClient - Error in getDataInMatches : unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none) requested params:+COUNTRY=US+YR=2016+DIV=Ford+WB=122.0 javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
这是我的 xml 文件:
This is my xml file:
<SearchAndList>
<fvd>
+COUNTRY=US+YR=2016+DIV=Ford+WB=122.0
</fvd>
<sol>
<rsi>
<sType>Ss</sType>
<mHave>true</mHave>
<toAr>0</toAr>
<toAr>0</toAr>
<toAr>22</toAr>
</rsi>
<rsi>
<sType>ssa</sType>
<mHave>true</mHave>
<toAr>77</toAr>
</rsi>
</sol>
<sol>
<rsi>
<sType>sve</sType>
<mHave>false</mHave>
<toAr>0</toAr>
<toAr>21</toAr>
</rsi>
</sol>
</SearchAndList>
推荐答案
当 XSD 架构不包含元素定义而只包含类定义(即复杂类型)时会遇到这种情况.
This is encountered when the XSD schema does not contain element definitions and only contains class definitions (i.e. complex types).
例如对于这个 XSD,
e.g. for this XSD,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="bar" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
创建的对象工厂是这样的:
The object factory created is like this:
@XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public Foo createFoo() {
return new Foo();
}
}
但是对于这个 XSD:
BUT FOR THIS XSD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo" type="foo" nillable="true"/>
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="bar" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
JAXB 创建的 ObjectFactory 类是:
The ObjectFactory class created by JAXB is:
@XmlRegistry
public class ObjectFactory {
private final static QName _Foo_QNAME = new QName("", "foo");
public ObjectFactory() {
}
public Foo createFoo() {
return new Foo();
}
@XmlElementDecl(namespace = "", name = "foo")
public JAXBElement<Foo> createFoo(Foo value) {
return new JAXBElement<Foo>(_Foo_QNAME, Foo.class, null, value);
}
}
可以看到还添加了 JAXBElement 包装器创建方法.使用第二个 XSD,解组器知道在遇到名称为foo"的标签时该做什么.因此,如果您有 XSD,请添加元素"定义以及复杂类型.
You can see that the JAXBElement wrapper creation method is also added. With the second XSD, the unmarshaller knows what to do when it encounters a tag with name "foo". So if you have an XSD, add "element" definitions as well as the complex types.
----- 编辑----示例解组器代码:
----- EDIT---- The sample unmarshaller code:
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object result = ((JAXBElement<Object>) jaxbUnmarshaller.unmarshal(stream)).getValue();
这篇关于没有 URI javax.xml.bind.UnmarshalException:意外元素(uri:"&quot;, local:"SearchAndList&quot;).预期元素为(无)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!