How can I parse the below XML data using Python? - python

Source XML
<?xml version='1.0' encoding='UTF-8'?>
<ProcessType xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" defaultContext="Default">
<node componentName="tRedshiftRow" componentVersion="0.102" offsetLabelX="0" offsetLabelY="0" posX="-32" posY="96">
<elementParameter field="TECHNICAL" name="QUERYSTORE:QUERYSTORE_TYPE" value="BUILT_IN"/>
<elementParameter field="TEXT" name="DBNAME" value=""""/>
<elementParameter field="TEXT" name="SCHEMA_DB" value=""""/>
<elementParameter field="MEMO_SQL" name="QUERY" value=""DELETE FROM schema.tablename;""/>
</node>
</ProcessType>
I want to get the DELETE statement only where tag is "QUERY", and write it in a text file.
Expected output : DELETE FROM schema.tablename;
I was trying the following way, which obviously didn't work out !
from lxml import etree, objectify
import xml.etree.ElementTree as ET
def convert_xml_to_comp():
metadata = 'source.xml'
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(metadata, parser)
root = tree.getroot()
for elem in root.getiterator():
# print(elem)
i = elem.tag.find('}')
if i >= 0:
elem.tag = elem.tag[i+1 :]
objectify.deannotate(root, cleanup_namespaces=True)
tree.write('done.xml', pretty_print=True, xml_declaration=True, encoding='UTF-8')
tree = ET.parse('done.xml')
root = tree.getroot()
def get_sql_text():
file = open( "newdelete.txt", "w")
for root in tree.getroot():
### Get the elements' names ###
for elementParameter in root.iterfind('elementParameter[#name="UNIQUE_NAME"]') :
name=elementParameter.get('value')
### Get the elements' name and SQL ###
for elementParameter in root.iterfind('elementParameter[#name="QUERY"]') :
#print (root.attrib)
val=elementParameter.get('value')
print(root.find('val[#value="DELETE FROM schema.tablename;"]'))
file.close()
get_sql_text()
if __name__ == '__main__':
convert_xml_to_comp()

You do this all in a just a couple of statements using an xpath query. Something like:
>>> from lxml import etree
>>> doc = etree.parse(open('data.xml'))
>>> query = doc.xpath('//elementParameter[#name="QUERY"]')[0].get('value')
>>> print(query)
"DELETE FROM schema.tablename;"
This says "find all the elementParameter elements with name="QUERY" and then return the value of the value attribute of the first one.
To select just those elements that contain "DELETE" in their value attribute, use the contains() function:
>>> doc.xpath('//elementParameter[#name="QUERY" and contains(#value, "DELETE")]')

Related

Python XML: Getting unnecessary new lines when adding new element

Basically I'm trying to add a new element and for it to be properly indented, but with this code I get unnecessary new lines between elements. What is causing it and how do I fix it? Thanks
Example:
from xml.dom import minidom
import xml.etree.ElementTree as ET
def example(name, category):
tree = ET.parse("example1.xml")
root = tree.getroot()
for i in root:
if i.tag == category:
ET.SubElement(i, name).text = name
xmlStr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
with open("example1.xml", "w") as f:
f.write(xmlStr)
example("test", 'FRUITS')
XML File:
<?xml version="1.0" ?>
<root>
<FRUITS>
<APPLE>apple</APPLE>
<PEAR>pear</PEAR>
<PLUM>plum</PLUM>
</FRUITS>
<VEGETABLES>
<CARROT>carrot</CARROT>
<POTATO>potato</POTATO>
</VEGETABLES>

Want to remove all children when removing an element with lxml

Can't seem to figure out how to remove the element 'framelineName' and all the sub-elements attached to it. Bottom area in the else statement will only delete the element framelineName. I want to also delete 'line', 'left', and 'right'.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lxml import etree as ET
def cash_rules_everything_around_me():
shaolin = ET.SubElement(root, "Shaolin")
wtClan = ET.SubElement(root, "wtClan")
wtClan.set('StatenIsland', 'NYC')
RZA = ET.SubElement(shaolin, "RZA")
RZA.set('StatenIsland', 'NYC')
gf = ET.SubElement(RZA, "GhostfaceKillah")
rk = ET.SubElement(RZA, "Raekwon")
wutang = "36 chambers"
for wu in wutang:
if wu != "36 chambers":
wtClan.text = "A Tribe Called Quest"
else:
for w in root.xpath("//wtClan [#StatenIsland=\'NYC']"):
w.getparent().remove(w)
tree = ET.ElementTree(root)
tree.write("wutang.xml", pretty_print=True, xml_declaration=True, encoding='UTF-8')
if __name__ == '__main__':
root = ET.Element("HipHop")
cash_rules_everything_around_me()
To remove an element you need the actual element object not a list which is the return of lxml's xpath. Consider findall for iterating through element and move xpath logic to an if statement:
...
# ITERATE THROUGH A LIST (NOT STRING)
for wu in [wutang]:
if wu != "36 chambers":
wtClan.text = "A Tribe Called Quest"
else:
for w in root.findall("//wtClan"):
if w.attributed['StatenIsland']
root.remove(w)
tree = ET.ElementTree(root)
tree.write("wutang.xml", pretty_print=True,
xml_declaration=True, encoding='UTF-8')
Rextester demo (using built-in etree but compatible with lxml)

Python Parsing xml files

<goal>
<value>
<comment>n</comment>
<stats>
<goals>1</goals>
<shoton>1</shoton>
</stats>
<event_incident_typefk>406</event_incident_typefk>
<elapsed>22</elapsed>
<player2>38807</player2>
<subtype>header</subtype>
<player1>37799</player1>
<sortorder>5</sortorder>
<team>10261</team>
<id>378998</id><n>295</n>
<type>goal</type>
<goal_type>n</goal_type>
</value>
<value>
<comment>n</comment>
<stats>
<goals>1</goals>
<shoton>1</shoton>
</stats>
<event_incident_typefk>393</event_incident_typefk>
<elapsed>24</elapsed>
<player2>24154</player2>
<subtype>shot</subtype>
<player1>24148</player1>
<sortorder>4</sortorder>
<team>10260</team>
<id>379019</id><n>298</n>
<type>goal</type>
<goal_type>n</goal_type>
</value>
def extract_goal_type(data):
#print(data)
if data :
root = ET.fromstring(data)
tree = ET.ElementTree(root)
root = tree.getroot()
for c in root.getchildren():
return c.findtext('subtype')
the problem is that the function is only returning the first subset and l can't seem to get the other ... l also have XML files which have up to 6 children. Please help on how l can parse this to get everything which is in XML.... Thank you
expected out
header
shot
You are retuning from the for loop, you have to travers for all element then return data.
def extract_goal_type(data):
#print(data)
ret_data = []
if data :
root = ET.fromstring(data)
tree = ET.ElementTree(root)
root = tree.getroot()
for c in root.getchildren():
ret_data.append(c.findtext('subtype'))
return ret_data
Note: This will only work if you have valid XML

Normalize XML text node in Python minidom

I want to insert this string:
No, on the 5<Font Script="super">th</Font>
as a Text Node in XML by xml.dom.minidom createTextNode(), however, after I writexml() to a file, the signs:
< > "
turns to:
No, on the 5<Font Script="super">th</Font>
How can I avoid this? Thanks.
A part of my code:
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, None, None)
TextTextNode = dom.createTextNode(text.decode("utf-8"))
Text = dom.createElement("Text")
Text.appendChild(TextTextNode)
fileToWrite = codecs.open(output, 'w', encoding='utf-8')
dom.writexml(fileToWrite, indent=" ", addindent=" ", newl="\n", encoding='utf-8')
fileToWrite.close()
There is a sample for this by the cinecanvase specification:
<Text HAlign=”left” HPosition=”10.2” VAlign=”bottom” VPosition=”10.0”> This <Font Script=”super”>word </Font>is superscript </Text >
I need insert the <Font>..</Font> into another element, the .
I'm not familiar with that format, but that thing looks like an XML node. Try this:
from xml.dom import minidom
import codecs
output = "test.xml"
text="No, on the 5"
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, None, None)
FontNode = dom.createElement("Font")
FontNode.setAttribute('Script', 'super')
FontNode.appendChild(dom.createTextNode('th'))
Text = dom.createElement("Text")
TextTextNode = dom.createTextNode(text.decode("utf-8"))
Text.appendChild(TextTextNode)
Text.appendChild(FontNode)
fileToWrite = codecs.open(output, 'w', encoding='utf-8')
Text.writexml(fileToWrite, indent=" ", addindent=" ", newl="\n")
fileToWrite.close()
That outputs:
<Text>
No, on the 5
<Font Script="super">th</Font>
</Text>
Be aware that what you want to write a tree in a file (when you call writexml) you need to call the writexml method with your XML's tree root (you were calling it with dom, not with your root node)

variable in XML subelement

I'm thinking of Python code to create a dynamic xml ETREE subElement.
I have a hierarchical header to describe a peace of book as the following:
<Books>
<Booktype List= "Story > Fiction > Young">
#here the rest of book text
</Booktype>
<Booktype List= "Science > Math > Young">
#here the rest of book text
</Booktype>
</Books>
How to get a hierarchical xml tag like this :
<Books>
<Booktype>
<Story>
<Fiction>
<Young>
#here the rest of book text
</Young>
</Fiction>
</Story>
</Booktype>
</Books>
This is my code:
import re
import xml.etree.ElementTree as ET
from xml.etree import ElementTree
List= "Story>Fiction>Young"
List = List.split('>')
root = ET.Element('Books')
Booktype =ET.SubElement(root,'Booktype')
for l in List:
ND = ET.SubElement(Booktype,str(l))
Booktype.append(ND)
tree = ET.ElementTree(root)
ElementTree.tostring(root,'utf-8')
I got this bad result:
'<Books><Booktype><Story /><Story /><Story /><Fiction /><Fiction /><Young /><Young /><Story /><Story /><Fiction /><Fiction /><Young /><Young /></Booktype></Books>'
If you want to nest the list elements you have to keep the reference to the previous one so you can add the child element to it, and not to the Booktype element. See the variable currrent in the examples.
from xml.etree import ElementTree as ET
xml_string = '''<Books>
<Booktype List= "Story > Fiction > Young">
#here the rest of book text
</Booktype>
<Booktype List= "Science > Math > Young">
#here the rest of book text 2
</Booktype>
</Books>
'''
xml = ET.fromstring(xml_string)
for booktype in xml.findall('Booktype'):
types = map(lambda x: x.strip(), booktype.get('List').split('>'))
current = booktype
for t in types:
current = ET.SubElement(current, t)
current.text = booktype.text
booktype.text = ''
del booktype.attrib['List']
print ET.tostring(xml,'utf-8')
Gives me the result:
<Books>
<Booktype><Story><Fiction><Young>
#here the rest of book text
</Young></Fiction></Story></Booktype>
<Booktype><Science><Math><Young>
#here the rest of book text 2
</Young></Math></Science></Booktype>
</Books>
And if you want to create a completely new structure you can do:
xml = ET.fromstring(xml_string)
root = ET.Element('Books')
for booktype in xml.findall('Booktype'):
current = ET.SubElement(root, 'Booktype')
for t in map(lambda x: x.strip(), booktype.get('List').split('>')):
current = ET.SubElement(current, t)
current.text = booktype.text
print ET.tostring(root, 'utf-8')

Categories

Resources