我想使用MOXy JAXB解组一个XML文件。我有一组已经生成的类,我使用Xpath将我需要的每个XML元素映射到我的模型中。MOXY JAXB:如何将几个XML标记元素映射到相同的JAVA bean属性
我有一个这样的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<fe:Facturae xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:fe="http://www.facturae.es/Facturae/2009/v3.2/Facturae">
<Parties>
<SellerParty>
<LegalEntity>
<CorporateName>Company Comp SA</CorporateName>
<TradeName>Comp</TradeName>
<ContactDetails>
<Telephone>917776665</Telephone>
<TeleFax>917776666</TeleFax>
<WebAddress>www.facturae.es</WebAddress>
<ElectronicMail>[email protected]</ElectronicMail>
<ContactPersons>Fernando</ContactPersons>
<CnoCnae>28000</CnoCnae>
<INETownCode>2134AAB</INETownCode>
<AdditionalContactDetails>Otros datos</AdditionalContactDetails>
</ContactDetails>
</LegalEntity>
</SellerParty>
<BuyerParty>
<Individual>
<Name>Juana</Name>
<FirstSurname>Mauriño</FirstSurname>
<OverseasAddress>
<Address>Juncal 1315</Address>
<PostCodeAndTown>00000 Buenos Aires</PostCodeAndTown>
<Province>Capital Federal</Province>
<CountryCode>ARG</CountryCode>
</OverseasAddress>
<ContactDetails>
<Telephone>00547775554</Telephone>
<TeleFax>00547775555</TeleFax>
</ContactDetails>
</Individual>
</BuyerParty>
</Parties>
</fe:Facturae>
然后,我有我的模型:
@XmlRootElement(namespace="http://www.facturae.es/Facturae/2009/v3.2/Facturae", name="Facturae")
public class Facturae implements BaseObject, SecuredObject, CreationDataAware {
@XmlPath("Parties/SellerParty")
private Party sellerParty;
@XmlPath("Parties/BuyerParty")
private Party buyerParty;
}
public class Party implements BaseObject, SecuredObject, CreationDataAware {
@XmlPath("LegalEntity/ContactDetails")
private ContactDetails contactDetails;
}
正如你所看到的,<ContactDetails></ContactDetails>
存在于<SellerParty></SellerParty>
和<BuyerParty></BuyerParty>
但是这两个标记共享相同的JAVA对象(Party)。通过之前的映射(@XmlPath(“LegalEntity/ContactDetails”)),我可以正确传递SellerParty中的ContactDetails信息,但我还想同时传递<BuyerParty>
中的ContactDetails。
我是想这样的事情:
@XmlPaths(value = { @XmlPath("LegalEntity/ContactDetails"),@XmlPath("Individual/ContactDetails") })
private ContactDetails contactDetails;
,但它不工作。
你们能帮我一把吗?
非常感谢。
非常感谢,这是完美的! – rocotocloc 2012-03-22 11:17:20