GetXMLAttribute

File management function.

The GetXMLAttribute function retrieves the content of an attribute in the specified XML file. Not available in WinTask Lite

Syntax

GetXMLAttribute(<filename>,<xml_path>,<attribute_name>,<result$>)
or
ret=GetXMLAttribute(<filename>,<xml_path>,<attribute_name>,<result$>)

Parameters

<filename>, string, name of the XML file where to add/modify the attribute.

<XML_path>, string, list of node descriptors separated by the \ character to go where the attribute is. The string is not case-sensitive.The structure of such a path is:
tagname[attributename1='value']
The tagnames are strings without double quotes, they cannot contain spaces or reserved characters.
The attribute names are strings without double quotes, OR it can be the reserved keyword <text> or the reserved keyword <index>. <text> is used to specify the node inner text, <index> is used to specify the node index (first one starts at 1) when more nodes with the same tag and attributes exist within the same parent. Several attributes can be listed with the syntax tagname[attributename1='value1', attributename2='value2']
Examples:
If the XML file contains:
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
the <XML_path> can be "bookstore\book[genre='autobiography']" or "bookstore\book[publicationdate='1981']" or "bookstore\book[publicationdate='1981',genre='autobiography']"

<attribute_name>, string, name of the attribute (surrounded by double quotes as it is a string), OR keyword <text> surrounded by double quotes to retrieve the content of the text between the starting tag and the ending tag of the tag specified in <xml_path>.

<result$>, string, retrieved content for the specified attribute

Return value

Ret, numeric return code. When the content of the specified attribute is successfully retrieved from the XML file, the function returns 0, otherwise use this return code for Error Handling. The possible error codes are:

27

The XML file cannot be open

90

Internal error (COM error when invoking XML COM)

99

Invalid parameters

110

Invalid XML path

111

Invalid XML attribute or attribute not found

 

See also

AppendXMLNode

SetXMLAttribute

EnumXMLAttributes

EnumXMLChildren

Examples

This code generates a XML file and GetXMLAttribute is used to retrieve the content of 2 attribute names just created.

file$ = "c:\program files\wintask\scripts\test.xml"
create(file$)
appendxmlnode(file$, "", "root")
appendxmlnode(file$, "root", "book")
appendxmlnode(file$, "root", "book")
appendxmlnode(file$, "root\book", "Mark_Twain")
appendxmlnode(file$, "root\book", "Mark_Twain")
appendxmlnode(file$, "root\book[<index>=2]", "Portugalia")
setxmlattribute(file$, "root\book\Mark_Twain", "<text>", "Misc description")
setxmlattribute(file$, "root\book\Mark_Twain[<index>=2]", "year", "1987")
setxmlattribute(file$, "root\book\Mark_Twain[year='1987']", "<text>", "Another misc description")
getxmlattribute(file$, "root\book\Mark_Twain[<index>=2]", "year", val$)
msgbox(val$) 'val$ contains 1987
getxmlattribute(file$, "root\book\Mark_Twain[year='1987']", "<text>", val$)
msgbox(val$) 'val$ contains Another misc description

With this XML file:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

the script below returns the text titles for the books in category WEB:

dim children$(100)
file$ = "c:\program files\wintask\scripts\sample.xml"
ret = enumxmlchildren(file$, "bookstore", children$())
i=0
while i < ret
  getxmlattribute(file$, children$(i), "category", val$)
  if val$ = "WEB" then
  getxmlattribute(file$, children$(i)+"\title[lang='en']", "<text>", val$)
  msgbox(val$)
  endif
  i = i+1
wend