While dynamically
trying to retrieve an Attribute value using Jaxen...I find that I have a
problem. I have to "KNOW" within my program that I have searched for an
Attribute as opposed to an Element. This is because the XPath selectNodes
returns a list of Elements (using JDOM) that contain the attributes instead of a
list of Attributes.
I'm fine with it
returning a list of Elements and think that this may often be helpful since you
know that you are free to cast the List elements into JDOM Element
instances. The problem that I have is that I have to know that my
"dynamic" xpath expression was ACTUALLY asking for Attributes of a certain
name...then I have to go thru the Elements returned and pick out the values of
the named attributes....this takes away from the dynamic nature of the program
(or causes me to have to know things that I wish JAXEN would know for
me).
Is there a way
around this problem. Example StringFunction.evaluate(e, navigator)
will not print the "name" attribute value, but will instead print the value of
the element which contains the "name" attribute:
Document doc = builder.build( args[0]
);
XPath xpath = new XPath( "/TRIPS/TRIP/VACATION_ID[@name]");
List results = xpath.selectNodes( doc );
Iterator resultIter = results.iterator();
Navigator navigator = xpath.getNavigator();
XPath xpath = new XPath( "/TRIPS/TRIP/VACATION_ID[@name]");
List results = xpath.selectNodes( doc );
Iterator resultIter = results.iterator();
Navigator navigator = xpath.getNavigator();
while ( resultIter.hasNext()
)
{
Element e = (Element)resultIter.next();
System.out.println( e.getText() );
System.out.println( e.getAttribute("name").getValue() );
System.out.println("hERE IS E: " + StringFunction.evaluate(e, navigator));
}
{
Element e = (Element)resultIter.next();
System.out.println( e.getText() );
System.out.println( e.getAttribute("name").getValue() );
System.out.println("hERE IS E: " + StringFunction.evaluate(e, navigator));
}
becuase inside of
StringFunction it will hit:
else if (
nav.isElement( obj ) )
{
return nav.getElementStringValue( obj );
}
{
return nav.getElementStringValue( obj );
}
instead
of:
else if (
nav.isAttribute( obj ) )
{
return nav.getAttributeStringValue( obj );
}
{
return nav.getAttributeStringValue( obj );
}
tcp