[jdom-interest] Name space and attributes

Brett McLaughlin brett.mclaughlin at lutris.com
Mon Jul 17 18:50:44 PDT 2000


Vincent Aumont wrote:
> 
> 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
> document:
>  <doc xmlns=ns>

First, this must be

  <doc xmlns="ns">

>    <foo attr="bar"/>
>  </doc>
> 
> you would retrieve the 'foo' element with:
> 
> (1)    element = doc.getRootElement().getChild("foo","ns");

Then, this would work (since you are in the default namespace, and the
URI is "ns"

> 
> and the attribute with :
> 
>  (2) attr = element.getAttribute("attr","ns")

This should not work, because the attribute attr has no namespace, and
no URI as a result. The default namespace applies only to elements, not
to attributes as well. So this is expected behavior in the latest
nightly, and a bug in Beta 4

-Brett

> 
> 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?
> 
> Thanks,
> 
> Vincent.
> 
> 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);}
>   }
> }
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org



More information about the jdom-interest mailing list