[jdom-interest] JDOM Xpath attribute help

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Mar 2 08:03:58 PST 2004


Hi,

The XPath expression you pass to getString should just be "@duplicate". But be 
careful then because selectSingleNode will return the Attribute node.

You may also replace getString() by :
     new Xpath(path).valueOf(elem);
except that this would return "" if the attribute is absent.

Laurent


john tal wrote:

> Any ideas on how to retrieve this value from a
> schema-based XML file?  
> 
> JDOM tutorials are a little thin on the XPath side of
> the house.
> 
> Target XML trying to read:
> 
> <acknowledgementHeader type="PROCESSED" success="TRUE"
> duplicate="FALSE">
> 
> This does not work:
> 
> 			org.jdom.xpath.XPath nodePath =
> org.jdom.xpath.XPath.newInstance("/test:envelope/body/response/acknowledge/acknowledgement/acknowledgementHeader");
> 			Iterator nodeIter =
> nodePath.selectNodes(doc).iterator();
> 			while (nodeIter.hasNext()) 
> 			{
> 				org.jdom.Element element =
> (org.jdom.Element)nodeIter.next();
> 				String duplicate =
> getString(element,"attr[@name='duplicate']/string");
> 
> 
> GetString is defined as:
> 
>    protected String getString(org.jdom.Element elem,
> String path) throws org.jdom.JDOMException
>     {
>         org.jdom.xpath.XPath xpath =
> org.jdom.xpath.XPath.newInstance(path);
>         org.jdom.Element e =
> (org.jdom.Element)xpath.selectSingleNode(elem);
>         return e!=null ? e.getText() : null;
>     }			



More information about the jdom-interest mailing list