Wiki source for FunctionStAXgetAttribute


Show raw source

=====Function getAttribute=====

This function is part of the [[FunctionsStAX StAX]] support.

====Name====
**getAttribute** returns the value of a named attribute in a START_ELEMENT event.


====Synopsis====
getAttribute( $event name )



====Description====


getAttribute returns the string value of an attribute.
The $event argument must be a START_ELEMENT event returned from [[FunctionStAXnextEvent nextEvent]].
The name argument is the name of attribute, it can be a string or a [[FunctionQName QName]].
Strings are interpreted as either a no-namespace local name, or in ClarkNotation.



To use the ""StAX"" functions you need to [[CommandImport import]] them into either the global namespace or a local namespace.
Its recommended that you use a local namespace so that there is less chance of name collisions.


Example: process every START_EVENT for element named "element" and print the attribute value for "attr"

Given an xml file "file.xml"
%%(xml)
<file>
<element attr="attr1"/>
<element attr="attr2">String</element>
</file>

%%

%%
import commands stax=stax
reader=stax:newEventReader( file.xml )
while [ stax:hasNext( $reader START_ELEMENT element ) ] ; do
event=stax:nextEvent( $reader ) ;
attr=stax:getAttribute( $event attr )
echo $attr
done

stax:closeReader $reader

%%

Result
%%
attr1
attr2
%%



----
[[Commands]]
[[FunctionsStAX StAX Functions]]
[[FunctionStAXnewEventReader newEventReader]]
[[FunctionStAXnextEvent nextEvent]]
[[JavaObjects Java Objects]]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki