Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
btpoole
Level 7

Parse XML with multiple tags of same name

How is it possible to extract the value from an xml file that uses the same name for multiple elements? In the xml example below I would like to parse out either the first or third "display-name" but have been unsuccessful. I can parse other parts of the file with no problem using code as follows:

date: xml.myxmlfile[i]@start

title: xml.myxmlfile[i].title.gettext()



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE my SYSTEM "xmlmy.dtd">

<video id="I4.28458309">
<display-name>4 XXXX</display-name>
<display-name>4</display-name>
<display-name>XXXX</display-name>
<display-name>MY VIDEO</display-name>
</video>
0 Kudos
7 Replies
TheEndless
Level 7

Re: Parse XML with multiple tags of same name

nameElements = xml.getNamedElements("display-name")
?nameElements[0].getText()
?nameElements[1].getText()
...
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
btpoole
Level 7

Re: Parse XML with multiple tags of same name

Thanks for the help, works great.
0 Kudos
btpoole
Level 7

Re: Parse XML with multiple tags of same name

I have encountered something with the parse I am not really sure how to fix.
The suggestion by Endless works great but with one exception. The format of xml is like below, you will notice the number of "display-name" elements can vary from video id to video id.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE my SYSTEM "xmlmy.dtd">

<video id="I4.28458309">
<display-name>4 XXXX</display-name>
<display-name>4</display-name>
<display-name>XXXX</display-name>
<display-name>MY VIDEO</display-name>
</video>
<video id="I4.3254785">
<display-name>5 UUUU</display-name>
<display-name>5</display-name>
<display-name>UUUU</display-name>
<display-name>5U5U5U</display-name>
<display-name>ANOTHER VIDEO</display-name>
</video>

I am using something like the following to extract the values.

nameElements = xml.getNamedElements("display-name")
for i = 0 to xml.video.count()-1
videoinfo={
name: nameElements[i].gettext()
End for


This works but it gathers all the values for all the nameElements. I am only interested in the first value for the first "display-name". Because the number of "display-name" elements vary I have been unable to exact only this value. I have tried numerous ways to narrow the parse but have not been successful because the first instance of "display-name" in video id does not occur in any specific location. Any ideas? A second parse maybe?
0 Kudos
TheEndless
Level 7

Re: Parse XML with multiple tags of same name

Your code isn't right regardless of what value you want. You're looping through the video elements, and using that index to get the display-name element, which is very wrong. You're also getting the display-name elements form the wrong node. Try this instead:

for each video in xml.video
' get the display-name elements from the video node
nameElements = video.getNamedElements("display-name")
videoinfo = {
name: nameElements[0].gettext() ' Use 0 here, since you only want the first element.
}
' do something with your videoinfo object here (i.e., add it to an array)
end for

Technically you should also check to make sure you have some nameElements before you try to grab the first one, as it'll blow up if there aren't any.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
btpoole
Level 7

Re: Parse XML with multiple tags of same name

Thanks again for the help but part of the code will not perform as desired.

name: nameElements[0].gettext() ' Use 0 here, since you only want the first element


This does pull the first element but with each step thru the video in xml.video the first element in the nameElements has a different index number. So, trying to pull the first element for each <display-name> under each video requires the nameElements[] to have an increasing number. The code doesn't "reset" to [0] when a new video is reached, rather it keeps increasing, this is part of the reason I was using a For i= 0 to count, I was increasing the index for each nameElements which works but there is not way to determine where the first element in each video is located since there is not a set number of "display-name" elements in each video. I thought it would also pull just the first element under video with the code you provided and really not sure why it doesn't "reset" to 0 with each video.
0 Kudos
TheEndless
Level 7

Re: Parse XML with multiple tags of same name

"btpoole" wrote:
Thanks again for the help but part of the code will not perform as desired.

name: nameElements[0].gettext() ' Use 0 here, since you only want the first element


This does pull the first element but with each step thru the video in xml.video the first element in the nameElements has a different index number. So, trying to pull the first element for each <display-name> under each video requires the nameElements[] to have an increasing number. The code doesn't "reset" to [0] when a new video is reached, rather it keeps increasing, this is part of the reason I was using a For i= 0 to count, I was increasing the index for each nameElements which works but there is not way to determine where the first element in each video is located since there is not a set number of "display-name" elements in each video. I thought it would also pull just the first element under video with the code you provided and really not sure why it doesn't "reset" to 0 with each video.

Sorry, but what you're saying doesn't make sense (or I'm misunderstanding it). getNamedElements() returns an roXmlList of all of the elements with that name from the node it's requested from, so index 0 will always be the first "display-name" element under the current video node. If that's not the results you're getting, either there's something else wrong in your code, or the XML you're parsing isn't in the same format as the example you provided. Can you provide the exact code you're seeing this behavior with?
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
btpoole
Level 7

Re: Parse XML with multiple tags of same name

Endless, thanks for the help. I tore the code down and started over, found the problem. I had added xml in the line
which I believe was screwing it up. Once I removed that it works like you intended.
Thanks again
0 Kudos