質問

I am trying to add an element in an Existing XML. After the transformation I am getting xmlns="" in the added element, which I don't need.

Original 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>

Actual (undesired) Output:

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

Expected Output:

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

I do not want the xmlns="" in the added element <StatusCode>SI</StatusCode>.

Java Code:

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);
}
役に立ちましたか?

解決

You are creating a new element in no-namespace, but all the elements in your original XML belong to the http://www.ncpdp.org/schema/SCRIPT namespace. In order to add the element correctly, the parser adds the xmlns="" attribute so the element is declared to belong to no-namespace.

To fix that, create the element using org.w3c.dom.Document.createElementNS providing the namespace of your original file:

Element el = doc.createElementNS("http://www.ncpdp.org/schema/SCRIPT", "StatusCode");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top