JaxB Unmarshall错误:java.lang.RuntimeException:意外元素(uri:“”,local:“code”)。预期元素是< {} section>

时间:2018-02-21 13:36:04

标签: java jaxb

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

java.lang.RuntimeException:意外元素(uri:“”,local:“code”)。预期元素为< {} section>

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

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>

1 个答案:

答案 0 :(得分:3)

问题是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更改为下面的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>
相关问题