我有以下xml:
<table1>
<row0>
<column1>String</column1>
<column2>int</column2>
</row0>
<row1>
<column1>aa</column1>
<column2>65432</column2>
</row1>
<row2>
<column1>ww</column1>
<column2>1111</column2>
</row2>
<row3>
<column1>fff</column1>
<column2>333</column2>
</row3>
<row4>
<column1>jj</column1>
<column2>2</column2>
</row4>
</table1>
Run Code Online (Sandbox Code Playgroud)
我想删除节点row3及其元素.我在java.how中写这个XML文件来做那个?我在另一篇文章中看到了这段代码但却无法理解
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]");
Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
b13Node.getParentNode().removeChild(b13Node);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(document), new StreamResult(System.out));
}
Run Code Online (Sandbox Code Playgroud) 我有以下XML文件:
<Tables>
<table>
<row></row>
</table>
<Tables>
Run Code Online (Sandbox Code Playgroud)
我想将其编辑为:
<Tables>
<table>
<row>some value</row>
</table>
<Tables>
Run Code Online (Sandbox Code Playgroud)
我使用文件编写器编写XML文件。我该如何编辑?
发现我创建的临时文件包含编辑内容,然后删除原始文件并重命名该临时文件。还有其他办法吗?
那是我编写文件的代码:
public boolean createTable(String path, String name, String[] properties) throws IOException {
FileWriter writer = new FileWriter(path);
writer.write("<Tables>");
writer.write("\t<" + name + ">");
for(int i=0; i<properties.length; i++){
writer.write("\t\t<" + properties[0] + "></" + properties[0] + ">");
}
writer.write("\t</" + name + ">");
writer.write("</Tables>");
writer.close();
return false;
}
Run Code Online (Sandbox Code Playgroud)