2017-12-14 1 views
0

XMLStreamReader를 사용하여 중첩 된 XML 파일을 비 정렬 화하려고합니다. 나는 다음과 같은 클래스를 만들었습니다XSD없이 XMLStreamReader를 사용하여 복잡한 중첩 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 
} 

을 이후 클래스 정의 내 XML 파일은 다음과 같다.

내가 태그에 진출하는 XMLStreamReader를 사용하고하고 나는 내 unmarshaler 코드를 쓰고 있어요 :

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

는 그러나, 나는 프로듀서 객체로 설정 null 값을 얻고있다. 내가 잘못하고있는 것이 있습니까? 간단한 XML 파일을 비 정렬화할 수 있지만이 중첩 수준은 나를 위해 문제를 만들고 있습니다. 누군가가 쉽게 할 수 있는지 또는 내 코드 스켈레톤에서 변경해야 할 사항을 제안 할 수 있습니까?

미리 감사드립니다.

+0

JAXB의 잘못된 이름 공간 때문입니까? –

+0

XML은'xmlns : producer = "http://example1.com/"네임 스페이스를 사용하지만 Producer 클래스는'namespace = "http://example.com/"' –

답변

0

당신이 잘못하고있는 것을 말하기가 약간 어렵습니다. 그러나 코드에 Producer을 만든 다음 모든 클래스가 괜찮은지 확인하기 위해 마샬링 및 언 마샬링을 제안합니다.

클래스가 괜찮 으면 마샬링/언 마샬링이 작동하면 producer 변수가 null이 아니어야합니다. 당신이 예외를 throw하지 않습니다 자바 8이 코드를 실행하면

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; 
    } 
} 

: 여기

이 운동을 같이하는 방법의 예입니다.

관련 문제