10

I want to remove namespace from XML in Java. Can you pls guide on what needs to be done. Can use DOM parser but that would be a node by node parsing. I want to know if there is some code wherein all the namespace(mig:) can be removed from the entire XML.

My XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mig:menu-compare xmlns:mig="http://www..com/migration/" xmlns:xsi="http://www..org/2001/XMLSchema-instance" xsi:schemaLocation="http://www..com/migration">
    <mig:menu-info>
        <mig:menu type="ons" name="HRDM"/>
        <mig:menu type="ux" name="ARD"/>
    </mig:menu-info>
    <mig:field-details>
        <mig:fields existence="nonMap">
            <mig:field>
                <mig:field-type type="ons">
                    <mig:ui-field>funcCode</mig:ui-field>
                    <mig:label>FLT000204</mig:label>
                    <mig:label-desc>Function</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
            </mig:field>
            <mig:field>
                <mig:field-type type="ux">
                    <mig:ui-field>submit</mig:ui-field>
                    <mig:label>FBT000000</mig:label>
                    <mig:section-structure></mig:section-structure>
                    <mig:form></mig:form>
                </mig:field-type>
            </mig:field>
            <mig:field>
                <mig:field-type type="ux">
                    <mig:ui-field>cancel</mig:ui-field>
                    <mig:label>FBT000001</mig:label>
                    <mig:section-structure></mig:section-structure>
                    <mig:form></mig:form>
                </mig:field-type>
            </mig:field>
        </mig:fields>
        <mig:fields existence="both">
            <mig:field name="rptDfnMsg.rptDfnInfo.gprRptNum">
                <mig:field-type type="ons">
                    <mig:control-type>Text Field</mig:control-type>
                    <mig:ui-field>rptNum</mig:ui-field>
                    <mig:label>FLT006718</mig:label>
                    <mig:label-desc>Report No.</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
                <mig:field-type type="ux">
                    <mig:control-type>FinTextInputWithSearcher</mig:control-type>
                    <mig:ui-field>reportNo</mig:ui-field>
                    <mig:label>FLT005821</mig:label>
                    <mig:label-desc>Report No.</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
            </mig:field>
            <mig:field name="rptDfnMsg.rptDfnInfo.gprRptDesc">
                <mig:field-type type="ons">
                    <mig:control-type>Desc. Label</mig:control-type>
                    <mig:ui-field>rptDesc</mig:ui-field>
                    <mig:label></mig:label>
                    <mig:mandatory>N</mig:mandatory>
                </mig:field-type>
                <mig:field-type type="ux">
                    <mig:control-type>FinTextInput</mig:control-type>
                    <mig:ui-field>desc</mig:ui-field>
                    <mig:label>FLT000690</mig:label>
                    <mig:label-desc>Description</mig:label-desc>
                </mig:field-type>
            </mig:field>

        </mig:fields>
        <mig:fields existence="ons">
            <mig:field name="rptDfnMaster.gprRptDesc">
                <mig:field-type type="ons">
                    <mig:ui-field>rptDesc</mig:ui-field>
                    <mig:label>FLT002771</mig:label>
                    <mig:label-desc>Description</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
            </mig:field>
            <mig:field name="rptDfnMaster.gprRptType.code">
                <mig:field-type type="ons">
                    <mig:ui-field>rptType</mig:ui-field>
                    <mig:label>FLT007124</mig:label>
                    <mig:label-desc>Report Type</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
            </mig:field>

        </mig:fields>
        <mig:fields existence="ux">
            <mig:field name="rptDfnMsg.rptDfnInfo.gprRptType.code">
                <mig:field-type type="ux">
                    <mig:ui-field>reporttype</mig:ui-field>
                    <mig:label>FLT005818</mig:label>
                    <mig:label-desc>Report Type</mig:label-desc>
                    <mig:mandatory>Y</mig:mandatory>
                </mig:field-type>
            </mig:field>
            <mig:field name="rptDfnMsg.rptDfnInfo.gprRptConsolFlg">
                <mig:field-type type="ux">
                    <mig:control-type>FinComboBox</mig:control-type>
                    <mig:ui-field>printRep</mig:ui-field>
                    <mig:label>FLT011541</mig:label>
                </mig:field-type>
            </mig:field>
         </mig:fields>
    </mig:field-details>
</mig:menu-compare>
6
  • Have you tried a simple String replacement with regular expressions? Commented Jul 30, 2014 at 9:33
  • Would string.removeAll("mig:") be an option? Commented Jul 30, 2014 at 9:34
  • 2
    Stop suggesting regex for tasks like that, nothing good will come out of this: stackoverflow.com/questions/1732348/… Commented Jul 30, 2014 at 9:37
  • Use an XML parser or XSLT to remove namespaces. There are a lot of options out there and the right choice depend on a number of factors we can not determine for you. Commented Jul 30, 2014 at 9:42
  • 1
    Why? It's legal XML. If you're using standard XML tools they should all understand it, and if you're not you should be. One obvious solution would be to use XSLT and declare the namespace as the default. But the purpose remains obscure. Commented Jul 30, 2014 at 9:49

2 Answers 2

13

You can use xslt for that. Try

removeNs.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="*">
        <xsl:element name="{local-name(.)}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="." />
    </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

Sample.java

import java.io.File;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Sample {

    public static void main(String[] args) {
        try{
            TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File("removeNs.xslt"));
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new File("data.xml"));
        transformer.transform(text, new StreamResult(new File("output.xml")));
        System.out.println("Done");
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

Regex can be used for more information refer this

public static string RemoveAllXmlNamespace(string xmlData)
        {
            string xmlnsPattern = "\\s+xmlns\\s*(:\\w)?\\s*=\\s*\\\"(?<url>[^\\\"]*)\\\"";
            MatchCollection matchCol = Regex.Matches(xmlData, xmlnsPattern);

            foreach (Match m in matchCol)
            {
                xmlData = xmlData.Replace(m.ToString(), "");
            }
            return xmlData;
        }
   }

You can find a similar example here

Regex can be painful. you can also use this api (dom) to get rid of all namespaces.refer this

 import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    ...

    /**
     * Recursively renames the namespace of a node.
     * @param node the starting node.
     * @param namespace the new namespace. Supplying <tt>null</tt> removes the namespace.
     */
    public static void renameNamespaceRecursive(Node node, String namespace) {
        Document document = node.getOwnerDocument();
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            document.renameNode(node, namespace, node.getNodeName());
        }
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); ++i) {
            renameNamespaceRecursive(list.item(i), namespace);
        }
    }

1 Comment

It is always terrible advice to use regex for XML parsing. It simply can not be done. Using this code you will certainly run into problems, e.g. suddenly you have the string "xmlns" somewhere as text in your XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.