具有多个名称空间的Jaxb Unmarshar

时间:2018-11-22 10:21:18

标签: java xml jaxb marshalling unmarshalling

我正在尝试解组具有多个名称空间的xml。 最终,我遇到了一些或其他例外情况。 您能为我提供以下XML的最佳解决方案吗? 我尝试使用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>

下面是我尝试过的Java代码

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);

0 个答案:

没有答案