Home > Net >  Add libraries elements to an existing xml file
Add libraries elements to an existing xml file

Time:07-08

Xml file is:

    <?xml version="1.0"?>
<sdk:sdk-addon
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdk="http://schemas.android.com/sdk/android/addon/7">
    <sdk:add-on>
        <sdk:vendor-id>vendor1</sdk:vendor-id>
        <sdk:vendor-display>vendor1</sdk:vendor-display>
        <sdk:name-id>vendor1_apis</sdk:name-id>
        <sdk:name-display>vendor1 Z2 SDK Add-On</sdk:name-display>
        <sdk:description>SDK Add-On for the Z2 Platform</sdk:description>
        <sdk:api-level>30</sdk:api-level>
        <sdk:revision>1</sdk:revision>
        <sdk:libs>
        </sdk:libs>
        <sdk:archives>
            <sdk:archive>
                <sdk:size>84287</sdk:size>
                <sdk:checksum type='sha1'>7825e75aa697841fe0fe5b9438ca44792c1786d0</sdk:checksum>
                <sdk:url>z2_sdk_addon-eng.jenkins-linux-x86.zip</sdk:url>
        <sdk:host-os>linux</sdk:host-os>
            </sdk:archive>
        </sdk:archives>
    </sdk:add-on>
</sdk:sdk-addon>

Libraries file is:

library1
library2

Output should be:

    <?xml version="1.0"?>
<sdk:sdk-addon
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdk="http://schemas.android.com/sdk/android/addon/7">
    <sdk:add-on>
        <sdk:vendor-id>vendor1</sdk:vendor-id>
        <sdk:vendor-display>vendor1</sdk:vendor-display>
        <sdk:name-id>vendor1_apis</sdk:name-id>
        <sdk:name-display>vendor1 Z2 SDK Add-On</sdk:name-display>
        <sdk:description>SDK Add-On for the Z2 Platform</sdk:description>
        <sdk:api-level>30</sdk:api-level>
        <sdk:revision>1</sdk:revision>
        <sdk:libs>
          <sdk:lib>
           <sdk:name>libray1</sdk:name>
          </sdk:lib>
              <sdk:lib>
           <sdk:name>libray2</sdk:name>
          </sdk:lib>
        </sdk:libs>
        <sdk:archives>
            <sdk:archive>
                <sdk:size>84287</sdk:size>
                <sdk:checksum type='sha1'>7825e75aa697841fe0fe5b9438ca44792c1786d0</sdk:checksum>
                <sdk:url>z2_sdk_addon-eng.jenkins-linux-x86.zip</sdk:url>
        <sdk:host-os>linux</sdk:host-os>
            </sdk:archive>
        </sdk:archives>
    </sdk:add-on>
</sdk:sdk-addon>

I created a shell script that creates a text

 <sdk:lib>
   <sdk:name>libray1</sdk:name>
  </sdk:lib>
      <sdk:lib>
   <sdk:name>libray2</sdk:name>
  </sdk:lib>

And tried directly adding the result text using python code

from lxml import etree
root = etree.parse('my.xml').getroot()
#print(root)
#print(etree.tostring(root))
libraries = root.find(".//{http://schemas.android.com/sdk/android/addon/7}libs")

print(libraries)



with open('libraries_to_be_added_xml') as libraries_to_be_added_file:
    text=libraries_to_be_added_file.read()
    libraries.text=str(text)

tree = etree.ElementTree(root)
tree.write("my.xml", encoding="UTF-8", xml_declaration=True, standalone=True)

The output I am getting has &gt;sdk:lib&lt; instead of <sdk:lib>

Output I am getting

    <?xml version="1.0"?>
<sdk:sdk-addon
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdk="http://schemas.android.com/sdk/android/addon/7">
    <sdk:add-on>
        <sdk:vendor-id>vendor1</sdk:vendor-id>
        <sdk:vendor-display>vendor1</sdk:vendor-display>
        <sdk:name-id>vendor1_apis</sdk:name-id>
        <sdk:name-display>vendor1 Z2 SDK Add-On</sdk:name-display>
        <sdk:description>SDK Add-On for the Z2 Platform</sdk:description>
        <sdk:api-level>30</sdk:api-level>
        <sdk:revision>1</sdk:revision>
        <sdk:libs>
          &gt;sdk:lib&lt;
           &gt;sdk:name&lt;libray1&gt;/sdk:name&lt;
          &gt;/sdk:lib&lt;
              &gt;sdk:lib&lt;
           &gt;sdk:name&lt;libray2&gt;/sdk:name&lt;
          &gt;/sdk:lib&lt;
        </sdk:libs>
        <sdk:archives>
            <sdk:archive>
                <sdk:size>84287</sdk:size>
                <sdk:checksum type='sha1'>7825e75aa697841fe0fe5b9438ca44792c1786d0</sdk:checksum>
                <sdk:url>z2_sdk_addon-eng.jenkins-linux-x86.zip</sdk:url>
        <sdk:host-os>linux</sdk:host-os>
            </sdk:archive>
        </sdk:archives>
    </sdk:add-on>
</sdk:sdk-addon>

CodePudding user response:

You are adding text to the xml node <sdk:libs>. Thats why the text gets escaped, so it does not break the xml document.

You need to work with xml elements, in the case of ElementTree:

# Create a lib element with a subelement
sdk_lib = ET.Element('{http://schemas.android.com/sdk/android/addon/7}lib')
sdk_name = ET.SubElement(sdk_lib, '{http://schemas.android.com/sdk/android/addon/7}name')
sdk_name.text = 'Library 1'

# Adds the sdk_lib element to your <sdk:libs> element
libraries.append(sdk_lib)

Relevant documentation: https://docs.python.org/3/library/xml.etree.elementtree.html

  • Related