Forum Discussion

DukeOfMarshall's avatar
11 years ago

XML Parsing Structure

So the documentation for the roXMLElement object and parsing is fairly quiet and minimal on this issue.

http://sdkdocs.roku.com/display/sdkdoc/roXMLElement

Given an example XML file


<?xml .... ?>
<feed>
<foo att="something">
<bar>text</bar>
<test>text</test>
</foo>
<foo att="something2">
<bar>text2</bar>
<test>text2</test>
</foo>
</feed>


What does the structure look like when parsed through the roXMLElement command?


rsp = CreateObject("roXMLElement")
rsp.Parse(html)


I know you can call it like "rsp.foo[1]" and "rsp.foo[1].bar, but how do you extend that to attributes?

7 Replies

  • "DukeOfMarshall" wrote:
    I know you can call it like "rsp.foo[1]" and "rsp.foo[1].bar, but how do you extend that to attributes?

    Attributes are accessed either via the @ shortcut or the GetAttributes() method. So, given you example above, you can get to "att" on the first "foo" element with "rsp.foo[0]@att".
  • Thanks for the help. I'm calling it like this "rsp.foo[0]@att.gettext()", but the debugger reports


    Member function not found in BrightScript Component or interface


    Do I have something else incorrect here?
  • So here's a follow up. Why does this not work when simply printing to the debugger


    print "Image => "+rsp.foo[0]@att.gettext()


    But this DOES work when printing to debugger


    print "Image => "+rsp.foo[0]@att.tostr()


    But neither work when setting the attribute


    o.SDPosterUrl = rsp.foo[0]@att.gettext()
    o.HDPosterUrl = rsp.foo[0]@att.tostr()


    Instead I have to do this


    hd_image = rsp.foo[0]@att
    o.HDPosterUrl = hd_image


    What in the world?
  • "DukeOfMarshall" wrote:
    So here's a follow up. Why does this not work when simply printing to the debugger


    print "Image => "+rsp.foo[0]@att.gettext()


    But this DOES work when printing to debugger


    print "Image => "+rsp.foo[0]@att.tostr()


    But neither work when setting the attribute


    o.SDPosterUrl = rsp.foo[0]@att.gettext()
    o.HDPosterUrl = rsp.foo[0]@att.tostr()


    Instead I have to do this


    hd_image = rsp.foo[0]@att
    o.HDPosterUrl = hd_image


    What in the world?

    The @ shortcut returns a string, not an roXmlElement. Calling .toStr() is redundant in this case. This should work just fine:
    o.SDPosterUrl = rsp.foo[0]@att
    o.HDPosterUrl = rsp.foo[0]@att
  • If you prefer working with nested arrays and dictionaries instead, you can try this converter viewtopic.php?f=34&t=75809#p462597 - on your example it will return
    [ 
    { },
    { att: "something", bar: "text", test: "text" },
    { att: "something2", bar: "text2", test: "text2" }
    ]
    and then can access as
    o.HDPosterUrl = rsp[1].att
  • Native is always better and it's faster not using plugins. But thanks for the information anyways.