Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 避免在输出消息中添加XML名称空间_Java_Xml_Dom_Xml Namespaces - Fatal编程技术网

Java 避免在输出消息中添加XML名称空间

Java 避免在输出消息中添加XML名称空间,java,xml,dom,xml-namespaces,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' xml

我试图在现有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>
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);
}

您没有在名称空间中创建新元素,但是原始XML中的所有元素都属于
http://www.ncpdp.org/schema/SCRIPT
名称空间。为了正确地添加元素,解析器添加了
xmlns=”“
属性,以便声明该元素不属于任何名称空间

要解决此问题,请使用提供原始文件的命名空间来创建元素:

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