我无法使用JAXB来读取带有命名空间(ns2)的属性来解析Java中的XML

时间:2017-06-18 15:15:05

标签: java xml namespaces jaxb ns2

我必须从这个XML文件中读取数据:

<SENT_112 xmlns:ns2="http://www.mf.gov.pl/SENT/2017/01/18/STypes.xsd" xmlns="http://www.mf.gov.pl/SENT/2017/01/18/SENT_112.xsd">
<Carrier><ns2:TraderInfo><ns2:IdSisc>PL957271726800000</ns2:IdSisc><ns2:TraderName>FIRMA SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ</ns2:TraderName>
<ns2:TraderIdentityType>NIP</ns2:TraderIdentityType>
<ns2:TraderIdentityNumber>9572717268</ns2:TraderIdentityNumber>
</ns2:TraderInfo>
<ns2:TraderAddress><ns2:Street>Arysztacka</ns2:Street><ns2:HouseNumber>91A</ns2:HouseNumber><ns2:City>Cieszyn</ns2:City><ns2:Country>PL</ns2:Country><ns2:PostalCode>43-400</ns2:PostalCode></ns2:TraderAddress></Carrier>
</SENT_112>

我的主要课程是使用JAXB:

        JAXBContext jc = JAXBContext.newInstance(SENT_112.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SENT_112 sent_112 = (SENT_112) unmarshaller.unmarshal(xml);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(sent_112, System.out);

Carrier.java

public class Carrier implements java.io.Serializable {
     private int nrCarrier;
     //private String sentnumber;
     private String IdSisc;
     private String tradername;
     private String traderidentitytype;
     private String traderidentitynumber;
     private String street;
     private String housenumber;
     private String city;
     private String country;
     private String postalcode;
//Contrutors
//Getters and Setters 
//ToString Method 

SENT_112.java

@XmlRootElement(name="SENT_112")
public class SENT_112 {
    @XmlElement(name="Carrier")
    private List<Carrier> carrier;
    public List<Carrier> getCarrier() {
        return carrier;
    } 

PACKAGE-INFO.JAVA

@XmlSchema(
    namespace = "http://www.mf.gov.pl/SENT/2017/01/18/SENT_112.xsd",
    elementFormDefault = XmlNsForm.QUALIFIED)
package com.przedlak.entity;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

我的代码返回

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SENT_112 xmlns="http://www.mf.gov.pl/SENT/2017/01/18/SENT_112.xsd">
    <Carrier>
        <nrCarrier>0</nrCarrier>
    </Carrier>
</SENT_112>

我的代码有什么问题?我需要阅读所有信息。

1 个答案:

答案 0 :(得分:0)

如果您想获取所有信息,请适当地编写您的类,即检索所有数据。将Carrier.java更改为:

<强> Carrier.java

@XmlRootElement(name="Carrier")
@XmlAccessorType(XmlAccessType.FIELD)
public class Carrier  implements java.io.Serializable 
{
    @XmlElement( name = "TraderInfo" )
    private TraderInfo traderInfo;

    @XmlElement( name = "TraderAddress" )
    private TraderAddress traderAddress;

    private int nrCarrier;

    public int getNrCarrier() {
        return nrCarrier;
    }

    public void setNrCarrier(int nrCarrier) {
        this.nrCarrier = nrCarrier;
    }

    public TraderInfo getTraderInfo() {
        return traderInfo;
    }

    public void setTraderInfo(TraderInfo traderInfo) {
        this.traderInfo = traderInfo;
    }

    public TraderAddress getTraderAddress() {
        return traderAddress;
    }

    public void setTraderAddress(TraderAddress traderAddress) {
        this.traderAddress = traderAddress;
    }
}

然后为TraderAddressTraderInfo定义两个类:

<强> TraderInfo

@XmlRootElement(name="TraderInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class TraderInfo
{
    @XmlElement( name = "IdSisc")
    private String IdSisc;

    @XmlElement( name = "TraderName")
    private String tradername;

    @XmlElement( name = "TraderIdentityType")
    private String traderidentitytype;

    @XmlElement( name = "TraderIdentityNumber")
    private String traderidentitynumber;

    public String getIdSisc() {
        return IdSisc;
    }

    public void setIdSisc(String idSisc) {
        IdSisc = idSisc;
    }

    public String getTradername() {
        return tradername;
    }

    public void setTradername(String tradername) {
        this.tradername = tradername;
    }

    public String getTraderidentitytype() {
        return traderidentitytype;
    }

    public void setTraderidentitytype(String traderidentitytype) {
        this.traderidentitytype = traderidentitytype;
    }

    public String getTraderidentitynumber() {
        return traderidentitynumber;
    }

    public void setTraderidentitynumber(String traderidentitynumber) {
        this.traderidentitynumber = traderidentitynumber;
    }

}

<强> TraderAddress.java

@XmlRootElement(name="TraderInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class TraderAddress
{


    @XmlElement( name = "Street")
    private String street;

    @XmlElement( name = "HouseNumber")
    private String houseNumber;

    @XmlElement( name = "City")
    private String city;

    @XmlElement( name = "Country")
    private String country;

    @XmlElement( name = "PostalCode")
    private String postalCode;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

}

使用此设置,您应该从编组中获取完整的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SENT_112 xmlns="http://www.mf.gov.pl/SENT/2017/01/18/SENT_112.xsd">
    <Carrier>
        <TraderInfo>
            <IdSisc>PL957271726800000</IdSisc>
            <TraderName>FIRMA SPÓÅ?KA Z OGRANICZONÄ„ ODPOWIEDZIALNOÅšCIÄ„
            </TraderName>
            <TraderIdentityType>NIP</TraderIdentityType>
            <TraderIdentityNumber>9572717268</TraderIdentityNumber>
        </TraderInfo>
        <TraderAddress>
            <Street>Arysztacka</Street>
            <HouseNumber>91A</HouseNumber>
            <City>Cieszyn</City>
            <Country>PL</Country>
            <PostalCode>43-400</PostalCode>
        </TraderAddress>
        <nrCarrier>0</nrCarrier>
    </Carrier>
</SENT_112>