4
\$\begingroup\$

I have to extract friendlyName from the XML document.

Here's my current solution:

root = ElementTree.fromstring(urllib2.urlopen(XMLLocation).read())        
for child in root.iter('{urn:schemas-upnp-org:device-1-0}friendlyName'):
    return child.text

Is there any better way to do this (maybe any other way which does not involve iteration)? Could I use XPath?


XML content:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
   <specVersion>
      <major>1</major>
      <minor>0</minor>
   </specVersion>
   <device>
      <dlna:X_DLNADOC xmlns:dlna="urn:schemas-dlna-org:device-1-0">DMR-1.50</dlna:X_DLNADOC>
      <deviceType>urn:schemas-upnp-org:device:MediaRenderer:1</deviceType>
      <friendlyName>My Product 912496</friendlyName>
      <manufacturer>embedded</manufacturer>
      <manufacturerURL>http://www.embedded.com</manufacturerURL>
      <modelDescription>Product</modelDescription>
      <modelName>Product</modelName>
      <modelNumber />
      <modelURL>http://www.embedded.com</modelURL>
      <UDN>uuid:93b2abac-cb6a-4857-b891-002261912496</UDN>
      <serviceList>
         <service>
            <serviceType>urn:schemas-upnp-org:service:ConnectionManager:1</serviceType>
            <serviceId>urn:upnp-org:serviceId:ConnectionManager</serviceId>
            <SCPDURL>/xml/ConnectionManager.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager</controlURL>
         </service>
         <service>
            <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
            <serviceId>urn:upnp-org:serviceId:AVTransport</serviceId>
            <SCPDURL>/xml/AVTransport2.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelAVTransport</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelAVTransport</controlURL>
         </service>
         <service>
            <serviceType>urn:schemas-upnp-org:service:RenderingControl:3</serviceType>
            <serviceId>urn:upnp-org:serviceId:RenderingControl</serviceId>
            <SCPDURL>/xml/RenderingControl2.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelRenderingControl</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelRenderingControl</controlURL>
         </service>
         <service>
            <serviceType>urn:schemas-embedded-com:service:RTSPGateway:1</serviceType>
            <serviceId>urn:embedded-com:serviceId:RTSPGateway</serviceId>
            <SCPDURL>/xml/RTSPGateway.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway</controlURL>
         </service>
         <service>
            <serviceType>urn:schemas-embedded-com:service:SpeakerManagement:1</serviceType>
            <serviceId>urn:embedded-com:serviceId:SpeakerManagement</serviceId>
            <SCPDURL>/xml/SpeakerManagement.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelSpeakerManagement</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelSpeakerManagement</controlURL>
         </service>
         <service>
            <serviceType>urn:schemas-embedded-com:service:NetworkManagement:1</serviceType>
            <serviceId>urn:embedded-com:serviceId:NetworkManagement</serviceId>
            <SCPDURL>/xml/NetworkManagement.xml</SCPDURL>
            <eventSubURL>/Event/org.mpris.MediaPlayer2.mansion/RygelNetworkManagement</eventSubURL>
            <controlURL>/Control/org.mpris.MediaPlayer2.mansion/RygelNetworkManagement</controlURL>
         </service>
      </serviceList>
      <iconList>
         <icon>
            <mimetype>image/png</mimetype>
            <width>120</width>
            <height>120</height>
            <depth>32</depth>
            <url>/org.mpris.MediaPlayer2.mansion-120x120x32.png</url>
         </icon>
         <icon>
            <mimetype>image/png</mimetype>
            <width>48</width>
            <height>48</height>
            <depth>32</depth>
            <url>/org.mpris.MediaPlayer2.mansion-48x48x32.png</url>
         </icon>
         <icon>
            <mimetype>image/jpeg</mimetype>
            <width>120</width>
            <height>120</height>
            <depth>24</depth>
            <url>/org.mpris.MediaPlayer2.mansion-120x120x24.jpg</url>
         </icon>
         <icon>
            <mimetype>image/jpeg</mimetype>
            <width>48</width>
            <height>48</height>
            <depth>24</depth>
            <url>/org.mpris.MediaPlayer2.mansion-48x48x24.jpg</url>
         </icon>
      </iconList>
      <X_embeddedDevice xmlns:edd="schemas-embedded-com:extended-device-description">
         <firmwareVersion>v1.0 (4.155.1.15.002)</firmwareVersion>
         <features>
            <feature>
               <name>com.sony.Product</name>
               <version>1.0.0</version>
            </feature>
            <feature>
               <name>com.sony.Product.btmrc</name>
               <version>1.0.0</version>
            </feature>
            <feature>
               <name>com.sony.Product.btmrs</name>
               <version>1.0.0</version>
            </feature>
         </features>
      </X_embeddedDevice>
   </device>
</root>
\$\endgroup\$
1
  • \$\begingroup\$ This page contains the following errors: error on line 18 at column 20: Namespace prefix sec on ProductCap is not defined Below is a rendering of the page up to the first error. 1 0 urn:samsung.com:device:FileTransferServer:1 [Phone] SM-G550T1 Samsung Electronics samsung.com file transfer file transfer device 1.0 samsung.com 200807140950 uuid:74c0c86f-7f23-4181-9568-bb132e4ef67a p2pmac=56:f2:01:54:0e:2a; urn:samsung.com:service:FileTransport:1 urn:samsung.com:serviceId:FileTransport /smp_4_ /smp_5_ /smp_3_ \$\endgroup\$ – Leonardo E Jimarez Jan 10 '17 at 2:46
2
\$\begingroup\$

I am going to assume you are using Python's built-in XML library.

There are some basic XPath functionality implemented into Python's XML library. Using XPath and the find() function:

import xml.etree.ElementTree as ElementTree

namespace = '{urn:schemas-upnp-org:device-1-0}'
root = ElementTree.fromstring(urllib2.urlopen(XMLLocation).read())

# The `//` specifies all subelements within the whole tree.
return root.find('.//{}friendlyName'.format(namespace)).text

The find() function stops when it finds the first match. If you wanted to get all of the elements that match the XPath, use the findall() function.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.