简体   繁体   中英

add namespace to java dom attribute

I am trying to create an attribute with a namespace prefix. The following code does not seem to work:

Element newData = docFromXML.createElement("Data");
Attr typeAttr = docFromXML.createAttributeNS(namespaceContext.getNamespaceURI("ss"), "type");
typeAttr.setValue("String");
newData.setAttributeNode(typeAttr);

I expect the following:

<Data ss:type="String"></Data>

But the following is produced:

<Data type="String"></Data>

How do I explicitely add the namespace prefix "ss". The getNamespaceURI function returns the URL for the ss prefix in the DOM.

The following works:

Attr typeAttr = docFromXML.createAttribute("ss:type");

It generates:

<Data ss:type="String"></Data>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM