Forum Discussion

paul_p's avatar
paul_p
Visitor
14 years ago

Not parsing the entire xml

I have a strange behavior when parsing a large xml (but I'm not sure if the fact that is large is causing the problem).

This is the xml format:

 <?xml version="1.0" encoding="UTF-8"?>
<webservice>
<control>
<operation>get_clips</operation>
<status>0</status>
</control>
<data>
<cat size="4" lang="EN"/>
<cat size="3" lang="EN"/>
' and 19 more other similar 'cat' elements
</data>
</webservice>



and this is the code, where
m.rawResponse
is the above xml

if m.rawResponse <> "" then
xml = CreateObject("roXMLElement")
print "################ "; m.rawResponse ' contains the entire xml
xml.Parse( m.rawResponse )
print "################ "; m.rawResponse ' contains the entire xml

' I've tried like this
categories = xml.data.GetChildElements()
print "number of categories: " ; categories.Count() ' prints 14

' and also like this
i = 0
for each categ in xml.data.cat
i = i +1
end for
print i ' prints 14
end if



The problem is that is only processing 14 cat elements, instead of 21 and I really don't know why.
Please help me with any ideas.
Thanks a lot!

10 Replies

  • Without seeing the full XML, it's hard to say, but I've parse XML files upwards of 5 megabytes in size without issue, so it's unlikely to be an issue with the size of the XML. If it were a size issue, the Parse() itself would almost certainly fail, though you're not checking for that in your code. The Parse() method returns a boolean to indicate whether the parse was successful or not, so you should start by adding a check for a successful result there first.
  • In the XML document, you need to code the ampersand characters using the XML entity reference: &amp;

    For example:

    <director>Donald Nij &amp; Rick Senjin</director>


    Otherwise the XML is not valid and will not parse.
  • renojim's avatar
    renojim
    Community Streaming Expert
    I've run into this same problem. Here's how I fixed it:
        xmlraw = xfer.GetToString()
    re = CreateObject("roRegEx"," & ","")
    xml = re.ReplaceAll(xmlraw," &amp; ")

    -JT
  • "renojim" wrote:
    I've run into this same problem. Here's how I fixed it:
        xmlraw = xfer.GetToString()
    re = CreateObject("roRegEx"," & ","")
    xml = re.ReplaceAll(xmlraw," &amp; ")

    -JT



    Thanks a lot to all of you!!!! It works now perfect!
  • I wonder if I should encode all the special characters which may appear in the xml responses? What do you think? And which exactly these should be ? Thanks again/
  • It really depends on the server. The server should be encoding these characters itself. Otherwise it is sending invalid XML. You can't just encode all the special characters in the XML response, since that will destroy the XML structure. You only want to encode characters within each field. This gets complicated, which is why the server is supposed to do it. Note that your current solution will fail if the server uses any legitimate ampersand escapes like &gt; or &lt; in its content.

    --Mark
  • renojim's avatar
    renojim
    Community Streaming Expert
    "RokuMarkn" wrote:
    Note that your current solution will fail if the server uses any legitimate ampersand escapes like &gt; or &lt; in its content.

    It only replaces an ampersand preceded and followed by a space, so it should be ok. Of course it could still miss an unescaped ampersand if there isn't a preceding and following space.

    -JT
  • destruk's avatar
    destruk
    Streaming Star
    The point is, that if you are using that same xml response for any other platform, iPhone, Android, web requests, etc etc - you will need the same 'post-downloaded fix' in each specific application that will be using it or the xml parse will fail on those too. It's better to correct the xml encoding on the server before the xml is sent out and then you can use the same feed for everything with no problems.

    Of course, if Roku and this app is the only thing that will ever be using it, then use whatever you like.... 🙂
  • I have the same problem but i using the sample code from Roku SDK "videoplayer", I not have experiencie in Roku Api or language.

    Where made the changes mentioned above for correcting parse the ampersand?

    Thanks.