0

I am trying to Unmarshal a xml with multiple name spaces. Eventually i am getting some or other exception. Can you please guide me with best suitable solution for the below xml I have tried using Jaxb Marshaller

<?xml version="1.0" encoding="utf-8"?>
<entry  xmlns="http://www.w3.org/2005/Atom"
       xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
       xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
       xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:etag="&quot;2&quot;">
    <id>Web/Lists(guid'72cbceb5-a38b-4fdc-a37b-550794d3a78a')/Items(99)</id>
    <category term="SP.Data.VDI_x0020_Build_x0020_Release_x0020_ScheduleListItem"
              scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link rel="edit" href="Web/Lists(guid'72cbceb5-a38b-4fdc-a37b-550794d3a78a')/Items(99)"/>
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FirstUniqueAncestorSecurableObject"
          type="application/atom+xml;type=entry" title="FirstUniqueAncestorSecurableObject"
          href="Web/Lists(guid'72cbceb5-a38b-4fdc-a37b-550794d3a78a')/Items(99)/FirstUniqueAncestorSecurableObject"/>
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RoleAssignments"
          type="application/atom+xml;type=feed" title="RoleAssignments"
          href="Web/Lists(guid'72cbceb5-a38b-4fdc-a37b-550794d3a78a')/Items(99)/RoleAssignments"/>
    <title/>
    <updated>2018-11-22T09:24:14Z</updated>
    <author>
        <name/>
    </author>
    <content type="application/xml">
        <m:properties>
            <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
            <d:Id m:type="Edm.Int32">99</d:Id>
            <d:Build_x0020_Release>201807</d:Build_x0020_Release>
            <d:Title>1.52.201807</d:Title>
            <d:Platform>LVDI3 Build (Office 2010)</d:Platform>
            <d:Phase>VDI Engineering</d:Phase>
            <d:Planned_x0020_Start m:type="Edm.DateTime">2018-08-03T04:00:00Z</d:Planned_x0020_Start>
            <d:Planned_x0020_End m:type="Edm.DateTime">2018-08-10T04:00:00Z</d:Planned_x0020_End>

        </m:properties>
    </content>
</entry>

Below is the Java Code which i have tried

private Map<String, String> namespaceMap = new HashMap<>();

/**
 * Create mappings.
 */
public DefaultNamespacePrefixMapper() {
    namespaceMap.put("http://www.w3.org/2005/Atom", "");
    namespaceMap.put("http://schemas.microsoft.com/ado/2007/08/dataservices", "d");
    namespaceMap.put("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", "m");
    namespaceMap.put("http://www.georss.org/georss", "georss");
    namespaceMap.put("http://www.opengis.net/gml", "gml");
}

@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
    return namespaceMap.getOrDefault(namespaceUri, suggestion);

}


DefaultNamespacePrefixMapper defaultNamespacePrefixMapper = new DefaultNamespacePrefixMapper();

    System.out.println("Response is::::"+response);
    JAXBContext jaxbContext =JAXBContext.newInstance(Entry.class);

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new DefaultNamespacePrefixMapper());
    Object sharePointResponse = unmarshaller.unmarshal(inputStream);
Sonu Agarwal
  • 97
  • 2
  • 12
  • Would you mind to post exception which you're getting? – Abhinav Nov 22 '18 at 10:52
  • @Abhinav Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"entry"). Expected elements are (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) – Sonu Agarwal Nov 22 '18 at 12:22
  • Possible duplicate of [javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none)](https://stackoverflow.com/questions/20586737/javax-xml-bind-unmarshalexception-unexpected-element-expected-elements-are-no) – Abhinav Nov 22 '18 at 13:05

0 Answers0