-2

I have a an xml files as "A.xml":

<?xml version="1.0" encoding="utf-8"?>  
    <package xmlns="http://example"> 
      <metadata> 
        <id>example</id> 
    </metadata> 
    <files> 
            <file src="lib/Debug/exampled.lib" target="lib/Debug/exampled.lib" /> 
    </files> 
</package> 

Another xml file "B.xml" as:

<?xml version="1.0" encoding="utf-8"?>  
    <package xmlns="http://example"> 
      <metadata> 
        <id>example</id> 
    </metadata> 
    <files> 
            <file src="lib/Release/example.lib" target="lib/Release/example.lib" /> 
    </files> 
</package> 

I only want to merge these two files in following way:

<?xml version="1.0" encoding="utf-8"?>  
    <package xmlns="http://example"> 
      <metadata> 
        <id>example</id> 
    </metadata> 
    <files> 
            <file src="lib/Debug/exampled.lib" target="lib/Debug/exampled.lib" /> 
            <file src="lib/Release/example.lib" target="lib/Release/example.lib" /> 
    </files> 
</package>

So, kindly suggest how to merge only the files tag of the two files using python scripting.

anamika email
  • 327
  • 9
  • 21

1 Answers1

0

This is a stackoverflow answer that mention many ways you can parse xml: How do I parse XML in Python?

I suggest going with ElementTree as it's very simple to use and can solve your case with minimal code.

Seeing how the community is hostile toward questions that had little research backing it, I'm not going to post the full answer here and suggest that you take time and read up on ElementTree.

Community
  • 1
  • 1
Khoi
  • 4,502
  • 6
  • 29
  • 31
  • your suggestion seems more related to xml parsing which i am not looking for. My question is about xml merging. – anamika email Oct 27 '15 at 09:44
  • To merge your 2 xml files, what I would do is to parse both files and then edit one of them to also include what is missing from the other one and then just save that into another file. The merging require the parsing so to speak. – Khoi Oct 28 '15 at 02:03
  • To clarify - what I mean by parsing in this case is simply to use the suggested tool to translate the original xml content into a format that is easy to traverse, read and edit by a program/script. Thus parsing is indeed fundamental to solving your merging need. – Khoi Oct 28 '15 at 02:08