hunterhacker / jdom

Java manipulation of XML made easy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove Duplicate Namespaces from XML in Java Ask

mhaseebkhan opened this issue · comments

I have the following soap response as a sample:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:mycompany:Service:2" xmlns:urn1="urn:mycompany:Customer:2">
  <soapenv:Header />
  <soapenv:Body>
    <urn:GetResponse>
      <urn:StatusCode>002</urn:StatusCode>
      <urn:StatusMessage>Pass</urn:StatusMessage>
      <urn:CustomerAffiliations>
        <urn:CustomerAffiliation>
          <urn:CustomerID>II39642</urn:CustomerID>
          <urn:CustomerContactDetails>
            <ns3:Channel xmlns:ns3="urn:mycompany:Customer:2">Business Phone</ns3:Channel>
            <ns3:Value xmlns:ns3="urn:mycompany:Customer:2">5553647</ns3:Value>
          </urn:CustomerContactDetails>
        </urn:CustomerAffiliation>
      </urn:CustomerAffiliations>
    </urn:GetResponse>
  </soapenv:Body>
</soapenv:Envelope>

urn:mycompany:Customer:2 has been included as urn1 in soapenv:Envelope but it is duplicated in ns3:Channel and ns3:Value.

The requirement is to clean the xml content so the correct namespaces declared in soapenv:Envelope is used in the child elements.

Is there a way in Java to clean/normalize this xml content and use proper namespace usage and duplication removal?

Answered on StackOverflow: http://stackoverflow.com/a/42724237/1305253

public static final void dedupElementNamespaces(Element node) {
    List<Namespace> created = node.getNamespacesIntroduced();
    if (!created.isEmpty()) {
        // check anything new against other stuff...
        List<Namespace> inherited = node.getNamespacesInherited();
        // check out element against previous declarations....
        if (node.getNamespace().getPrefix() != "") {
            // never swap defaulted namespaces to anything with a prefix.
            Namespace ens = node.getNamespace();
            Namespace use = findFirst(inherited, node.getNamespaceURI());
            if (use != null && use != ens) {
                node.setNamespace(use);
            }
        }           

    }
    for (Element e : node.getChildren()) {
        dedupElementNamespaces(e);
    }
}

Any idea on the implementation of findFirst method? Whats the method doing?