【问题标题】:JaxB Unmarshall error : java.lang.RuntimeException: unexpected element (uri:"", local:"code"). Expected elements are <{}section>JaxB 解组错误:java.lang.RuntimeException:意外元素(uri:“”,本地:“代码”)。预期元素是 <{}section>
【发布时间】:2018-08-01 02:51:18
【问题描述】:

请告知此 JAXB Unmarshall 代码可能存在的问题 这是我在尝试解组下面提供的 XML 时收到的错误消息:

java.lang.RuntimeException:意外元素(uri:“”,本地:“代码”)。预期元素是

Borrower 类使用基于 XSD 的 JaxB 解析,使用 JDK 1.7 和 Eclipse IDE。

public class UnmarshallApplication {

public static void main(String[] args) {



    try {

    JAXBContext jaxbContext  = JAXBContext.newInstance(Borrower.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {
                public boolean handleEvent(ValidationEvent event ) {
                    throw new RuntimeException(event.getMessage(),event.getLinkedException());
                                                                   }
        });


    StringReader xmlCCR = new StringReader(returnValue2());
    JAXBElement<Borrower> root = jaxbUnmarshaller.unmarshal(new StreamSource(xmlCCR), Borrower.class);
    Borrower productListResponse = root.getValue();



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

借款人类别:

package bg.rbb.unmarshall;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"section"})
@XmlRootElement(name = "borrower")

public class Borrower {

@XmlElement(required = true)
protected List<Section> section;
@XmlAttribute(name = "code", required = true)
protected String code;
@XmlAttribute(name = "name")
protected String name;

public List<Section> getSection() {
    if (section == null) {
        section = new ArrayList<Section>();
    }
    return this.section;
}

public String getCode() {
    return code;
}

public void setCode(String value) {
    this.code = value;
}


public String getName() {
    return name;
}


public void setName(String value) {
    this.name = value;
}

}

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:wmh="http://www.wmhelp.com/2003/eGenerator" 
elementFormDefault="qualified">
<xs:element name="borr-info-list">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="borrower" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="req-file-name" type="xs:string"/>
  <xs:attribute name="req-file-time" type="xs:string"/>
  <xs:attribute name="req-file-user" type="xs:string"/>
  <xs:attribute name="req-file-entity" type="xs:string"/>
  <xs:attribute name="out-file-time" type="xs:string"/>
  <xs:attribute name="borr_count" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="borrower">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="section" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="code" type="xs:string" use="required"/>
  <xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="section">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="active-credits"/>
    <xs:element ref="overdue-history"/>
    <xs:element ref="new-credits"/>
  </xs:sequence>
  <xs:attribute name="entity-type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="active-credits">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="summaries" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="cred-count" type="xs:string"/>
  <xs:attribute name="source-entity-count" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="summaries">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="summary" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="grouping-attribute" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="summary">
<xs:complexType>
  <xs:attribute name="date-from" type="xs:string"/>
  <xs:attribute name="type" type="xs:string"/>
  <xs:attribute name="amount-approved" type="xs:string"/>
  <xs:attribute name="amount-drawn" type="xs:string"/>
  <xs:attribute name="monthly-installment" type="xs:string"/>
  <xs:attribute name="outstanding-performing-principal" type="xs:string"/>
  <xs:attribute name="outstanding-overdue-principal" type="xs:string"/>
  <xs:attribute name="balance-sheet-value" type="xs:string"/>
  <xs:attribute name="off-balance-sheet-value" type="xs:string"/>
  <xs:attribute name="overdue-payment-period" type="xs:string"/>
  <xs:attribute name="rest" type="xs:string"/>
  <xs:attribute name="term" type="xs:string"/>
  <xs:attribute name="active" type="xs:string"/>
  <xs:attribute name="year" type="xs:string"/>
  <xs:attribute name="category" type="xs:string"/>
  <xs:attribute name="months-count" type="xs:string"/>
  <xs:attribute name="max-cred-count" type="xs:string"/>
  <xs:attribute name="max-outstanding-overdue-principal" type="xs:string"/>
  <xs:attribute name="max-outstanding-overdue-interest-and-others" 
  type="xs:string"/>
  <xs:attribute name="max-off-balance-sheet-dues" type="xs:string"/>
  <xs:attribute name="date-last-correction" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="overdue-history">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="summary" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="new-credits">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="summary" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<borrower>
   <code>7777777777</code>
   <name>FirstName Surname LastName</name>
   <section>
      <entity-type>banks</entity-type>
      <active-credits>
         <cred-count>2</cred-count>
         <source-entity-count>1</source-entity-count>
         <summaries>
            <grouping-attribute>type</grouping-attribute>
            <summary>
               <date-from>2017-12-31</date-from>
               <type>Card</type>
               <amount-approved>4000</amount-approved>
               <amount-drawn>2105</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>2105</outstanding-performing-principal>
               <outstanding-overdue-principal>0</outstanding-overdue-principal>
               <balance-sheet-value>2105</balance-sheet-value>
               <off-balance-sheet-value>1895</off-balance-sheet-value>
            </summary>
            <summary>
               <date-from>2017-12-31</date-from>
               <type>Овърдрафт</type>
               <amount-approved>7434</amount-approved>
               <amount-drawn>7447</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>7434</outstanding-performing-principal>
               <outstanding-overdue-principal>13</outstanding-overdue-principal>
               <balance-sheet-value>7484</balance-sheet-value>
               <off-balance-sheet-value>0</off-balance-sheet-value>
            </summary>
         </summaries>
         <summaries>
            <grouping-attribute>overdue-payment-period</grouping-attribute>
            <summary>
               <date-from>2017-12-31</date-from>
               <overdue-payment-period>от 0 до 30 дни</overdue-payment-period>
               <amount-approved>11434</amount-approved>
               <amount-drawn>9552</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>9539</outstanding-performing-principal>
               <outstanding-overdue-principal>13</outstanding-overdue-principal>
               <balance-sheet-value>9589</balance-sheet-value>
               <off-balance-sheet-value>1895</off-balance-sheet-value>
            </summary>
         </summaries>
         <summaries>
            <grouping-attribute>rest</grouping-attribute>
            <summary>
               <date-from>2017-12-31</date-from>
               <rest>До една година</rest>
               <amount-approved>7434</amount-approved>
               <amount-drawn>7447</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>7434</outstanding-performing-principal>
               <outstanding-overdue-principal>13</outstanding-overdue-principal>
               <balance-sheet-value>7484</balance-sheet-value>
               <off-balance-sheet-value>0</off-balance-sheet-value>
            </summary>
            <summary>
               <date-from>2017-12-31</date-from>
               <rest>Над една година</rest>
               <amount-approved>4000</amount-approved>
               <amount-drawn>2105</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>2105</outstanding-performing-principal>
               <outstanding-overdue-principal>0</outstanding-overdue-principal>
               <balance-sheet-value>2105</balance-sheet-value>
               <off-balance-sheet-value>1895</off-balance-sheet-value>
            </summary>
         </summaries>
         <summaries>
            <grouping-attribute>term</grouping-attribute>
            <summary>
               <date-from>2017-12-31</date-from>
               <term>Над една година</term>
               <amount-approved>11434</amount-approved>
               <amount-drawn>9552</amount-drawn>
               <monthly-installment>0</monthly-installment>
               <outstanding-performing-principal>9539</outstanding-performing-principal>
               <outstanding-overdue-principal>13</outstanding-overdue-principal>
               <balance-sheet-value>9589</balance-sheet-value>
               <off-balance-sheet-value>1895</off-balance-sheet-value>
            </summary>
         </summaries>
      </active-credits>
      <overdue-history />
      <new-credits />
   </section>
</borrower>

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    问题是 xsd(以及生成的 POJO)和 xml 不匹配。

    例如,您可以在 Borrower 类中看到,名称和代码使用 @XmlAttribute 注释,但您的 xml 没有将它们作为属性,而是作为元素。

    更具体地说,您的 xml 如下所示:

    <borrower>
       <code>7777777777</code>
       <name>FirstName Surname LastName</name>
       <section>
        ...
    </borrower>
    

    但是 POJO 的构建是为了期待这样的事情(只关注代码和名称):

    <borrower code="someCode" name="someName">
       <section>
        ...
    </borrower>
    

    并继续存在更多类似的问题。所以我们必须修复你的xsd。它在你的控制之下吗?

    如果您将 xsd 更改为以下之一,您将能够解组。

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:wmh="http://www.wmhelp.com/2003/eGenerator"
               elementFormDefault="qualified">
        <xs:element name="borr-info-list">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="borrower" maxOccurs="unbounded"/>
                    <xs:element name="req-file-name" type="xs:string"/>
                    <xs:element name="req-file-time" type="xs:string"/>
                    <xs:element name="req-file-user" type="xs:string"/>
                    <xs:element name="req-file-entity" type="xs:string"/>
                    <xs:element name="out-file-time" type="xs:string"/>
                    <xs:element name="borr_count" type="xs:string"/>
                </xs:sequence>
    
            </xs:complexType>
        </xs:element>
        <xs:element name="borrower">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="section" maxOccurs="unbounded"/>
                    <xs:element name="code" type="xs:string"/>
                    <xs:element name="name" type="xs:string"/>
                </xs:sequence>
    
            </xs:complexType>
        </xs:element>
        <xs:element name="section">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="active-credits"/>
                    <xs:element ref="overdue-history"/>
                    <xs:element ref="new-credits"/>
                    <xs:element name="entity-type" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="active-credits">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="summaries" maxOccurs="unbounded"/>
                    <xs:element name="cred-count" type="xs:string"/>
                    <xs:element name="source-entity-count" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="summaries">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="summary" maxOccurs="unbounded"/>
                    <xs:element name="grouping-attribute" type="xs:string" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="summary">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="date-from" type="xs:string"/>
                    <xs:element name="type" type="xs:string"/>
                    <xs:element name="amount-approved" type="xs:string"/>
                    <xs:element name="amount-drawn" type="xs:string"/>
                    <xs:element name="monthly-installment" type="xs:string"/>
                    <xs:element name="outstanding-performing-principal" type="xs:string"/>
                    <xs:element name="outstanding-overdue-principal" type="xs:string"/>
                    <xs:element name="balance-sheet-value" type="xs:string"/>
                    <xs:element name="off-balance-sheet-value" type="xs:string"/>
                    <xs:element name="overdue-payment-period" type="xs:string"/>
                    <xs:element name="rest" type="xs:string"/>
                    <xs:element name="term" type="xs:string"/>
                    <xs:element name="active" type="xs:string"/>
                    <xs:element name="year" type="xs:string"/>
                    <xs:element name="category" type="xs:string"/>
                    <xs:element name="months-count" type="xs:string"/>
                    <xs:element name="max-cred-count" type="xs:string"/>
                    <xs:element name="max-outstanding-overdue-principal" type="xs:string"/>
                    <xs:element name="max-outstanding-overdue-interest-and-others"
                                type="xs:string"/>
                    <xs:element name="max-off-balance-sheet-dues" type="xs:string"/>
                    <xs:element name="date-last-correction" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="overdue-history">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="summary" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="new-credits">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="summary" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    【讨论】:

    • 非常感谢您的快速响应,就像一个魅力:)
    猜你喜欢
    • 2016-07-12
    • 2017-08-14
    • 2014-06-05
    • 2022-01-08
    • 2021-09-05
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多