我使用的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>
我想保留前2个“目标”节点,但删除“目标”的其余2个节点。我该怎么办?我试图做这样的事情:
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>
答案 0 :(得分:2)
表达式
doc.getElementsByTagName("target").item(i)
选择<target>
,您无法从中删除<target>
。
Element root = doc.getDocumentElement(); // should be <project>
NodeList targets = doc.getElementsByTagName("target");
root.removeChild( targets.item( 3 ) );
root.removeChild( targets.item( 2 ) );
在2上没有硬连线的更通用的代码是:
int toKeep = 2;
int toDelete = targets.getLength() - toKeep;
for(int i = 0; i < toDelete; i++){
root.removeChild(targets.item(toKeep));
}
请注意(与您的代码相反),要预先计算要删除的目标元素的数量。使用更改列表长度来限制它会产生奇怪的效果。