我想使用JaxB解組一個基本的XML文件,但是代碼有問題。編組器正確運行,但解組器不是返回XML文件中的內容,而是返回[email protected]
。 (班級的名字後跟一個'@',隨機組合了字母和數字)。這是下面的代碼。感謝您的幫助或想法。JaxB Unmarshaller錯誤
XML註釋類:
@XmlRootElement
public class Jaxb {
String newString;
public String getNewString() {
return newString;
}
@XmlElement
public void setNewString(String newString) {
this.newString = newString;
}
}
的Marshaller:
public class Marshal {
Jaxb newWindow = new Jaxb();
String xmlString;
void marshal(String[] args) {
xmlString="a,b,c";
newWindow.setNewString(xmlString);
try {
File file = new File("newXml.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Jaxb.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(newWindow, file);
jaxbMarshaller.marshal(newWindow, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
的Unmarshaller:
public class unmarshal {
static String unMarshal() {
String unmarshString="";
try {
File x = new File("newXml.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Jaxb.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Jaxb newUnmarshal = (Jaxb) jaxbUnmarshaller.unmarshal(x);
unmarshString = newUnmarshal.toString();
} catch (JAXBException e) {
e.printStackTrace();
System.out.print("error");
}
return unmarshString;
}
}
謝謝你的幫助和信息! –