0

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.

1
  • I think I have figured out why it does not work. ss happens to be the default namespace in the root of the same DOM: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="w3.org/TR/REC-html40"> So how do I still add it explicitly? I still need it there. Commented Jun 14, 2012 at 17:54

1 Answer 1

0

The following works:

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

It generates:

<Data ss:type="String"></Data>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.