长话短说,我会自动将软件包添加到pom.xml
文件中,每次使用write()
方法时,它都会完全删除最上面的<?xml>
标签。
如何防止删除<?xml>
标签?
import xml.etree.ElementTree as ElementTree
ElementTree.register_namespace('', 'http://maven.apache.org/POM/4.0.0')
xml_tree = ElementTree.ElementTree()
xml_tree.parse(f'{current_directory}/pom.xml')
dependecies_tag = xml_tree.find('{http://maven.apache.org/POM/4.0.0}dependencies')
new_dependency = ElementTree.Element('{http://maven.apache.org/POM/4.0.0}dependency')
new_group_id = ElementTree.SubElement(new_dependency, '{http://maven.apache.org/POM/4.0.0}groupId')
new_group_id.text = 'groupId'
new_artifact_id = ElementTree.SubElement(new_dependency, '{http://maven.apache.org/POM/4.0.0}artifactId')
new_artifact_id.text = 'artifactId'
dependecies_tag.append(new_dependency)
xml_tree.write(f'{current_directory}/pom.xml')
<?xml version="1.0" encoding="UTF-8"?>
<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">
<dependencies>
</dependencies>
</project>
<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">
<dependencies>
<dependency><groupId>groupId</groupId><artifactId>artifactId</artifactId></dependency><dependency><groupId>groupId</groupId><artifactId>artifactId</artifactId></dependency></dependencies>
</project>