避免将XML命名空间添加到输出消息

时间:2014-05-06 07:40:15

标签: java xml dom xml-namespaces

我正在尝试在现有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>

我不希望添加的元素xmlns=""中有<StatusCode>SI</StatusCode>

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

1 个答案:

答案 0 :(得分:2)

您正在 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");