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>
例如,正如您在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>
在这里,我将从XML创建java对象:
有几个关于这个问题的帮助主题,但我还没有找到一个解决我的问题的解决方案。我很欣赏在解决这个问题上的指导。 例外情况
我试图解封XML文件。我创建了Jaxb类,但是当我试图解封时,它会给出: 这是我的ObjectFactory.java类: 你能请帮助,因为我被卡住了,不能提供我的客户开发这个功能的延迟原因。 谢谢,Jayesh Jain
Axis2生成一个stub类,它包含所有的getters、setters和schema中的选项中的类。我使用该类并调用方法,如下所示: 然后我得到上面张贴的错误。为什么会出现这种情况?我的代码错了吗?
我需要使用xml绑定来解组映射是错误的。 客户端代码: 错误消息: