patternpythonMinor
Extracting the text of a specific XML node
Viewed 0 times
thenodetextxmlextractingspecific
Problem
I have to extract
Here's my current solution:
Is there any better way to do this (maybe any other way which does not involve iteration)? Could I use XPath?
XML content:
`
1
0
DMR-1.50
urn:schemas-upnp-org:device:MediaRenderer:1
My Product 912496
embedded
http://www.embedded.com
Product
Product
http://www.embedded.com
uuid:93b2abac-cb6a-4857-b891-002261912496
urn:schemas-upnp-org:service:ConnectionManager:1
urn:upnp-org:serviceId:ConnectionManager
/xml/ConnectionManager.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager
/Control/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager
urn:schemas-upnp-org:service:AVTransport:1
urn:upnp-org:serviceId:AVTransport
/xml/AVTransport2.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelAVTransport
/Control/org.mpris.MediaPlayer2.mansion/RygelAVTransport
urn:schemas-upnp-org:service:RenderingControl:3
urn:upnp-org:serviceId:RenderingControl
/xml/RenderingControl2.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelRenderingControl
/Control/org.mpris.MediaPlayer2.mansion/RygelRenderingControl
urn:schemas-embedded-com:service:RTSPGateway:1
urn:embedded-com:serviceId:RTSPGateway
/xml/RTSPGateway.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway
/Control/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway
urn:schemas-embedded-com:service:SpeakerManagement:1
u
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.textIs there any better way to do this (maybe any other way which does not involve iteration)? Could I use XPath?
XML content:
`
1
0
DMR-1.50
urn:schemas-upnp-org:device:MediaRenderer:1
My Product 912496
embedded
http://www.embedded.com
Product
Product
http://www.embedded.com
uuid:93b2abac-cb6a-4857-b891-002261912496
urn:schemas-upnp-org:service:ConnectionManager:1
urn:upnp-org:serviceId:ConnectionManager
/xml/ConnectionManager.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager
/Control/org.mpris.MediaPlayer2.mansion/RygelSinkConnectionManager
urn:schemas-upnp-org:service:AVTransport:1
urn:upnp-org:serviceId:AVTransport
/xml/AVTransport2.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelAVTransport
/Control/org.mpris.MediaPlayer2.mansion/RygelAVTransport
urn:schemas-upnp-org:service:RenderingControl:3
urn:upnp-org:serviceId:RenderingControl
/xml/RenderingControl2.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelRenderingControl
/Control/org.mpris.MediaPlayer2.mansion/RygelRenderingControl
urn:schemas-embedded-com:service:RTSPGateway:1
urn:embedded-com:serviceId:RTSPGateway
/xml/RTSPGateway.xml
/Event/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway
/Control/org.mpris.MediaPlayer2.mansion/RygelRTSPGateway
urn:schemas-embedded-com:service:SpeakerManagement:1
u
Solution
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
The
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)).textThe
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.Code Snippets
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)).textContext
StackExchange Code Review Q#50909, answer score: 2
Revisions (0)
No revisions yet.