maverickYQB
maverickYQB

Reputation: 11

Parsing element on this XML server response

Greeting, I would like to parse in python the val="" of this XML string returned from a server

Any help would be apreciated

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type='text/xsl' href='/obix/xsl'?>
<real xmlns:c2g="http://www.can2go.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" writable="true" href="http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/" xsi:schemaLocation="http://obix.org/ns/schema/1.0/obix/xsd" xmlns="http://obix.org/ns/schema/1.0" val="71" />

here's my code atm.

#!/usr/bin/python
import requests

value = 71
payload = "<real val=\""+str(value)+"\" />"
url = "http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/"

def getObix():
    r = requests.get(url)
    print r.text


def putObix():
    r = requests.post(url, payload)


getObix()
#putObix()

Upvotes: 0

Views: 325

Answers (2)

maverickYQB
maverickYQB

Reputation: 11

I found it if anyone is interested

#!/usr/bin/python

import requests
from xml.etree import ElementTree

url = "http://192.168.1.133/obix/network/N002061/DEV100/AV1/"

def getObix():
    r = requests.get(url)
    xml = ElementTree.fromstring(r.content)

    print "AV1 Descriptor = "+xml[2].attrib["val"]
    print "AV1 Present_Value = "+xml[5].attrib["val"]
    print "AV1 Display = "+xml[6].attrib["display"]
    print "AV1 Units = "+xml[7].attrib["val"]

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

You could use BeautifulSoup.

>>> from bs4 import BeautifulSoup
>>> s = '''<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type='text/xsl' href='/obix/xsl'?>
<real xmlns:c2g="http://www.can2go.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" writable="true" href="http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/" xsi:schemaLocation="http://obix.org/ns/schema/1.0/obix/xsd" xmlns="http://obix.org/ns/schema/1.0" val="71" />'''
>>> soup = BeautifulSoup(s)
>>> [i['val'] for i in soup.find_all('real')]
['71']

Upvotes: 1

Related Questions