Remove namespace from XML in Java

前端 未结 2 419
不思量自难忘°
不思量自难忘° 2021-01-05 05:55

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

相关标签:
2条回答
  • 2021-01-05 06:35

    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();
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-05 06:35

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题