# Unmarshalling multiple namespaces with jaxb



## Rakshan (20. Aug 2021)

I am working on a XML file with multiple naespaces and I am trying ot unmarshal it. I have looked into some concepts previously but have not yet met with the solution.

The XML file:

```
<?xml version="1.0" encoding="UTF-8"?>
<Registry xmlns="http://www.registar.com" xmlns:ms="http://www.registar.com/ScoreVariant">
    <Student>
        <FirstName>RP</FirstName>
        <Value>
            <ms:String>Pass</ms:String>
        </Value>
    </Student>
    <Student>
        <FirstName>SK</FirstName>
        <Value>
            <ms:Int>100</ms:Int>
        </Value>
    </Student>
</Registry>
```

The Registry, Student class:

```
@Data
@XmlRootElement(name="Registry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Registry {
    @XmlElement
    private List<Student> Student;
}
```


```
@Data
@XmlAccessorType(XmlAccessType.FIELD)
//@XmlType(namespace = "http://www.registar.com/Grad")
public class Student {
    @XmlElement
    private String FirstName;
    private Value value; // suggestions to achieve this with different namespaces
}
```

The package-info class:


```
@XmlSchema(
    namespace="http://www.registar.com",
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns={
            @XmlNs(prefix = "", namespaceURI = "http://www.registar.com"),
            @XmlNs(prefix = "ms",namespaceURI = "http://www.registar.com/Grad")
    }
)
@XmlAccessorType(XmlAccessType.FIELD)
import javax.xml.bind.annotation.*;
```

I have the main program which prints out the unmarshallede objects to the console in a XMLformat.
However with the above approach , i face an error :

```
unexpected element (URI: "http://www.registar.com", local: "Registry"). Expected elements are <{} Registry>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:744)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:257)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:124)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1149)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:574)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:556)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:168)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:518)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3078)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:836)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:541)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:170)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:209)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
    at Mainparser.main(Mainparser.java:20)
```

Could you provide me some insights and suggestions here?


----------

