[jdom-interest] Name space and attributes

Vincent Aumont vincent.aumont at vslab.com
Mon Jul 17 17:48:50 PDT 2000

I'm using the latest nightly build (Jul 17, 2000).

I'm not too familiar with name spaces but I thought that if you had to following
 <doc xmlns=ns>
   <foo attr="bar"/>

you would retrieve the 'foo' element with:

(1)    element = doc.getRootElement().getChild("foo","ns");

and the attribute with :

 (2) attr = element.getAttribute("attr","ns")

At least that's how it worked in b4.

With the latest build, (1) works fine but (2) returns null.
To retrieve the attribute, I have to omit the namespace:
  element = doc.getRootElement().getChild("foo","ns");
  attr = element.getAttribute("attr");

Is that the expected behavior?



The code below demonstrates the problem.

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class TestNS {

 static String m_xml = "<doc xmlns=\"ns\"> <foo attr=\"bar\"/></doc>";

 public static void main (String[] args)
    SAXBuilder builder= new SAXBuilder("org.apache.xerces.parsers.SAXParser");
    ByteArrayInputStream in = new ByteArrayInputStream(m_xml.getBytes());
    Document doc ;
    Element element;

    try {
      doc = builder.build(in);

      element = doc.getRootElement().getChild("foo","ns");

      if (doc.getRootElement().getChild("foo","ns") == null)
        System.out.println("no entry !!!");

      if (element.getAttribute("attr","ns") == null)
                System.out.println("Attribute not found");
    catch (Exception e) {System.out.println(e);}

More information about the jdom-interest mailing list