How to extract lines between repeated n number of tags from XML and continue to the last tag?
I have an XML file with over 2500 <Item>
elements.
The example below shows a sample layout. I want to copy every line between <Item name="1st">
and <Item name="500th">
into a new file as it is. Then move on to the next 500 from <Item name=501st">
and then write this to a new file. Result - 5 new files. Nothing to miss.
<Item name="1st"><ItemProperties>
<property>data</property><property>data</property>
</ItemProperties>
...
...
<Item name="500th"><ItemProperties>
<property>data</property><property>data</property>
</ItemProperties>
The operation below does it for the first 500, but I don't know how to continue until the last closing tag.
xmllint --xpath "//Item[position()<=500]" FileName.XML > Output1.XML
See this link for an example:
source to share
Using python, the first solution is to process from line 0 to the last line, one line at a time:
nfh = None
with open('foo.xml') as fh:
num = 0
for index, line in enumerate(fh):
if not index % 500:
num += 1
if nfh:
nfh.close()
nfh = open('file_name{}.txt'.format(num), 'w')
nfh.write(line)
if nfh:
nfh.close()
Second, it uses lxml to list only a specific tag in the XML file:
import lxml.etree as etree
xml_data = etree.parse('foo.xml')
nfh = None
num = 0
for index, tag in enumerate(xml_data.xpath('//Item')):
# Enumerate 500 tags
if not index % 500:
num += 1
if nfh:
nfh.close()
nfh = open('Output{}.XML'.format(num), 'wb')
nfh.write(etree.tostring(tag))
if nfh:
nfh.close()
This if your XML is closer to this:
<root>
<Item name="1st"><ItemProperties>
<property>data</property><property>data</property>
</ItemProperties>
</Item>
<Item name="2nd"><ItemProperties>
<property>data</property><property>data</property>
</ItemProperties>
</Item>
....
<Item name="500th"><ItemProperties>
<property>data</property><property>data</property>
</ItemProperties>
</Item>
....
</root>
source to share
import xml.etree.ElementTree as ET
xml_doc = ET.parse('table.xml')
results = xml_doc.getroot()
def chunkify(lst,n):
# Split the list into 'n' equal parts
return [ lst[i::n] for i in xrange(n) ]
count = 1
for f in chunkify(results,5):
temp_str = ''
for element in f:
temp_str = temp_str + ET.tostring(element)
with open(str(count) +"_Output.xml", "w") as text_file:
text_file.write(temp_str)
count = count +1
source to share