xml/json을 생성하기 위해 MOXy 및 동일한 도메인 객체 모델을 사용하고 있습니다. Marshalling 및 예상대로 json 출력을 생성하려면 unmarhsalling unmarshalling 후 모든 값을 사용하여 Java 개체를 얻을 수 있지만 xml 않습니다 unmarshalling Java 개체를 모든 예상 된 값 대신 null 값을 제공합니다. 마샬링은 잘 작동하지만. 다음은널 바인딩 된 객체를 반환하는 xml 바인딩에 MOxy 사용
도메인 개체 모델
@XmlType(propOrder = {"colour","model","transmission"})
public class BMW {
private String colour;
private String model;
private String transmission;
@XmlPath("Cars/BMW/colour/text()")
public void setColour(String colour){
this.colour = colour;
}
@XmlPath("Cars/BMW/model/text()")
public void setModel(String model){
this.model = model;
}
@XmlPath("Cars/BMW/transmission/text()")
public void setTransmission(String transmission){
this.transmission = transmission;
}
public String getColour(){
return this.colour;
}
public String getModel(){
return this.model;
}
public String getTransmission(){
return this.transmission;
}
}이 코드에서 볼 수 있듯이 정렬 화를
시험 방법 및 비 정렬 화
BMW bmw = new BMW();
bmw.setColour("white");
bmw.setModel("X6");
bmw.setTransmission("AUTO");
File fileXML = new File("/..../bmw.xml");
File fileJson = new File("/..../bmw.json");
XMLInputFactory xif = XMLInputFactory.newInstance();
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(BMW.class);
Marshaller m = jaxbContext.createMarshaller();
Unmarshaller um = jaxbContext.createUnmarshaller();
//===== XML
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(bmw, fileXML);
m.marshal(bmw, System.out);
StreamSource xml = new StreamSource(fileXML);
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
// xsr.nextTag();
// xsr.nextTag();
BMW bmwXML = (BMW)um.unmarshal(xsr,BMW.class).getValue();
//==== JSON
m.setProperty("eclipselink.json.include-root", false);
m.setProperty("eclipselink.media-type", "application/json");
um.setProperty("eclipselink.media-type", "application/json");
um.setProperty("eclipselink.json.include-root", false);
m.marshal(bmw, fileJson);
m.marshal(bmw, System.out);
StreamSource json = new StreamSource(fileJson)
BMW bmwJson = um.unmarshal(json, BMW.class).getValue();
} catch (JAXBException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
내가 해봤 이상 사용 XSR 코드를입니다 .NextTag()하지만 도움이되지 않았다.
따라서 bmwXML은 모두 null 값을 가지지 만 bmwJson은 정상적으로 작동합니다. 내가 뭘 잘못하고 있는지 모르겠다.
덕분에 많은 블 레즈는 두 솔루션은 날 위해 일했습니다. 빠른 답장을 보내 주셔서 감사합니다. –