0

I need to rewrite scripts from PowerShell to python and I'm new to python. How can I get value from XML response in python e.g

I have a response in XML:

<?xml version="1.0" encoding="utf-8"?>
<Channels resultCount="1" xmlns="urn:xmlsample:1.0">
  <Channel id="01">
    <Events resultCount="1">
      <Event id="123456789abcdefghij"/>
    </Events>
  </Channel>
</Channels>

I need to get only value from Event id in response

123456789abcdefghij

In PowerShell, I added

[xml]$xml = $req
$xml.Channels.Channel.Events.Event.Id

Is there is an equivalent in python? I only find how to get value from the xml file but I need from response.

For now, I have

import requests

requestIP = 'http://192.168.0.12/channel' # enter IP

def getEventsToRecord():
    body = '<SubQueryOptions xmlns="urn:xmlsample:1.0"><QueryOption path="Events">/filter/AvailabilityStart>=now()&amp;AvailabilityEnd&lt;now(P1D)</QueryOption></SubQueryOptions>'
    r = requests.request("PUT", f'{requestIP}/Channels', data = body)
    print(r.text)
Solaire
  • 23
  • 5
  • Can you use `BeautifulSoup` for parsing the XML? – Andrej Kesely Jun 29 '19 at 16:32
  • All examples are good, but what if I have more than 1 Event? I tried to do that but response that I received is always 1 event – Solaire Jun 29 '19 at 20:44
  • If there are multiple events, then you would loop over them. There are thousands of examples on the internet of looping over a collection in Python. BeautifulSoup's documentation gives examples for retrieving a list of elements. Do some homework; this is not a tutorial site, but a QA site. It is your responsibility to write well researched, complete and verifiable questions. You never once mentioned this in your question. Both answers listed are complete, given the question asked. – Keozon Jun 30 '19 at 13:35

2 Answers2

1

Here (no need to install any external software)

import xml.etree.ElementTree as ET
import re

data = '''<?xml version="1.0" encoding="utf-8"?>
<Channels resultCount="1" xmlns="urn:xmlsample:1.0">
  <Channel id="01">
    <Events resultCount="1">
      <Event id="123456789abcdefghij"/>
    </Events>
  </Channel>
</Channels>
    '''

data = re.sub(' xmlns="[^"]+"', '', data, count=1)
root = ET.fromstring(data)
event_id = root.find('.//Event').attrib['id']
print(event_id)

output

123456789abcdefghij
balderman
  • 22,927
  • 7
  • 34
  • 52
0

If you have pip installed, you can do this very easily with two external packages.

pip install lxml and pip install bs4

lxml is the xml parser that will be used by BeautifulSoup (bs4). After those two are installed, the code is nearly identical to your PowerShell code:

from bs4 import BeautifulSoup
req = """<?xml version="1.0" encoding="utf-8"?>
<Channels resultCount="1" xmlns="urn:xmlsample:1.0">
 <Channel id="01">
    <Events resultCount="1">
      <Event id="123456789abcdefghij"/>
    </Events>
  </Channel>
</Channels>"""
soup = BeautifulSoup(req, "xml")
soup.Channels.Channel.Events.Event["id"]
'123456789abcdefghij'
Keozon
  • 998
  • 10
  • 25