如何使用JAVA删除XML中的节点
我使用的XML文件是这样的,请注意有4个'target'标记
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project basedir="." default="end" name="precompile">
<property name="charset" value="utf-8"/>
<target name="start">
</target>
<target depends="precompile-templates1,precompile-templates2" name="end">
<echo>finish generating precompiled templates</echo>
</target>
<target name="precompile-templates1">
<property name="outputJS1" value="jsp/jsp_2/js/templates.js"/>
<property name="templatesPath1" value="jsp/jsp_2/js/tmpl"/>
<java dir="${basedir}" failonerror="true" fork="true" jar="lib/js.jar">
<arg value="otherFiles/lib/rhino-handlebars-compiler.js"/>
<arg value="--handlebars"/>
<arg value="otherFiles/third-party/handlebars-v1.3.0.js"/>
<arg value="--templates"/>
<arg value="${templatesPath1}"/>
<arg value="--output"/>
<arg value="${outputJS1}"/>
</java>
<echo>Template Precompiled to web/js/compiled-templates.js</echo>
<echo> is now ready to compress....</echo>
</target>
<target name="precompile-templates2">
<property name="outputJS2" value="jsp/jsp_3/js/templates.js"/>
<property name="templatesPath2" value="jsp/jsp_3/js/tmpl"/>
<java dir="${basedir}" failonerror="true" fork="true" jar="lib/js.jar">
<arg value="otherFiles/lib/rhino-handlebars-compiler.js"/>
<arg value="--handlebars"/>
<arg value="otherFiles/third-party/handlebars-v1.3.0.js"/>
<arg value="--templates"/>
<arg value="${templatesPath2}"/>
<arg value="--output"/>
<arg value="${outputJS2}"/>
</java>
<echo>Template Precompiled to web/js/compiled-templates.js</echo>
<echo> is now ready to compress....</echo>
</target></project>
我想保留前两个'target'节点,但删除'target'的其余两个节点。我该怎么办?我试着这样做:
File fXmlFile = new File(subBuildFile);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
Node s = doc.getElementsByTagName("target").item(1);
for(int i =2; i<doc.getElementsByTagName("target").getLength();i++){
doc.getElementsByTagName("target").item(i).removeChild(doc.getElementsByTagName("target").item(i));
}
但总有这样一个类似的错误:
Exception in thread "main" org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at com.sun.org.apache.xerces.internal.dom.ParentNode.internalRemoveChild(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ParentNode.removeChild(Unknown Source)
at xmlReader.Test.resetXML(Test.java:52)
at xmlReader.Test.main(Test.java:39)
有什么建议吗
另外: 当我使用以下代码时,有些奇怪:
System.out.println("the number of target tag is "+doc.getElementsByTagName("target").getLength());
for(int i =2; i<targets.getLength();i++){
root.removeChild(targets.item(2));
System.out.println(i);
}
System.out.println(doc.getElementsByTagName("target").getLength());
我使用的XML如下所示:
<target name="start">
</target>
<target depends="precompile-templates1,precompile-templates2" name="end">
<echo>finish generating precompiled templates</echo>
</target>
<target id="2">
</target>
<target id="3">
</target>
<target id="4">
</target>
<target id="5">
</target>
<target id="6">
</target>
<target id="7">
</target>
<target id="8">
</target>
<target id="9">
</target>
<target id="10">
</target>
<target id="11">
</target>
<target id="12">
</target>
<target id="13">
</target>
</project>
输出如下:
the number of target tag is 8
2
3
4
5
最终输出的XML如下所示:
<target name="start">
</target>
<target depends="precompile-templates1,precompile-templates2" name="end">
<echo>finish generating precompiled templates</echo>
</target>
<target id="11">
</target>
<target id="12">
</target>
<target id="13">
</target>
</project>
# 1 楼答案
表情
选择一个
<target>
,但不能从中删除<target>
2上没有硬连线的更通用的代码是:
请注意(与代码相反),要删除的目标元素的数量是预先计算的。通过改变列表的长度来限制它会产生一种奇怪的效果