如何解决尝试使用 Jsoup java 根据属性值删除 XML 标签
我试图根据属性值 kind='lib 删除 xml 元素。当我打印出文件时,内容被删除,但是当我写入文件并打开文件时,我看到条件中未给出的额外元素被删除。我可以看到 kind ='con' 被删除了,但我需要保留这个
我的代码
Path classPath=Paths.get(prop.getProperty("testClasspath"));
String pomFile = new String(Files.readAllBytes(classPath),StandardCharsets.UTF_8);
Document doc_classpath = Jsoup.parse(pomFile,"",Parser.xmlParser());
//for pretty xml formating
doc_classpath.outputSettings().prettyPrint(false);
doc_classpath.select("[kind=lib]").remove();
System.out.println(doc_classpath.toString());
Files.write(Paths.get(prop.getProperty("testClasspath")),doc_classpath.toString().getBytes());
我的 xml 当前
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/test/lib_local/activation-1.1.jar"/>
<classpathentry kind="lib" path="C:/test/lib_local/ahClient-1.0_A41.jar"/>
<classpathentry kind="lib" path="C:/test/lib_local/ahClient-1.0_A51.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="target/classes"/>
输出xml
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/java">
<attributes>
<attribute name="optional" value="true" />
<attribute name="maven.pomderived" value="true" />
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/resources">
<attributes>
<attribute name="maven.pomderived" value="true" />
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true" />
<attribute name="maven.pomderived" value="true" />
<attribute name="test" value="true" />
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true" />
<attribute name="test" value="true" />
</attributes>
</classpathentry>
当我执行程序时,我看到额外的 cml 标签被删除。有什么帮助吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。