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>
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();
}
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With