【问题标题】:Avoid adding XML Namespace to Output Message避免将 XML 命名空间添加到输出消息
【发布时间】:2014-05-06 07:40:15
【问题描述】:

我正在尝试在现有 XML 中添加一个元素。转换后,我在添加的元素中得到xmlns="",我不需要。

原始 XML:

<Message version='010' release='006' 
         xsi:schemaLocation='http://www.ncpdp.org/schema/SCRIPT SS_SCRIPT_XML_10_6MU.xsd' 
         xmlns='http://www.ncpdp.org/schema/SCRIPT' 
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>

    <Header> ... </Header>
    <Body>
        <New> ...
            <Medication>
                ...
                <StatusCode>NF</StatusCode>
                <StatusCode>NR</StatusCode>
           </Medication>
        </New>
    </Body>
</Message>

实际(不需要的)输出:

<Message version='010' release='006' 
         xsi:schemaLocation='http://www.ncpdp.org/schema/SCRIPT SS_SCRIPT_XML_10_6MU.xsd' 
         xmlns='http://www.ncpdp.org/schema/SCRIPT' 
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>

    <Header> ... </Header>
    <Body>
        <New> ...
            <Medication>
                ...
                <StatusCode>NF</StatusCode>
                <StatusCode>NR</StatusCode>
                <StatusCode xmlns="">SI</StatusCode>
           </Medication>
        </New>
    </Body>
</Message>

预期输出:

<Message version='010' release='006' 
         xsi:schemaLocation='http://www.ncpdp.org/schema/SCRIPT SS_SCRIPT_XML_10_6MU.xsd' 
         xmlns='http://www.ncpdp.org/schema/SCRIPT' 
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>

    <Header> ... </Header>
    <Body>
        <New> ...
            <Medication>
                ...
                <StatusCode>NF</StatusCode>
                <StatusCode>NR</StatusCode>
                <StatusCode>SI</StatusCode>
           </Medication>
        </New>
    </Body>
</Message>

我不希望在添加的元素 &lt;StatusCode&gt;SI&lt;/StatusCode&gt; 中出现 xmlns=""

Java 代码:

private DocumentBuilderFactory getDocumentBuilderFactory() {
    if (factory == null) {
        factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
    }
    return factory;
}

public void addSIElement() throws ParserConfigurationException, SAXException, IOException, TransformerException {

     Transformer transformer = null;
     Document doc = getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource(new StringReader(xmlMsg)));

     Node list = doc.getElementsByTagName("Medication").item(0);
     Element el = doc.createElement("StatusCode");
     el.setTextContent("SI");
     list.appendChild(el);
     Source source = new DOMSource(doc);
     StringWriter writer = new StringWriter();
     Result newResult = new StreamResult(writer);
     if (transformer == null) {
         transformer = TransformerFactory.newInstance().newTransformer();
     }

     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     transformer.transform(source, newResult);

     String outStr = writer.toString();
     System.out.println("Final " + outStr);
}

【问题讨论】:

    标签: java xml dom xml-namespaces


    【解决方案1】:

    您正在 no-namespace 中创建一个新元素,但原始 XML 中的所有元素都属于 http://www.ncpdp.org/schema/SCRIPT 命名空间。为了正确添加元素,解析器添加了xmlns="" 属性,因此该元素被声明为属于no-namespace

    要解决此问题,请使用 org.w3c.dom.Document.createElementNS 创建元素,并提供原始文件的命名空间:

    Element el = doc.createElementNS("http://www.ncpdp.org/schema/SCRIPT", "StatusCode");
    

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2023-03-24
      • 2017-09-21
      • 2012-03-29
      • 2023-03-07
      相关资源
      最近更新 更多