# Help with XML Parsing

**URL:** <https://forums.plex.tv/t/help-with-xml-parsing/8075>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [March 1, 2011, 9:34pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075 "2011-03-01T21:34:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonitonae](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@tonitonae](https://forums.plex.tv/u/tonitonae)\
**Post date:** [March 1, 2011, 9:34pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/1 "2011-03-01T21:34:30Z")

</div>

Hi!  
  
  
  
I’m playing around with the plugins capabilities of Plex. I more or less understand the structure and the workflow of plugins, I can do general programming in Python, but I don’t know too much on XML.  
  
  
  
I’m trying to parse the contents of a query to the Spotify public API, which is in fact an HTML file as the following one:

```auto

<br />
<br />
<?xml version="1.0" encoding="utf-8"?> <br />
<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> <br />
  <opensearch:Query role="request" startPage="1" searchTerms="u2"/> <br />
  <opensearch:totalResults>10</opensearch:totalResults> <br />
  <opensearch:startIndex>0</opensearch:startIndex> <br />
  <opensearch:itemsPerPage>100</opensearch:itemsPerPage> <br />
  <artist href="spotify:artist:51Blml2LZPmy7TTiAg47vQ"> <br />
    <name>U2</name> <br />
    <popularity>0.86445</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:3CtDccxbY3gFwNHTjRM9aq"> <br />
    <name>Various Artists - U2 Tribute</name> <br />
    <popularity>0.42919</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:5V1upZJErBcgcOP9ZHd4Vy"> <br />
    <name>Made famous by U2</name> <br />
    <popularity>0.34118</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:3dhoDqkI6atVLE43nkx8VZ"> <br />
    <name>LMC vs U2</name> <br />
    <popularity>0.33760</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:7JgPUKQFABI9fZlYIuNJmY"> <br />
    <name>Karaoke - U2</name> <br />
    <popularity>0.30951</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:79sIFMbGMA0vvACW90IwUt"> <br />
    <name>U2;Willie Nelson;Mickey Raphael</name> <br />
    <popularity>0.30162</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:7ypJwUskH6rxpPPCDTdV5V"> <br />
    <name>U2 Tribute Band</name> <br />
    <popularity>0.28478</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:0I1dbLRjzgDmDSLllie5aS"> <br />
    <name>The Allstars Cover LMC vs. U2</name> <br />
    <popularity>0.26643</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:02WKZN2nUu91lcaheGVcd8"> <br />
    <name>Bossa n U2 by Michael Sound</name> <br />
    <popularity>0.25545</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:4KaIn2quiXipktScBBt3jI"> <br />
    <name>Karaoke - U2 / Paul McCartney</name> <br />
    <popularity>0.20478</popularity> <br />
  </artist> <br />
</artists> <br />

```

  
  
My question is: could somebody help writing a parser for such a structure? It shouldn't be difficult for somebody familiar with XML, but for me it will mean struggling many hours with the XML library for a quite small piece of code.  
  
Thanks in advance for your valuable time.  
  
Regards.

---

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [March 1, 2011, 9:56pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/2 "2011-03-01T21:56:41Z")

</div>

are you able to parse html inside of a plugin? the difference is very minor.

---

<div class="post-metadata">

**Author:** ![tonitonae](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@tonitonae](https://forums.plex.tv/u/tonitonae)\
**Post date:** [March 1, 2011, 10:04pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/3 "2011-03-01T22:04:25Z")

</div>

Not actually… My problem is not technical, but conceptual.  
  
  
  
I’m trying with a code I haven taken from another place:

```auto

<br />
    data = XML.ElementFromURL("http://ws.spotify.com/search/1/artist?q=U2")<br />
    PLUGIN_NAMESPACE = {'opensearch':'http://a9.com/-/spec/opensearch/1.1/'}<br />
    Log(data.xpath('//artist', namespaces=PLUGIN_NAMESPACE))<br />

```

  
  
But it (obviously does not work). What I don't see is how should I use namespaces when querying the XML object. In the XML that I get in response, the xml starts with:  
  

```auto

<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> 

```

  
  
What I have understood from examples is that I should use the namescape "opensearch':'http://a9.com/-/spec/opensearch/1.1/" when querying for "totalResults", "startIndex", etc., I am wrong?  
  
But, what about the artist blocks? I don't see how can I use xpath to obtain the list of artist in that XML?  
  
I hope I explain better now.  
  
Thanks for the quick response.

---

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [March 1, 2011, 10:15pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/4 "2011-03-01T22:15:41Z")

</div>

check out the deviant art plugin for a reference on namespaces. you can put both namespaces into one variable ( i believe that’s the right terminology) I helped pooploser write that one for his first plugin. It looks awefully similar to what you are trying to do.

---

<div class="post-metadata">

**Author:** ![tonitonae](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@tonitonae](https://forums.plex.tv/u/tonitonae)\
**Post date:** [March 1, 2011, 11:51pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/5 "2011-03-01T23:51:11Z")

</div>

I see. My problem was that I did no try to add a name to the anonymous namespace in my XML. Now I correctly parse the XML. Thanks!!!  
  
  
  
Now I have another problem. I’m creating TrackItems for each of the parsed tracks. This TrackItems call a callback (this works) but I do not know what return here. My idea is to call a helper from the callback function which carries out some task and the return. But, what should I return from the callback? Plex is not intended to reproduce anything, but to just launch the actual reproductor in background. Is all this possible?  
  
  
  
Thanks again!

---

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [March 1, 2011, 11:56pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/6 "2011-03-01T23:56:28Z")

</div>

After a little googling, it turns out there is a spotify app for plex. I would imagine that a lot of what you need help with (helper app) and what you may need help with in the future will be in that code as examples for you.

---

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [March 2, 2011, 12:43am UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/7 "2011-03-02T00:43:30Z")

</div>

After re-reading my last post, I don’t want you to think I don’t want to help. I just wanted to point out a good resource for you. If you have any questions after looking at the spotify app, please continue to post them.

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [March 2, 2011, 12:59am UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/8 "2011-03-02T00:59:19Z")

</div>

Hi tonitonae!

> [@](#):
>
> I'm creating TrackItems for each of the parsed tracks. This TrackItems call a callback (this works) but I do not know what return here.

  
The callback function has to return a url to the location of the actual audio file.

---

<div class="post-metadata">

**Author:** ![tonitonae](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@tonitonae](https://forums.plex.tv/u/tonitonae)\
**Post date:** [March 2, 2011, 6:38am UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/9 "2011-03-02T06:38:13Z")

</div>

> [@](#):
>
> After re-reading my last post, I don't want you to think I don't want to help. I just wanted to point out a good resource for you. If you have any questions after looking at the spotify app, please continue to post them.

  
  
No problem. I will take a look at the Spotify plugin, although my approach is a bit different (I'm only using the open API to query for music information, not the API to access directly to the music). Anyway, thanks for trying helping. Most of the times there's only the need for somebody to point in the right direction to find out what you were doing wrong.  
  
Regards

---

<div class="post-metadata">

**Author:** ![tonitonae](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@tonitonae](https://forums.plex.tv/u/tonitonae)\
**Post date:** [March 2, 2011, 6:40am UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/10 "2011-03-02T06:40:35Z")

</div>

Hi!,  
  
  
  
thanks for your response.

> [@](#):
>
> Hi tonitonae!  
>   
>   
> The callback function has to return a url to the location of the actual audio file.

  
  
Ok. So, what if I do not want to actually reproduce the music file with Plex but to launch a helper which does it in the background (without gaining focus). Is this possible? Should I use another Media Item to show the results of my query?  
  
Regards, and thanks again for your time.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 9:01pm UTC](https://forums.plex.tv/t/help-with-xml-parsing/8075/11 "2019-12-20T21:01:18Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
