OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to merge 2 xml's in python

  • Thread starter Thread starter Pooja
  • Start date Start date
P

Pooja

Guest
XML 1:

Code:
<invoiceCopy>
<document>
    <invoicenumber>1245678</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>12345678.pdf</pdffilename>
    <sendingsystemid>ABC</sendingsystemid>
</document>
</invoicecopy>

XML 2:

Code:
<invoiceCopy>
<document>
    <invoicenumber>2222222</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>2222222.pdf</pdffilename>
    <sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>

Required output:

Code:
 <invoiceCopy>
<document>
    <invoicenumber>1245678</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>12345678.pdf</pdffilename>
    <sendingsystemid>ABC</sendingsystemid>
</document>
</invoicecopy>
<invoiceCopy>
<document>
    <invoicenumber>2222222</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>2222222.pdf</pdffilename>
    <sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>

This is the output i am getting from the following program:

Code:
 <invoiceCopy>
<document>
    <invoicenumber>1245678</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>12345678.pdf</pdffilename>
    <sendingsystemid>ABC</sendingsystemid>
</document>
<invoiceCopy>
<document>
    <invoicenumber>2222222</invoicenumber>
    <invoicedate>2024-06-06</invoicedate>
    <pdffilename>2222222.pdf</pdffilename>
    <sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>
</invoicecopy>

My program :

Code:
xml_files = glob.glob(XML_DIR +"/*.xml")
print ("xml files are " , xml_files)
xml_element_tree = None
for filename in xml_files:
    data = ElementTree.parse(filename).getroot()
    print (data)
    for result in data.iter('invoiceCopy'):
     if xml_element_tree is None:
       newfilename = "print" + datetime.today().strftime('%Y%m%d_%H%M%S')+ ".xml"
       print(newfilename)
       xml_element_tree=data
     else:
       for statement in data.iter('invoiceCopy'):
        xml_element_tree.append(statement)

    if xml_element_tree is not None:
    result1 = ''
    m_encoding = "iso-8859-1"
    dom = xml.dom.minidom.parseString(ElementTree.tostring(xml_element_tree))
    xml_string = dom.toprettyxml()
    for result in xml_string.split('\n'):
       if not result.strip() == '':
         result1=result1+result+"\n"
    part1, part2 = result1.split('?>')        
    with open("/data/ebpp/star/fh/invoicecopy/print/" + newfilename, "w") as xfile:
       xfile.write(part1 + 'encoding=\"{}\"?>'.format(m_encoding) + part2)
       xfile.close()

Why is the 2nd xml appended inside the root of first xml? Please help
<p>XML 1:</p>
<pre><code><invoiceCopy>
<document>
<invoicenumber>1245678</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>12345678.pdf</pdffilename>
<sendingsystemid>ABC</sendingsystemid>
</document>
</invoicecopy>
</code></pre>
<p>XML 2:</p>
<pre><code><invoiceCopy>
<document>
<invoicenumber>2222222</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>2222222.pdf</pdffilename>
<sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>
</code></pre>
<p>Required output:</p>
<pre><code> <invoiceCopy>
<document>
<invoicenumber>1245678</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>12345678.pdf</pdffilename>
<sendingsystemid>ABC</sendingsystemid>
</document>
</invoicecopy>
<invoiceCopy>
<document>
<invoicenumber>2222222</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>2222222.pdf</pdffilename>
<sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>
</code></pre>
<p>This is the output i am getting from the following program:</p>
<pre><code> <invoiceCopy>
<document>
<invoicenumber>1245678</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>12345678.pdf</pdffilename>
<sendingsystemid>ABC</sendingsystemid>
</document>
<invoiceCopy>
<document>
<invoicenumber>2222222</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>2222222.pdf</pdffilename>
<sendingsystemid>XYZ</sendingsystemid>
</document>
</invoicecopy>
</invoicecopy>
</code></pre>
<p>My program :</p>
<pre><code>xml_files = glob.glob(XML_DIR +"/*.xml")
print ("xml files are " , xml_files)
xml_element_tree = None
for filename in xml_files:
data = ElementTree.parse(filename).getroot()
print (data)
for result in data.iter('invoiceCopy'):
if xml_element_tree is None:
newfilename = "print" + datetime.today().strftime('%Y%m%d_%H%M%S')+ ".xml"
print(newfilename)
xml_element_tree=data
else:
for statement in data.iter('invoiceCopy'):
xml_element_tree.append(statement)

if xml_element_tree is not None:
result1 = ''
m_encoding = "iso-8859-1"
dom = xml.dom.minidom.parseString(ElementTree.tostring(xml_element_tree))
xml_string = dom.toprettyxml()
for result in xml_string.split('\n'):
if not result.strip() == '':
result1=result1+result+"\n"
part1, part2 = result1.split('?>')
with open("/data/ebpp/star/fh/invoicecopy/print/" + newfilename, "w") as xfile:
xfile.write(part1 + 'encoding=\"{}\"?>'.format(m_encoding) + part2)
xfile.close()
</code></pre>
<p>Why is the 2nd xml appended inside the root of first xml? Please help</p>
 

Latest posts

A
Replies
0
Views
1
Anthonin Chaboteaux
A
Top