Adding a namespace to an already created XML document

I am creating a W3C document object using a String value. When I created the Document object, I want to add a namespace to the root element of this document. Here is my current code:

Document document = builder.parse(new InputSource(new StringReader(xmlString)));
document.getDocumentElement().setAttributeNS("http://com", "xmlns:ns2", "Test");
document.setPrefix("ns2");
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result dest = new StreamResult(new File("c:\\xmlFileName.xml"));
aTransformer.transform(src, dest);

What I use for input:

<product>
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</product>

What should be the result:

<ns2:product xmlns:ns2="http://com">
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</ns2:product>

I need to add the prefix value and namespace also to the xml input string. If I try the code above, I get this exception:

NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

Appreciate your help!

+5
source share
2 answers

, , . , , (URI) , .

setAttribute setPrefix ( 2,3)

String namespace = "http://com";
String prefix = "ns2";
// Upgrade the DOM level 1 to level 2 with the correct namespace
Element originalDocumentElement = document.getDocumentElement();
Element newDocumentElement = document.createElementNS(namespace, originalDocumentElement.getNodeName());
// Set the desired namespace and prefix
newDocumentElement.setPrefix(prefix);
// Copy all children
NodeList list = originalDocumentElement.getChildNodes();
while(list.getLength()!=0) {
    newDocumentElement.appendChild(list.item(0));
}
// Replace the original element
document.replaceChild(newDocumentElement, originalDocumentElement);

:

.setAttributeNS("http://com", "xmlns:ns2", "Test");

, , http://www.w3.org/2000/xmlns/ URI.

.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns2", "http://com");
+20

, , , .

  • root .
  • XML. - XML .
  • xml .
  • XML-, JAXB ..

private static String addNamespaceToXml(InputStream in)
        throws ParserConfigurationException, SAXException, IOException,
        TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    /*
     * Must not namespace aware, otherwise the generated XML string will
     * have wrong namespace
     */
    // dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(in);
    Element documentElement = document.getDocumentElement();
    // Add name space to root element as attribute
    documentElement.setAttribute("xmlns", "http://you_name_space");
    String xml = transformXmlNodeToXmlString(documentElement);
    return xml;
}

private static String transformXmlNodeToXmlString(Node node)
        throws TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(buffer));
    String xml = buffer.toString();
    return xml;
}
0

All Articles