d.griner d.griner - 1 month ago 7
Python Question

Python - Delete entire node using lxml

I have a an xml document like the following:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>company</groupId>
<artifactId>art-id</artifactId>
<version>RELEASE</version>
</parent>

<properties>
<tomcat.username>admin</tomcat.username>
<tomcat.password>admin</tomcat.password>
</properties>

<dependencies>
<dependency>
<groupId>asdf</groupId>
<artifactId>asdf</artifactId>
<version>[3.8,)</version>
</dependency>
<dependency>
<groupId>asdf</groupId>
<artifactId>asdf</artifactId>
<version>[4.1,)</version>
</dependency>
</dependencies>
</project>


how can I delete the entire node "dependencies"?

I have looked at other questions and answers on stackoverflow and what is different about is the namespace aspect of this xml, and the other questions ask to delete a subelement like "dependency" while I want to delete the whole node "dependencies." Is there an easy way using lxml to delete the entire node?

The following gives a TypeError: remove() takes exactly 2 arguments (1 given) error:

import xml.etree.ElementTree as ET
tree = ET.parse('pom.xml')
root = tree.getroot()
namespace = '{http://maven.apache.org/POM/4.0.0}'
root.find(namespace+'dependencies').remove()

Answer

You can create a dict mapping for your namespace(s), find the node then call root.remove passing the node, you don't call .remove on the node:

x = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>company</groupId>
    <artifactId>art-id</artifactId>
    <version>RELEASE</version>
</parent>    
<properties>
    <tomcat.username>admin</tomcat.username>
    <tomcat.password>admin</tomcat.password>
</properties>    
<dependencies>
    <dependency>
        <groupId>asdf</groupId>
        <artifactId>asdf</artifactId>
        <version>[3.8,)</version>
    </dependency>
    <dependency>
        <groupId>asdf</groupId>
        <artifactId>asdf</artifactId>
        <version>[4.1,)</version>
    </dependency>
</dependencies>
</project>"""
import lxml.etree as et
from StringIO import StringIO

tree = et.parse(StringIO(x))
root =tree.getroot()

nsmap = {"mav":"http://maven.apache.org/POM/4.0.0"}

root.remove(root.find("mav:dependencies", namespaces=nsmap))

print(et.tostring(tree))

Which would give you:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>company</groupId>
    <artifactId>art-id</artifactId>
    <version>RELEASE</version>
</parent>    
<properties>
    <tomcat.username>admin</tomcat.username>
    <tomcat.password>admin</tomcat.password>
</properties>   
</project>
Comments