gpt4 book ai didi

java - 没有注释的 JAXB Unmarshal

转载 作者:行者123 更新时间:2023-11-30 08:24:15 25 4
gpt4 key购买 nike

我正在尝试解码一个没有注释的编码对象。

这是我的对象类。

class Student {
private String name;
private int age;

public Student() {
}

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}

这就是我用来编码对象的。

public String marshall(Student student){
StringWriter writer = new StringWriter();
JAXBContext context;
try {
context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();

m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<Student> rootElement = new JAXBElement<Student>(new QName("student"), Student.class, student);
m.marshal(rootElement, writer);
return writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}

这就是我用来解码对象的方法。

public Student unMarshall(String input){
JAXBContext context;
try {
context = JAXBContext.newInstance(Student.class);
Unmarshaller m = context.createUnmarshaller();
return (Student) m.unmarshal(new StringReader(input));
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}

当我尝试解码时,出现以下错误

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"student"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:648)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1051)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:484)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at noannotation.Main.unMarshel(Main.java:49)
at noannotation.Main.main(Main.java:22)

我该如何解决这个错误?

最佳答案

您只需要使用带有Class 参数的unmarshal 方法之一。

public Student unMarshall(String input){
JAXBContext context;
try {
context = JAXBContext.newInstance(Student.class);
Unmarshaller m = context.createUnmarshaller();
StreamSource source = new StreamSource(new StringReader(input))
return m.unmarshal(source, Student.class).getValue();
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}

了解更多信息

您可以在我的博客中找到有关此用例的更多信息:

关于java - 没有注释的 JAXB Unmarshal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com