[jdom-interest] How do I use namespaces?

Frédéric Laurent fl at opikanoba.org
Thu Feb 5 00:58:06 PST 2004


Quoting Garey Mills <gmills at library.berkeley.edu>:
> Hi - 

Hello


> 
> 	I am using JDOM to parse documents that use attributes in a number
> of different namespaces. The namespaces correspond to standards that are
> being revised, so I can't be sure what their URI is (since each revision
> is given a new URI).
> 
> 	The namespaces are declared in the root element, for example:
> 
> xmlns:mets="http://www.loc.gov/METS/"
> xmlns:moa2="http://sunsite.berkeley.edu/MOA2/"
> xmlns:mods="http://www.loc.gov/mods/v3" 
> xmlns:mix="http://www.loc.gov/mix/"
> xmlns:xlink="http://www.w3.org/TR/xlink"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> 
> 
> 	If, at some point in the document, I want to retrieve an attribute
> that belongs to, say, the 'mods' namespace, can I just use the prefix?
> 
> 
> 	Attribute x = myElement.getAttribute("x", "mods");
> 
> 	I don't think I can because getAttribute needs a Namespace and not
> a string
> 
> 	So do I have to do something more convoluted, such as retrieve the
> namespaces defined on the element, search for the one with the name "mods"
> and then include that one in the get Attribute call?
> 
> 	Or is there someway I can access the namespaces declared in the
> root element?

for sure !
see 
http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getNamespace()
and http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getAdditionalNamespaces()

> 	In other words, what does it get me that namespaces are declared
> in the root element?

here is an sample that might give you some clue
Retrieve all the namespaces defined on the root element 
and choose which object (Namespace objet) defines your prefix
or you URI... Then use this namespace objet as getElement or
getAttribute paramter...

It's written in jython (to be shorter and faster)


------------------------------------------------------------
from java.io import *
from java.lang import *
from org.jdom.input import *
from org.jdom.xpath import *

xmlbuffer ="""<?xml version='1.0' encoding='ISO-8859-1'?>
<rootElt xmlns:mets="http://www.loc.gov/METS/"
	xmlns:moa2="http://sunsite.berkeley.edu/MOA2/"
	xmlns:mods="http://www.loc.gov/mods/v3"
	xmlns:mix="http://www.loc.gov/mix/"
	xmlns:xlink="http://www.w3.org/TR/xlink"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<mix:a mods:attr="test"/>
</rootElt>
"""

docBuilder = SAXBuilder()
doc = SAXBuilder().build(StringReader(xmlbuffer))
Result = StringBuffer()

nsList = doc.getRootElement().getAdditionalNamespaces()
mixNS = modsNS = None
for ns in nsList:
	if ns.getURI() == "http://www.loc.gov/mix/":
		mixNS = ns
	elif ns.getURI() == "http://www.loc.gov/mods/v3":
		modsNS = ns


EltA = doc.getRootElement().getChild("a",mixNS)
print "Elt A : ",EltA
attr = EltA.getAttribute("attr",modsNS)
print "Attr  name (",attr.getName(),") value  (",attr.getValue(),")"
print "Attr dump : ",attr

------------------------------------------------------------

output is 

     [java] Elt A :  [Element: <mix:a [Namespace: http://www.loc.gov/mix/]/>]
     [java] Attr  name ( attr ) value  ( test )
     [java] Attr dump :  [Attribute: mods:attr="test"]


HTH 
cheers

-- 
XPath free testing software :  http://lantern.sourceforge.net
Frédéric Laurent                     http://www.opikanoba.org



More information about the jdom-interest mailing list