0

Using Java DOM I'm trying to set an attribute for an element without the namespace before the attribute name.

So, what I need is:

<documentObject xmlns="http://www.myschema.com">
    <element1 attr1="value">foo</element1>
</documentObject>

If I try to set the attribute as following element1.setAttributeNS("http://www.myschema.com", "attr1", value); I get an empty xmlns tag and additionaly a xmlns with prefix like the following:

<element1 attr1="value" xmlns="" xmlns:ns3="http://www.myschema.com">foo</element1>

If I try to set the attribute as following element1.setAttribute("xmlns:attr1", value); I get a prefix (xmlns) before my attribute name as shown here:

<element1 xmlns:attr1="value">foo</element1>

As for further information I create my elements as following:

 Element element = dom.createElementNS("http://www.myschema.com", elemName);
 element.appendChild(dom.createCDATASection("foo");
 xmlElement.appendChild(element);

2 Answers 2

1

Let's look at your desired output again:

<documentObject xmlns="http://www.myschema.com">
    <element1 attr1="value">foo</element1>
</documentObject>

In this document, the following statements are true:

  1. documentObject and element1 are in the http://www.myschema.com namespace.

  2. The attribute attr1 is not in any namespace.

While elements whose names are not prefixed are going to be in whatever default namespace is in effect, attributes whose names are not prefixed are not in any namespaces. See the spec:

Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

So to obtain the output you desire, you should be able to just do:

element1.setAttribute("attr1", value);

Of course this all depends on the desired output being correct. If really attr1 must be in a namespace, then your desired output is incorrect.

Sign up to request clarification or add additional context in comments.

Comments

0

Below code will produce output

<?xml version="1.0" encoding="UTF-8"?>
<documentObject xmlns="http://www.myschema.com">
    <element1 attr1="value">foo</element1>
</documentObject>

Java Code

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLTest {

    public static void main(String[] args) throws ParserConfigurationException, TransformerException {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();

        Element element = doc.createElementNS("http://www.myschema.com", "documentObject");
        doc.appendChild(element);

        Element element1 = doc.createElement("element1");
        element.appendChild(element1);

        element1.appendChild(doc.createTextNode("foo"));
        Attr attr = doc.createAttribute("attr1");
        attr.setValue("value");
        element1.setAttributeNode(attr);

        element.appendChild(element1);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("testfile.xml"));
        transformer.transform(source, result);
        System.out.println("File saved!");
    }
}

2 Comments

In this case I get the result in the following format: <element1 attr1="value" xmlns="" xmlns:ns3="myschema.com">foo</element1>
I have added complete sample. Try this!

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.