0
votes

I am trying to map my xml to java classes. Xml comes from a third-party service. The structure is the same, but there may be a different prefix or namespace. XML:

<?xml version="1.0" encoding="UTF-8"?>
<xdms:container xmlns:xdms="http://www.namespace.com/RTS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xdms:uid="FHGHDFGDFJKGDFHG" xdms:version="3.2">
    <xdms:requisites>
        <xdms:documentKind>letter</xdms:documentKind>
        <xdms:classification>main</xdms:classification>
        <xdms:annotation>unknown</xdms:annotation>
    </xdms:requisites>
</>

My classes:

@XmlRootElement(name = "container")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
    private static final long serialVersionUID = 1L;

    @XmlElement(name = "requisites")
    private Requisites requisites;

    public Container() {
        super();
    }

    @Override
    public String toString() {
        return "Container{" +
                "requisites=" + requisites +
                '}';
    }
}

@XmlRootElement(name = "requisites")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requisites implements Serializable {
    private static final long serialVersionUID = 1L;

    private String documentKind;
    private String classification;
    private String annotation;

    public Requisites() {
        super();
    }

    @Override
    public String toString() {
        return "Requisites{" +
                "documentKind='" + documentKind + '\'' +
                ", classfication='" + classification + '\'' +
                ", annotation='" + annotation + '\'' +
                '}';
    }
}

and the main class in which I run parsing:

    JAXBContext jaxbContext;
    File xmlFile = new File("test.xml");
    try
    {
        jaxbContext = JAXBContext.newInstance(Container.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Container cont = (Container) jaxbUnmarshaller.unmarshal(xmlFile);

        System.out.println(cont);
    }
    catch (JAXBException e)
    {
        e.printStackTrace();
    }

And I get the error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container"). Expected elements are <{}container>,<{}requisites> 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:374) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)

UPD: I added nameSpace, the error is gone. But the fields of my objects are not filled with information. They are empty, although in xml they are filled with information

Container{requisites=Requisites{documentKind='null', classfication='null', annotation='null'}}

2

2 Answers

1
votes

You don't specify the namespace in your jaxb class

Something like that:

@XmlRootElement(name = "container", namespace = "http://www.namespace.com/RTS")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {

    @XmlElement(name = "requisites", namespace="http://www.namespace.com/RTS")
    private Requisites requisites;

}

you might need to add namespace to every element in Requisites as well.

@XmlRootElement(name = "requisites")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requisites implements Serializable {
    private static final long serialVersionUID = 1L;

    @XmlElement(name = "documentKind", namespace="http://www.namespace.com/RTS")
    private String documentKind;
    @XmlElement(name = "classification", namespace="http://www.namespace.com/RTS")
    private String classification;
    @XmlElement(name = "annotation", namespace="http://www.namespace.com/RTS")
    private String annotation;
1
votes

Use the namespace attribute in the @XmlRootElement as follows:

@XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")

In case, if it still throws the same error, you also have to specify the namespace attribute in your @XmlElement:

@XmlElement(name="requisites", namespace="http://www.namespace.com/RTS")

Your each attribute inside the Requisites tag should also contain the namespace within its @XmlElement annotation.