[jdom-interest] Problems with default namespaces in Attributes

oskar.lonnberg at nokia.com oskar.lonnberg at nokia.com
Wed Jun 27 03:05:27 PDT 2001


Thanks for the cool XML implementation. 

I have been trying to figure out if this solves my tasks but came into 
a situation which I guess is a bug. (For some reason I can't access the 
web site now to read the FAQ and stuff)

I have some problems with default namespaces and attributes. If there is
a default namespace (xmlns="...") defined for an element it should apply
to its attributes also. The element namespace is retrieved correctly, but
the attributes do not have any namespace defined. The code in the end should
demonstrate the bug.

I used xerces parser because crimson seamed to get totally mixed up and
it didn't parse. And I used the beta 6 and beta 7 rc when testing this.


Oskar Lönnberg


____________________________________________________


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



public class Tester
{

    public static void main(String[] args )
    {
        Reader source = new StringReader("<?xml version =\"1.0\"?>\n" + 
            "<Root Attr1 = \"Some value\"  ns:Attr2 = \"Value\" " + 
            "xmlns = \"http://somenamespace.com\" " +
            "xmlns:ns = \"http://anothernamespace.com\"> " +
            "<SomeElement/><ns:OtherElement/></Root>");

        try {
          SAXBuilder b = new SAXBuilder();
          Document doc = b.build(source);
          Element root = doc.getRootElement();
          Namespace ns =
Namespace.getNamespace("ns","http://anothernamespace.com");
          System.out.println("root.getNamespace() " + root.getNamespace());
          System.out.println("ns = " + ns);
          
          // This should return null but it return the Attribute
          System.out.println("root.getAttribute(Attr1) " +
root.getAttribute("Attr1"));
          // This should return Attribute but it returns null
          System.out.println("root.getAttribute(Attr1,root.getNamespace()) "

                                    +
root.getAttribute("Attr1",root.getNamespace()));
          System.out.println("root.getAttribute(Attr2) " 
                                    + root.getAttribute("Attr2"));
          System.out.println("root.getAttribute(Attr2,ns) " 
                                    + root.getAttribute("Attr2",ns));
        } catch (Exception e) {
          e.printStackTrace();
        }
        
    }
}



More information about the jdom-interest mailing list