【问题标题】:No URI javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none)没有 URI javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“SearchAndList”)。预期元素是(无)
【发布时间】:2017-01-06 20:28:09
【问题描述】:

我无法解组我的数据。我收到以下错误:

错误 FsceClient - getDataInMatches 中的错误:意外元素 (uri:“”,本地:“SearchAndList”)。预期元素是(无) 要求的参数:+COUNTRY=US+YR=2016+DIV=Ford+WB=122.0 javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“SearchAndList”)。预期元素是(无) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)

这是我的 xml 文件:

<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>

【问题讨论】:

  • 请编辑您的问题并显示您的 XML 架构以及 XML。如果我们能看到进行解组的代码也会有所帮助。
  • 我对网络服务很陌生。我正在使用注释:这是我的代码: UnmarshallerPool searchAndListPool = null; JAXBContext jaxbContext_SearchAndList = JAXBContext.newInstance(SearchAndList.class); searchAndListPool = new UnmarshallerPool(MAX_POOL_SIZE, jaxbContext_SearchAndList); searchAndListPool.initialize();解组器 unmarshaller = null; InputStream in = makeCachedGETServerCall(finalURL); unmarshaller = searchAndListPool.getResource(); return (SearchAndList) unmarshaller.unmarshal(in);
  • @XmlRootElement(name="searchAndList") @XmlAccessorType(XmlAccessType.FIELD) public class SearchAndList extends ArrayList implements Serializable{ private static final long serialVersionUID = 2447564738604L; @XmlElement 私有字符串 fvd;公共 SearchAndList() { super(); } public void setFvd(String fvd) { this.fvd = fvd; } 公共字符串 getFvd() { 返回 fvd; } @XmlElement(name="sol", type=Sol.class) public List getSol() { return this; } }
  • 问题的底部是显示关键字(java、xml、rest、jaxb)的阴影矩形。在那些下面是一个“编辑”链接。请使用该链接更改您的问题并将您的代码放在那里。如您所见,在 cmets 中代码非常难以阅读。
  • 您能否检查并可能接受答案?

标签: java xml rest jaxb


【解决方案1】:

当 XSD 架构不包含元素定义而仅包含类定义(即复杂类型)时会遇到这种情况。

例如对于这个 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>

创建的对象工厂是这样的:

@XmlRegistry
public class ObjectFactory {
   public ObjectFactory() {
   }
   public Foo createFoo() {
        return new Foo();
    }
}

但对于这个 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类是:

@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 wrapper 创建方法。使用第二个 XSD,解组器在遇到名称为“foo”的标记时知道该怎么做。因此,如果您有 XSD,请添加“元素”定义以及复杂类型。

----- 编辑---- 示例解组器代码:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object result = ((JAXBElement<Object>) jaxbUnmarshaller.unmarshal(stream)).getValue();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2018-01-10
    • 2021-08-01
    • 2012-08-30
    相关资源
    最近更新 更多