Reputation: 47
I have a project where I connect to an API that puts out the data in XML. I can grab the values of the tags and print those out but want to grab the whole line of specific lines in the XML and write it out to an XML file. How is the best way to do this in Python? I don't really have any code to share as I'm unsure how to write this.
Here is an example of the XML file or output given by the api:
<?xml version="1.0" ?>
<Product ParentID="XXX" ID="XXX" title="XXX">
<Values>
<Value AttributeID="ABC" title="ABC1">ABC</Value>
<Value AttributeID="DEF" title="DEF1">DEF</Value>
<Value AttributeID="GHI" title="GHI1">GHI</Value>
</Values>
</Product>
I would want to write the xml file to read like this:
<?xml version="1.0" ?>
<Product ParentID="XXX" ID="XXX" title="XXX">
<Values>
<Value AttributeID="ABC" title="ABC1">ABC</Value>
<Value AttributeID="GHI" title="GHI1">GHI</Value>
</Values>
</Product>
Upvotes: 1
Views: 1353
Reputation: 24930
You can get there using lxml with xpath:
from lxml import etree
products = """[your xml above]"""
doc = etree.XML(products)
values = doc.xpath('//Value')
for value in values:
if value.text!="ABC" and value.text!="GHI":
#alternatively:
if value.text=="DEF":
value.getparent().remove(value)
print(etree.tostring(doc).decode())
Output:
<Product ParentID="XXX" ID="XXX" title="XXX">
<Values>
<Value AttributeID="ABC" title="ABC1">ABC</Value>
<Value AttributeID="GHI" title="GHI1">GHI</Value>
</Values>
</Product>
Upvotes: 1
Reputation: 94
In my experience I've always used a parser of some kind to parse the text and save it as an object with attributes accessible like any other array or list.
Here is a link to python 3's xml element tree parser which will create an object based on your xml input that can be accessed using indices or string keys.
https://docs.python.org/3/library/xml.etree.elementtree.html
I hope this and the examples help!
Upvotes: 0