简体   繁体   中英

How to merge only selected lines of two different xml files into single xml file using python?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM