Reputation: 359
I have the given sample code(which contains an xml variable) and I want to read some of the attributes of the Xml and then update them. Once updated I want to post them using requests.post. I get the error as "not well formed" token, and I am not able to parse the xml. please suggest what is wrong in the code.
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as etree
dataxml = """<APIDataMessage MessageID="747950743" SensorID=extref MessageDate=messagedate State="16" SignalStrength=random.randint(40,70) Voltage="2.83" Battery=random.randint(80,90) Data=random.randint(27,40) DisplayData="67.1° F" PlotValue="67.1" MetNotificationRequirements="False" GatewayID="106558" DataValues="19.5" DataTypes="TemperatureData" PlotValues="67.1" PlotLabels="Fahrenheit" />"""
parser = etree.XMLParser(encoding="utf-8")
root = etree.fromstring(dataxml, parser=parser)
root.set('SignalStrength',100)
print etree.tostring(root)
Upvotes: 2
Views: 9695
Reputation: 10213
According to me following need to do in code:
""
e.g. <test id="12">
. This is missng in input SensorID=extref MessageDate=messagedate
set
method i.e. root.set('SignalStrength','100')
Demo:
dataxml = """<APIDataMessage MessageID="747950743" SensorID="extref"\
MessageDate="messagedate" State="16" SignalStrength="%s" \
Voltage="2.83" Battery="%s" Data="%s" DisplayData="67.1° F" PlotValue="67.1" \
MetNotificationRequirements="False" GatewayID="106558" DataValues="19.5" \
DataTypes="TemperatureData" PlotValues="67.1" PlotLabels="Fahrenheit" />"""\
%(random.randint(40,70), random.randint(80,90), random.randint(27,40))
from xml.etree import ElementTree as etree
parser = etree.XMLParser(encoding="utf-8")
root = etree.fromstring(dataxml, parser=parser)
root.set('SignalStrength',"100")
print etree.tostring(root)
Upvotes: 1