【XML】将String格式的XML文件转化成JAVA实体类
程序员文章站
2024-02-03 20:32:28
...
1.问题:
如何将String类型的XML转化成JAVA实体类?
2.源代码:
public class TestXML {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String xmlStr="<object>\n" +
" <int name=\"start\">1</int>\n" +
" <string name=\"order\">receiveDate</string>\n" +
" <int name=\"desc\">1</int>\n" +
" <int name=\"fid\">1</int>\n" +
" <int name=\"norefreshSid\">0</int>\n" +
" <int name=\"total\">20</int>\n" +
" <string name=\"sessionEnable\">0</string>\n" +
"</object>";
System.out.println(xmlStr);
/*JAXBContext应用入口,用于xml和java的绑定*/
JAXBContext context=null;
try {
context=JAXBContext.newInstance(Objectz.class);
/*Unmarshaller接口,将xml反序列化为java对象*/
Unmarshaller unmarshaller= context.createUnmarshaller();
System.out.println(unmarshaller);
StringReader stringReader=new StringReader(xmlStr);
System.out.println(stringReader);
System.out.println("unmarshaller:"+unmarshaller.unmarshal(stringReader));
Objectz objectz=(Objectz)unmarshaller.unmarshal(stringReader);
System.out.println(objectz);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
3.截图:
4.总结:
这里说,不需要元素,但是却获得了本地却获得了object;请问怎么解决这个异常?
附:报错代码:
javax.xml.bind.UnmarshalException: 意外的元素 (uri:"", local:"object")。所需元素为(none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:614)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3135)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
at com.fxrj.email.test.TestXML.main(TestXML.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
上一篇: 一个可以显示当前年份以及生肖的js代码
推荐阅读
-
【XML】将String格式的XML文件转化成JAVA实体类
-
请教PHP怎样将XML文件读入JSON格式数据,并,JS怎样将该格式的数据进行解析
-
将Java对象序列化成JSON和XML格式的实例
-
java将XML文档转换成json格式数据的示例
-
Python批量将csv文件转化成xml文件的实例
-
解决python将xml格式文件转换成txt文件的问题(xml.etree方法)
-
java中 使用jdom 解析和生成循环嵌套的xml格式文件
-
【工具脚本】将VOC格式XML标注文件的目标裁剪为小图保存下来
-
将COCO格式的json文件转化为VOC格式的xml文件
-
如何把xml文件转化成格式化的pandas数据