欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【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.截图:

【XML】将String格式的XML文件转化成JAVA实体类

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)