xml.etree.ElementTree how can i add attribute inside of a node? - python

I want the build an xml file and i made some research. I decided the use xml tree but i couldn't manage the use it like i want.
I want the generate this xml.
<Invoice test="how can i generate this ?">
</Invoice>
i am doing in python
import xml.etree.ElementTree as gfg
def GenerateXML(fileName):
root = gfg.Element("Invoice")
root.tail = 'test="how can i generate this ?"'
tree = gfg.ElementTree(root)
with open(fileName, "wb") as files:
tree.write(files)
It's generate xml file look like:
<Invoice />test="how can i generate this ?"
I know i shouln't use tail for i want. But i can't find a way for the make a xml look like what i want. Thank you for help.

This piece of XML structure is called "attribute".
You can get it using the set(attr, value) method.
import xml.etree.ElementTree as gfg
def GenerateXML(fileName):
root = gfg.Element("Invoice")
root.set('test', 'how can i generate this ?')
tree = gfg.ElementTree(root)
with open(fileName, "wb") as files:
tree.write(files)
GenerateXML('test.xml')
test.xml:
<Invoice test="how can i generate this ?" />

Related

I can't read an XML file

It's my first time working with XML files, yet I have a problem with a code as simple as:
from xml.etree import ElementTree as ET
tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree, method = 'xml')
root = tree.getroot()
all I am trying to do here is reading the XML file as a string,
but whenever I try to run this I get an error:
AttributeError: 'ElementTree' object has no attribute 'tag'
I have no idea what I did wrong just yet, so I would need any hint
and thanks in advance
You can't use ET.tostring on the full tree; you can use it on the root element.
from xml.etree import ElementTree as ET
tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree.getroot(), method='xml')

How to write Element Tree dump into file

I am try to write the xml dump into the another file. Here is my python code
import xml.etree.ElementTree as ET
tree = ET.parse('extract_orginal.xml')
root = tree.getroot()
with open('extract.xml', 'w') as extract:
for item in root.findall(f"doc[#id='289e1292134534']"):
extract.write(ET.dump(item))
Getting the output as "NONE" in the extract.xml file. Can you please help me.
From the docs of .dump():
"Write element tree or element structure to sys.stdout. This function should be used for debugging only."
The function .dump() returns None!
I think you want to use .tostring():
import xml.etree.ElementTree as ET
tree = ET.parse('extract_orginal.xml')
root = tree.getroot()
with open('extract.xml', 'w') as extract:
for item in root.findall(f"doc[#id='289e1292134534']"):
extract.write(ET.tostring(item, encoding="utf-8"))

Append XML responses in Python

I am trying to parse multiple XML responses in one file. However, when I write a responses to file, it shows only last one. I assume I need to add append somewhere in order to keep all responses.
Here is my code:
import json
import xml.etree.ElementTree as ET
#loop test
feins = ['800228936', '451957238']
for i in feins:
rr = requests.get('https://pdb-services.nipr.com/pdb-xml-reports/hitlist_xml.cgi?report_type=0&id_fein={}'.format(i),auth=('test', 'test'))
root = ET.fromstring(rr.text)
tree = ET.ElementTree(root)
tree.write("file.xml")
Try changing
for i in feins:
...
tree = ET.ElementTree(root)
tree.write("file.xml")
to (note the indentation):
for i in feins:
...
tree = ET.ElementTree(root)
with open("file.xml", "wb") as f:
tree.write(f)
and see if it works.

no modifications in XML file after running python code

I wrote a code that must modify some values in a xml file. it looks to be working, but when i open this xml file threw PyCharm where i have added the modified file, it just doesn't change a thing. If anyone gave a respond to such a question, please point me where is it. Here is the code as well as the xml.
import xml.etree.ElementTree as ET
tree = ET.parse("farms.xml")
root = tree.getroot()
for elem in root.findall('farm'):
elem.set('money', '2000')
money = elem.get('money')
print(money)
xml
<farms>
<farm farmId="1" name="Моя ферма" color="1" loan="0.000000" money="213" loanAnnualInterestRate="304.166656">
<players>
</players>
</farm>
</farms>
What you are missing is writing the tree back to disk.
import xml.etree.ElementTree as ET
tree = ET.parse("farms.xml")
root = tree.getroot()
for elem in root.findall('farm'):
elem.set('money', '2000')
with open('new_farms.xml', 'wb') as f:
tree.write(f)
It works for me.
Additionally,
print(xml.etree.ElementTree.tostring(root))
will show what you expect.

Split one large .xml file in more .xml files (python)

I've been trying to split one large .xml file in more .xml files in python for a few days now. The thing is I haven't really succeeded yet. So here I am asking for your help.
My large .xml file looks like this:
<Root>
<Testcase>
<Info1>[]<Info1>
<Info2>[]<Info2>
</Testcase>
<Testcase>
<Info1>[]<Info1>
<Info2>[]<Info2>
<Testcase>
...
...
...
<Testcase>
<Info1>[]<Info1>
<Info2>[]<Info2>
<Testcase>
</Root>
It has over 2000 children and what I would like to do is to parse this .xml file and split in smaller .xml files with 100 children each. That would result in 20 new .xml files.
How can I do that?
Thank you!
L.E.:
I've tried to parse the .xml file using xml.etree.ElementTree
import xml.etree.ElementTree as ET
file = open('Testcase.xml', 'r')
tree = ET.parse(file)
total_testcases = 0
for Testcase in root.findall('Testcase'):
total_testcases+=1
nr_of_files = (total_testcases/100)+1
for i in range(nr_of_files+1):
tree.write('Testcase%d.xml' % (i), encoding="UTF-8")
The thing is I don't know how to specifically get only the Testcases and copy them to another file...
Actually, root.findall('Testcase') will return a list of "Testcase" sub elements.
So what need to do is:
create root
add sub elements to root.
Here is example:
>>> tcs = root.findall('Testcase')
>>> tcs
[<Element 'Testcase' at 0x23e14e0>, <Element 'Testcase' at 0x23e1828>]
>>> len(tcs)
2
>>> r = ET.Element('Root')
>>> r.append(tcs[0])
>>> ET.tostring(r, 'utf-8')
'<Root><Testcase>\n <Info1>[]</Info1>\n <Info2>[]</Info2>\n </Testcase>\n </Root>'

Categories