I’m having trouble getting some text out of parsed XML. For the most part, everything I need is visible as I walk through the xml, but in the following instance, I can’t get to the text. Let’s say I have an object Parser (type roXMLElement), and a string xml which contains the following:
<xml>
<tag1 class="class1">
<a href="http://www.google.com">Google!</a>
Some more text goes here
</tag1>
</xml>
After parsing this as Parser.Parse(xml), Parser.tag1@class would return “class1”, Parser.tag1.a@href would return “http://www.google.com”, and Parser.tag1.a.getText() would return “Google!”. The problem I’m having is getting to the “Some more text goes here”. I thought that using Parser.tag1.getText() would return that, but it’s returning empty. Am I doing this correctly? Is there a way to get to that text?
“joetesta” wrote:
I bet you can get it if you put another wrapper around it; assuming you need to maintain html compatibility I’d use
No, the question was how to work with the XML as given, not to mangle it server-side. The example is a well-formed XML (i fed it to validator just to be sure) and as such would parse and all content should be available. Question is, how to reach that “Some more text goes here” via roXML* APIs?
I subscribed the topic the other day, thinking there is obvious answer i can learn from. XML elements may contain any text or other elements, or mixture of text and elements in any order; that much i know. But how do we eat it? Time for a lifeline: Ask-the-Expert
I don’t think there’s any way to get orphaned text. I tested this, and while it parses successfully, when you output it with xml.GenXml(False), the orphaned text is gone, so it seems the parser is losing it.
PPS. i can think of alternative representation too, in which will have 3 children, [0] being the string “foo” (or element with empty getName and “foo” as getText), then [1] is as usual, [2] the “qux” text (string or another empty tag). Then ifXMLElement.getText() will have to be clarified to “returns the first text contained in the element”. This is less hacky and more to the spirit of xml but likely requires more changes in parser and may surprise some existing BRS code that is very stuck up on the sequence list it gets from getChildElements().
“joetesta” wrote:
I bet you can get it if you put another wrapper around it; assuming you need to maintain html compatibility I’d use
No, the question was how to work with the XML as given, not to mangle it server-side.
It may be well formed XML and I may not have answered the question, but if you need this to happen now, I bet double or nothing my solution works.
“joetesta” wrote:
I bet you can get it if you put another wrapper around it; assuming you need to maintain html compatibility I’d use
No, the question was how to work with the XML as given, not to mangle it server-side.
It may be well formed XML and I may not have answered the question, but if you need this to happen now, I bet double or nothing my solution works.
Sure, if you have control over the server side. What if the XML is coming from some embedded device or other server you have no control over?
“joetesta” wrote:
I bet you can get it if you put another wrapper around it; assuming you need to maintain html compatibility I’d use
and see if you can get it with Parser.tag1.span.getText()
I ended up working around my problem using essentially this approach. In my actual XML, there was a tag after the which I didn’t need, so I used a roRegex to find all instances and replace them with the following: . After the replace all, my new code looked like this (minus all the nice formatting):
<xml>
<tag1 class="class1">
<a href="http://www.google.com">Google!</a>
</tag1>
<tag1 class="text">
Some more text goes here
</tag1>
</xml>
I was then able to get to the text I needed with Parser.tag1[1].getText(). I still would be interested in how this is supposed to be done using the xml api.
PS-I don’t have control over the server-side output. In a bit of an “Aha!” moment, I thought of the approach that Joetesta suggested.
It may be well formed XML and I may not have answered the question, but if you need this to happen now, I bet double or nothing my solution works.
Sure, if you have control over the server side. What if the XML is coming from some embedded device or other server you have no control over?
Then you’d be up a creek. Fortunately for wlwest82 that wasn’t the case
genXML should have reconstituted (more or less) the original but seems Foo and Qux have been lost in translation. Even Foo, that should’ve been the getText() to
Somebody with Roku* name, please respond: How are such text handled with roXML?
Update: just noticed that a new method has sprung, ifXMLElement.GetChildNodes() - and it seems designed to address the issue from this thread.
Other changes have been made to roXMLElement too: parsing might have been improved so that additional texts don’t get lost; getText() now seems to concatenate any such texts. An example is worth a thousand words:
BrightScript Debugger> x = CreateObject("roXMLElement")
BrightScript Debugger> x.parse("<xml> foo <tag> bar </tag> qux </xml>")
BrightScript Debugger> ? x.genXML(false) 'before, the output of this was not right
<xml> foo <tag> bar </tag> qux </xml>
BrightScript Debugger> ? x.getText()
foo qux
BrightScript Debugger> ? x.getChildElements()
<Component: roXMLElement>
BrightScript Debugger> ? x.getChildNodes()
foo
<Component: roXMLElement>
qux
It’s good news. My hopes that somebody in RokuCo is listening are rekindled!
Unfortunately nobody thought of dropping a note here for us to know about the change nor did mention it in Release Notes.
Which firmware does the new method work in, shouldn’t this be documented?