Antwort: RE: [jdom-interest] Namespace help

Gordon Tyler gordon.tyler at sitraka.com
Thu Jul 25 08:05:53 PDT 2002


Okay, I can see this thread is going nowhere fast. I also experience your
confusion with this whole namespace business so let's see if I can explain
it.

In XML (forget about JDOM for the moment), when you defined a namespace on
an element, all child elements inherit that namspace unless otherwise
specified. So, for example:

<root xmlns="http://foobar.com">
  <child1/>
  <child2 xmlns="http://baz.com"/>
  <child3 xmlns=""/>
</root>

In this example, the root element has a namespace, the child1 element
inherits it, the child2 has it's own namspace (which its children would
inherit) and child3 has _no_ namespace -- as if root didn't have an xmlns
attribute.

Now, in JDOM, when creating Element objects you _must_ specify a namespace
(either using a Namespace object or just a URI String) if you are using
namespaces. To achieve the above example in JDOM would involve the
following:

Element root = new Element("root", "http://foobar.com");
Element child1 = new Element("child1", "http://foobar.com");
root.addContent(child1);
Element child2 = new Element("child2", "http://baz.com");
root.addContent(child2);
Element child3 = new Element("child3", "");
root.addContent(child3);

When you run that through XMLOuputter you should get the same output as
above.

Look at the Javadoc for Element
(http://www.jdom.org/docs/apidocs/org/jdom/Element.html) and you'll see that
the Element(String name) constructor says it will create an Element _in NO
Namespace_. In other words, it won't inherit it from it's parent Element
because it doesn't _know_ what it's parent Element is. So to make the
Element belong to a particular namespace you must use one of the the
constructors (or use setNamespace once the object is created).

The above code can be simplified a little like so:

// These could be made constants in some class
Namespace foobar = Namespace.getNamespace("http://foobar.com");
Namespace baz = Namespace.getNamespace("http://baz.com");

Element root = new Element("root", foobar);
Element child1 = new Element("child1", foobar);
root.addContent(child1);
Element child2 = new Element("child2", baz);
root.addContent(child2);
Element child3 = new Element("child3", Namespace.NO_NAMESPACE);
root.addContent(child3);

Ciao,
Gordon

----- Original Message -----
From: "Li Xu" <Lxu at copyright.com>
To: "Hans Prüller" <hans.prueller at igs.at>
Cc: <jdom-interest at jdom.org>
Sent: Thursday, July 25, 2002 10:27 AM
Subject: RE: Antwort: RE: [jdom-interest] Namespace help


> That's why this whole thing is so confusing to me. Elliotte and Bob both
said "" is not a namespace, you said "" is a namespace ... Furthermore, if
level3 can inherit level2's namespace without a xmlns=, then why didn't
level2 do so and inherit level1's namespace thus get rid of xmlns=""?
>
> I guess my original question is still unclear. I'm not having any problem
with namespace or the parser. All java objects I have, either a Document or
an Element, are exactly correct. The issue is with the Outputter, when it's
printing out a XML string why is it outputting 'xmlns=""' when it knows that
<level2> is inheriting the namespace form <root_elem> which is in
"http://my.com" namespace?
>
>
>
> > -----Original Message-----
> > From: Hans Prüller [mailto:hans.prueller at igs.at]
> > Sent: Thursday, July 25, 2002 10:10 AM
> > To: Li Xu
> > Cc: jdom-interest at jdom.org
> > Subject: Antwort: RE: [jdom-interest] Namespace help
> >
> >
> >
> > i think the level3-element inherits the xmlns="" from level 2.
> >
> > so it should be in the ""-namespace.
> >
> > hans
> >
> >
> >
> >
> >
> >
> >                     "Li Xu"
> >
> >
> >                     <Lxu at copyright.com        An:     "bob
> > mcwhirter" <bob at werken.com>
> >
> >                     >                         Kopie:
> > "Elliotte Rusty Harold" <elharo at metalab.unc.edu>,
> > <jdom-interest at jdom.org>
> >                     Gesendet von:             Thema:  RE:
> > [jdom-interest] Namespace help
> >
> >                     jdom-interest-admi
> >
> >
> >                     n at jdom.org
> >
> >
> >
> >
> >
> >
> >
> >
> >                     25.07.2002 15:59
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > > but XMLOutputter is giving this:
> > > >
> > > > ===============
> > > > ===============
> > > > <root_elem xmlns="http://my.com" ....>
> > > >        <level2 xmlns="">
> > > >                  <level3>xyz</level3>
> > > >        </level2>
> > > > </root_elem>
> > > > ===============
> > > >
> > > > Notice <level3> is perfectly fine without any namespace?
> > > Please tell me what I'm missing here.
> > >
> > > Right, because <level2> and <level3> are both not in namespaces,
> > > and semantically, that's what this document says.
> > >
> >
> >
> > So why didn't it print <level3 xmlns=""> just like <level2> then?
> > _______________________________________________
> > To control your jdom-interest membership:
> > http://lists.denveronline.net/mailman/options/jdom-interest/yo
> uraddr at yourhost.com
>
>
>
>
>
> ____________________________________________________
> IGS Systemmanagement
> GmbH & Co KG
> Dorfplatz 5
> A-4531 Piberbach
> phone: +43 7228 6451 0       home: http://www.igs.at
> fax: +43 7228 6451 30        eMail: igs at igs.at
> hotline:
> fax: +43 7228 6451 20        eMail: hotline at igs.at
> ____________________________________________________
>
> NEWSFLASH___________________________________________
>
> Personalmanagement ab sofort verfügbar.
> Reservieren Sie sich Ihren persönlichen Demotermin
> unter igs at igs.at
>
> NEWSFLASH___________________________________________
>
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com




More information about the jdom-interest mailing list