当前位置: 首页 > 知识库问答 >
问题:

JaxB解组错误:java.lang.RuntimeException:意外元素(URI:“”,本地:“code”)。所需元素为

陶裕
2023-03-14

Borrower类使用基于XSD的JaxB解析,使用JDK1.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:

html lang-html prettyprint-override"><?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个答案

金宣
2023-03-14

例如,正如您在Borrower类中所看到的,name和code是用@XMLattribute注释的,但您的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>
 类似资料: