簡體   English   中英

使用不帶XSD的XMLStreamReader解組復雜的嵌套XML文件

[英]Unmarshalling Complex Nested XML file using XMLStreamReader without XSD

我正在嘗試使用XMLStreamReader解組嵌套的XML文件。 我的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<tns:Envelope
    xmlns:tns="http://www.w3.org/2003/05/soap-envelope-dial"
    xmlns:lmic="http://www.example.com"
    xmlns:producer="http://example1.com/"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
    xmlns:ns5="http://www.example.com/dial/3/0">
<tns:header>
...
...
</tns:header>
<tns:body>
<producer:Producer id="1234">
   <producer:GenParty>
     <producer:NameInfo>
        <producer:Comm>
           <producer:SuppName>DATA</producer:SuppName>
           <producer:ContractNumber>123456</producer:ContractNumber>
        </producer:Comm>
    </producer:NameInfo>
    <producer:Address>
      <Street>ABC</Street>
      <Country>DEF</Country>
       ...
       ...
    </prodcer:Address>
    <producer:Address>
      <Street>ABC</Street>
      <Country>DEF</Country>
       ...
       ...
    </prodcer:Address>
  </producer:GenParty>
</producer:Producer>
</tns:body>
</tns:emvelope>

我創建了類似以下的類:

@XmlRootElement(name="Producer",namespace="http://example.com/")
@XmlAccessorType(XmlAccessType.FIELD)
Class Producer {
    private GenParty;
    // getter method of class GenParty
    // setter method of class GenParty
}

@XmlRootElement(name="GenParty")
@XmlAccessorType(XmlAccessType.FIELD)
class GenParty {
    private NameInfo;
    private List<Address> address;
    //getter of both fields
    // setter of both fields
}

並定義了后續類。

我正在使用XMLStreamReader前進到標簽,然后將我的編組器代碼編寫為:

JAXBContext jc = JAXBContext.newInstance(Producer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();                
Producer producer = unmarshaller.unmarshal(xsr,Producer.class).getValue();

但是,我將在Producer對象上設置為null值。 我做錯了什么嗎? 我可以解組簡單的XML文件,但是這種嵌套層次給我帶來了問題。 有人可以建議這樣做容易還是我應該在代碼框架中進行任何更改?

在此先多謝!

很難說你在做什么錯。 但是我建議您用代碼創建一個Producer ,然后進行編組和解組檢查以檢查您的所有類是否都可以。

如果這些類都正常並且可以編組/解組,則producer變量絕不能為null。

這是此練習的示例:

import com.sun.xml.internal.ws.streaming.DOMStreamReader;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

public class JAXBTester {

    public static void main(String[] args) throws JAXBException, ParserConfigurationException, IOException, SAXException {
        JAXBContext jc = JAXBContext.newInstance(Producer.class);
        Marshaller marshaller = jc.createMarshaller();
        Producer producer = createProducer();
        String producerStr = marshalproducer(marshaller, producer);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(producerStr.getBytes("UTF-8")));
        XMLStreamReader xmlStreamReader = new DOMStreamReader(document);
        Producer readProducer = unmarshaller.unmarshal(xmlStreamReader, Producer.class).getValue();
        if (readProducer == null) {
            throw new IllegalStateException();
        }
    }

    private static String marshalproducer(Marshaller marshaller, Producer producer) throws JAXBException {
        StringWriter writer = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(producer, writer);
        String res = writer.toString();
        System.out.println(res);
        return res;
    }

    private static Producer createProducer() {
        Producer producer = new Producer();
        GenParty genParty = new GenParty();
        producer.setGenParty(genParty);
        NameInfo nameInfo = new NameInfo();
        nameInfo.setInfo("Foo");
        genParty.setNameInfo(nameInfo);
        return producer;
    }

}

@XmlRootElement(name = "Producer", namespace = "http://example.com/")
@XmlAccessorType(XmlAccessType.FIELD)
class Producer {
    private GenParty genParty;

    public GenParty getGenParty() {
        return genParty;
    }

    public void setGenParty(GenParty genParty) {
        this.genParty = genParty;
    }
}

@XmlRootElement(name = "GenParty")
@XmlAccessorType(XmlAccessType.FIELD)
class GenParty {
    private NameInfo nameInfo;
    private List<Address> address;

    public NameInfo getNameInfo() {
        return nameInfo;
    }

    public void setNameInfo(NameInfo nameInfo) {
        this.nameInfo = nameInfo;
    }

    public List<Address> getAddress() {
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }
}

class NameInfo {
    private String info;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

class Address {

    private String street;

    public String getStreet() {
        return street;
    }

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

如果使用Java 8執行此代碼,則不會引發任何異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM