[jdom-interest] addNamespaceDeclaration()

D Gross justdave at onebox.com
Tue Apr 17 11:14:47 PDT 2001


I would like to add the xsi namespace to the top element of my JDOM document
so that I can use xsi:xxx attributes in subelements.  I did the following:

  Namespace XSI_NAMESPACE = Namespace.getNamespace("xsi", "http://www.w3.org/2000/10/XMLSchema-instance");

  // Set a namespace for xsi:type attributes on the top Element
  Element top = (Element)resultDoc.getRootElement();
  top.addNamespaceDeclaration(XSI_NAMESPACE);

What happens is JDOM will output an xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
on every element that has an xsi:xxx attribute and not on the top element
(presumably because there is no xsi:xxx attribute there).  This is a
very big waste of space in the output that I would like to clean up.
 Is there some other way to do what I want?

For example, I want the output to look like this:

  <?xml version="1.0" encoding="UTF-8"?>
  <top xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
    <sub1 xsi:type="foo"/>
    <sub2 xsi:type="bar"/>
  </top>

Not this:

  <?xml version="1.0" encoding="UTF-8"?>
  <top>
    <sub1 xsi:type="foo" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"/>
    <sub2 xsi:type="bar" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"/>
  </top>


Thanks,
-- Dave
D Gross
justdave at onebox.com - email
(312) 777-4000 x7673 - voicemail/fax



__________________________________________________
FREE voicemail, email, and fax...all in one place.
Sign Up Now! http://www.onebox.com




More information about the jdom-interest mailing list