编组问题:JAVA || JAXB || XMLRootElement

时间:2018-12-12 14:50:06

标签: java jaxb unmarshalling

我无法将xml解组为对象。 但是,如果完成以下更改,那么我可以编组||。但是我不能那样做,因为它不可接受,POJO无法更改。

POJO课:

        @XmlRootElement
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "pu.rci.outgoing")
        public class purciOutgoing {
            @XmlElement(name = "pu.rci.header", required = true)
            protected int header;
            @XmlElement(name = "pu.rci.body", required = true)
            protected String body;
            @XmlElement(name = "pu.rci.parameters", required = true, nillable= true)
            protected int Parameters;
            }


   // Now, My unmarshalling method, Obvio in some main method of some class

    String xml = "<pu.rci.outgoing>\r\n" + 
                "   <pu.rci.header>1</pu.rci.header>\r\n" + 
                "   <pu.rci.body>BI822G</pu.rci.body>\r\n" + 
                "   <pu.rci.parameters>3</pu.rci.parameters>\r\n" + 
                "</pu.rci.outgoing>";

    purciOutgoing req = new purciOutgoing();
    JAXBContext jaxbContext = JAXBContext.newInstance(purciOutgoing.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = new StringReader(xml);
    Object obj = null;
    obj = jaxbUnmarshaller.unmarshal(reader);
    reader.close();
    req = (purciOutgoing) obj;

/*
@XmlRootElement(name = "pu.rci.outgoing")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
*/

错误:ERROR XmlMarshaller:120-意外元素(uri:“”,本地:“ pu.rci.outgoing”)。预期元素为<{} purciOutgoing> javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“ pu.rci.outgoing”)。预期元素为<{} purciOutgoing>     在com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:740)〜[jaxb-impl.jar:2.2.10-b140802.1033]

0 个答案:

没有答案