user8521874 user8521874 - 3 years ago 150
Python Question

Reading xml and trying to extract it into 2 different xml's

I have a xml file which looks like below:

<?xml version="1.0" encoding="ASCII" standalone="yes"?>
<file>
<records>
<record>
<device_serial_number>PAD203137687</device_serial_number>
<device_serial_number_2>203137687</device_serial_number_2>
</record>
<record>
<device_serial_number>PAD203146024</device_serial_number>
<device_serial_number_2>203146024</device_serial_number_2>
</record>
</records>
</file>


Now i want to check device_serial_number in each record and check if the last 4 characters are 6024, if yes then write the complete record data to newxml file named one.xml

I have tried the below

from xml.etree import ElementTree as ET
tree = ET.parse('C:\\Users\\x3.xml')
for node in tree.findall('.//records//record/'):
print("<"+str(node.tag) + "> "+"<"+str(node.text)+"/>")

Answer Source

So from what I understand, you can try something like below:

     from xml.etree import ElementTree as ET
     from xml.dom.minidom import getDOMImplementation
     from xml.dom.minidom import parseString
     tree = ET.parse('C:\\Users\\x3.xml')
     root = tree.getroot()
     impl = getDOMImplementation()
     #print(root) #just to check
     commands = root.findall(".//records//")
     recs=[c for c in commands if c.find('device_serial_number')!=None and 
     c.find('soc_id').text[-4:]=='6024']
     bb=""
     for rec in recs:
         aa=(parseString(ET.tostring(rec)).toprettyxml(''))
         bb=bb+aa
     #print(bb) #it will have all data you need, write these into files
     newdoc = impl.createDocument(None, bb, None)
     newdoc.writexml(open('your_output_file.xml', 'w'),
     indent="",
     addindent="",
     newl='') #check documentation for these

Here is the linkfor documentation regarding writing to xml files.

Node.writexml(writer, indent=”“, addindent=”“, newl=”“) Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. The indent parameter is the indentation of the current node. The addindent parameter is the incremental indentation to use for subnodes of the current one. The newl parameter specifies the string to use to terminate newlines.

The above is from xml.dom.minidom documentation.Which explains how to write and what they mean.

Finally this will help you to write the required data to the file which you specify in writexml, in xml format.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download