View Full Version : xml parse and attributes
Scot - ScotsScripts.com
08-12-08, 08:03 PM
I'm using the xml_parse function and having some trouble accessing the attributes.
Say we have some xml file like this:
<cats id="123">
<title>My Cat Tiger</title>
<description>Tiger killing a mouse</description>
</cats>
I'm able to get the title and description no problem, and though I can see the "id" attribute when I display the entire parsed xml string, I haven't been able to access it, but there must be a way since it's there.
Any tips?
Thanks-
Scot
BillBuilt
08-12-08, 09:30 PM
Scot,
Pass the resulting xml array to this function like so:
<MvASSIGN NAME="var" VALUE="{xml_parse('test.xml','script',l.array)}">
<MvASSIGN NAME="var" VALUE="{debugArray(l.array)}">
<MvFUNCTION NAME="debugArray" PARAMETERS="array" STANDARDOUTPUTLEVEL="">
<MvASSIGN NAME="l.array" VALUE="{miva_array_serialize(l.array)}">
<MvASSIGN NAME="l.pos" VALUE="1">
<MvASSIGN NAME="l.text" VALUE="{gettoken(l.array, ',', l.pos)}">
<MvWHILE EXPR="{l.text}">
<MvEVAL EXPR="{encodeentities(decodeattribute(l.text)) $ '<br />'}">
<MvASSIGN NAME="l.pos" VALUE="{l.pos + 1}">
<MvASSIGN NAME="l.text" VALUE="{gettoken(l.array, ',', l.pos)}">
</MvWHILE>
</MvFUNCTION>
The results:
:CATS:ATTRIBUTE:ID=123
:CATS:CHILDREN[1]:LINENO=2
:CATS:CHILDREN[1]:NAME=title
:CATS:CHILDREN[1]:OFFSET=17
:CATS:CHILDREN[1]:VALUE=My Cat Tiger
:CATS:CHILDREN[2]:LINENO=3
:CATS:CHILDREN[2]:NAME=description
:CATS:CHILDREN[2]:OFFSET=46
:CATS:CHILDREN[2]:VALUE=Tiger killing a mouse
:CATS:LINENO=1
:CATS:NAME=cats
:CATS:OFFSET=0
:CATS:TAGS:DESCRIPTION[1]:LINENO=3
:CATS:TAGS:DESCRIPTION[1]:NAME=description
:CATS:TAGS:DESCRIPTION[1]:OFFSET=46
:CATS:TAGS:DESCRIPTION[1]:VALUE=Tiger killing a mouse
:CATS:TAGS:TITLE[1]:LINENO=2
:CATS:TAGS:TITLE[1]:NAME=title
:CATS:TAGS:TITLE[1]:OFFSET=17
:CATS:TAGS:TITLE[1]:VALUE=My Cat Tiger
:CATS:VALUE=
So to access the ID attribute:
<MvEVAL EXPR="{'ID='$l.array:cats:attribute:id}">
Bill
Scot - ScotsScripts.com
08-12-08, 11:34 PM
Bill, thanks for the function, works great and I was able to use it to figure out the data. Appreciate the help! Scot
BillBuilt
08-12-08, 11:43 PM
Scot,
NP. Glad to help :) I can't take credit for the function heehee but i cannot remember who/where i got it. But it sure is handy! Thanks whoever!!
Bill
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.