-2

I have to add a new line like ./dir/pqr in the <filename sections to the following existing xml file. The 'url' is a link

<dbchangelog
    xmlns="http://url/xml/ns/dbchangelog"
    xmlns:xsi="http://url/2001/XMLSchema-instance"
    xsi:schemaLocation="http://url/xml/ns/dbchangelog http://url/xml/ns/dbchangelog-4.1.xsd">
    <changeSet author="name" id="v3.0.0">
        <tagDatabase tag="release/v3.0.0" />
    </changeSet>
    <dependencyManagement>
        <dependencies>
            <path>
                <filename>./dir/commons</filename>
            </path>
            <path>
                <filename>./dir/abc</filename>
            </path>
            <path>
                <filename>./dir/xyz</filename>
            </path>
        </dependencies>
    </dependencyManagement>
</dbchangelog>

Tried following piece of code, it prints the appended value but doesn't actually append to the file.

import xml.etree.ElementTree as ET 
tree = ET.parse('pqr.xml')
root = tree.getroot() 
b = ET.SubElement(root, 'filename')
b.text = './dml/release/v3.0.0/abc_changelog.xml' 
print ET.tostring(root) 
3
  • Does this answer your question? python elementtree xml append Commented Feb 19, 2024 at 9:46
  • No, it doesn't. probably because the xml structure is different. I have found multiple answers but none woks for the said xml file Commented Feb 19, 2024 at 11:23
  • Please share a valid xml example. Commented Feb 19, 2024 at 11:39

2 Answers 2

0

You have to register the Namespace and insert than the attribute instead text:

import xml.etree.ElementTree as ET

tree = ET.parse('pqr.xml')
root = tree.getroot()

# Register the Namespace
ns = {'': 'http://url/xml/ns/dblogs', 'xsi': 'http://url/2001/XMLSchema-instance', 'pro': 'http://url/xml/ns/dblogs/ns/pro'}
for key, value in ns.items():
    ET.register_namespace(key, value)

# Set attribute value instead of text
b = ET.SubElement(root, 'include')
b.attrib['file'] = './dml/release/v3.0.0/abc_changelog.xml'

ET.indent(root, space='  ')
ET.dump(root)

# Write the result to a new file:
tree = ET.ElementTree(root)
ET.indent(tree, space='  ')
tree.write("out_new.xml", xml_declaration=True, encoding='UTF-8')

Output:

<dblogs xmlns="http://url/xml/ns/dblogs" xmlns:xsi="http://url/2001/XMLSchema-instance" xsi:schemaLocation="http://url/xml/ns/dblogs">
  <changeSet author="user_id" id="v3.0.0">
    <tagDatabase tag="release/v3.0.0" />
  </changeSet>
  <include file="./file_one.xml" />
  <include file="./file_two.xml" />
  <include file="./dml/release/v3.0.0/abc_changelog.xml" />
</dblogs>
Sign up to request clarification or add additional context in comments.

2 Comments

But when i open the pqr.xml, I don't see any new line added, though it's displayed in the output
How do I read the xmlns value while parsing the xml file ? The output must be url/xml/ns/dblogs
0

You are missing a closing " in the following line:

xsi:schemaLocation="http://url/xml/ns/dblogs >

When adding this your code works for me with following output:

b'<ns0:dblogs xmlns:ns0="http://url/xml/ns/dblogs" xmlns:ns1="http://url/2001/XMLSchema-instance" ns1:schemaLocation="http://url/xml/ns/dblogs">\n\n <ns0:changeSet author="user_id" id="v3.0.0">\n\n <ns0:tagDatabase tag="release/v3.0.0" />\n\n </ns0:changeSet>\n\n <ns0:include file="./file_one.xml" />\n\n <ns0:include file="./file_two.xml" />\n\n<include>./dml/release/v3.0.0/abc_changelog.xml</include></ns0:dblogs

notice that in your code the tag is added as:

<include>./dml/release/v3.0.0/abc_changelog.xml</include>

To make it appear in the same manner as the others you also have to include the namespace (Emitting namespace specifications with ElementTree in Python) and the file argument (How do I add attributes to SubElement in ElementTree (Python))

1 Comment

You lost the namespace declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.