Search code examples
javaxmlxml-namespacessetattribute

Setting Namespace Attributes on an Element


I'm trying to create an XML document in Java that contains the following Element:

<project xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2" 
         xmlns:acme="http://www.acme.com/schemas"
         color="blue">

I know how to create the project Node. I also know how to set the color attribute using

element.setAttribute("color", "blue")

Do I set the xmlns and xmlns:acme attributes the same way using setAttribute() or do I do it in some special way since they are namespace attributes?


Solution

  • I believe that you have to use:

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:acme", "http://www.acme.com/schemas");