在Java中从XML中删除命名空间

时间:2014-07-30 09:31:18

标签: java xml

我想在Java中从XML中删除命名空间。你能指导一下需要做什么吗?可以使用DOM解析器,但这将是逐个节点解析的节点。我想知道是否有一些代码可以从整个XML中删除所有命名空间(mig:)。

我的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>

2 个答案:

答案 0 :(得分:6)

您可以使用xslt。尝试

<强> 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();
        }
    }

}

答案 1 :(得分:0)

可以使用正则表达式来获取更多信息,请参阅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;
        }
   }

您可以找到类似的示例here

正则表达式可能很痛苦。您也可以使用此api(dom)来删除所有名称空间。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);
        }
    }