代码之家  ›  专栏  ›  技术社区  ›  PT_C

JDK11-如何在解组期间忽略名称空间

  •  0
  • PT_C  · 技术社区  · 4 年前

    我试图编写一个Java11XML解组器,在将名称空间数据强制转换到对象之前将其剥离,但我得到了以下错误。应用程序需要删除前面的内容 app: 从种姓之前的所有元素到对象。

    使用JDK11如何做到这一点?

    错误

    javax.xml.bind.UnmarshalException
     - with linked exception:
    [com.sun.istack.SAXParseException2; lineNumber: 2; columnNumber: 67; unexpected element (uri:"", local:"app:exampleXML"). Expected elements are <{http://www.example.com/schemas/app}object>]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:453)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:387)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:356)
        XmlUnmarshaller.unmarshal(XmlUnmarshaller.java:36)
        ...
        at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: com.sun.istack.SAXParseException2; lineNumber: 2; columnNumber: 67; unexpected element (uri:"", local:"app:exampleXML"). Expected elements are <{http://www.example.com/schemas/app}object>
        ...
        ... 20 more
    Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"app:exampleXML"). Expected elements are <{http://www.example.com/schemas/app}object>
        ... 31 more
    

    测验

    public void testUnmarshal() throws JAXBException, XMLStreamException {
    
            String xml3 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                    "<app:exampleXML xmlns:app=\"http://www.example.com/schemas/app\">\n" +
                    "<app:id>app-id</app:id>\n" +
                    "<app:time>2020-06-05T13:17:00.899Z</app:time>\n" +
                    "<app:type>test</app:type>\n" +
                    "<app:description>Test</app:description>\n" +
                    "</app:exampleXML>";
    
            XmlUnmarshaller unmarshaller = new XmlUnmarshaller();
            Object object = unmarshaller.unmarshal(xml3, Object.class);
    
            System.out.println(object);
    }
    

    破译者

    public <T> T unmarshal(String xml, Class<T> clazz) throws JAXBException, XMLStreamException {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            XMLInputFactory xif = XMLInputFactory.newFactory();
            xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
            StreamSource source = new StreamSource(new StringReader(xml));
            XMLStreamReader xsr = xif.createXMLStreamReader(source);
            return clazz.cast(unmarshaller.unmarshal(xsr));
    }
    

    对象

    @XmlRootElement(name = "exampleXML", namespace="http://www.example.com/schemas/app")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class XML {
        public static final int DESCRIPTION_LENGTH = 4000;
    
        @XmlElement(name = "id", required = true)
        private String id;
    
    
        @XmlElement(name = "time", required = true)
        @XmlJavaTypeAdapter(InstantXmlAdapter.class)
        @JsonSerialize(using = InstantJsonSerializer.class)
        @JsonDeserialize(using = InstantJsonDeserializer.class)
        private Instant time;
    
        @XmlElement(name = "type", required = true)
        private CheckpointLevel type;
    
        @XmlElement(name = "description")
        @XmlJavaTypeAdapter(CDATAXmlAdapter.class)
        private String description;
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   PT_C    4 年前

    补充 包裹信息。JAVA 添加到域对象并添加前缀

    @XmlSchema(
            namespace = "http://www.example.com/schemas/app",
            elementFormDefault = XmlNsForm.QUALIFIED,
            xmlns = {
                    @XmlNs(prefix = "app", namespaceURI = "http://www.example.com/schemas/app")
            }
    )
    package com.app.domainObjects;
    
    import javax.xml.bind.annotation.XmlNs;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;