简体   繁体   English

如何删除父节点将其所有子节点?

[英]How to delete a parent node will all its child nodes?

I have the following .xml file 我有以下.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
   <book id="1">
      <name>Book-1</name>
      <author>Author-1</author>
   </book>
</bookstore>

The question is I want to delete the book with id="1" . 问题是我想删除id="1"的书。 I want the way to delete a book node so that all its child nodes are removed automatically. 我想要删除书节点的方法,以便其所有子节点都被自动删除。 Is there a way to do so? 有办法吗?

You can use a XSLT script: 您可以使用XSLT脚本:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="book[@id='1']"/>
</xsl:stylesheet>

Basically what one does is copying the entire file without entities that match the pattern "book[@id='1']" 基本上,要做的是复制整个文件,而没有匹配模式"book[@id='1']"实体

XSLT will of course introduce some overhead but the advantages are: XSLT当然会带来一些开销,但是优点是:

  • You can easily modify the transformation when the query becomes more complex 当查询变得更复杂时,您可以轻松修改转换
  • The execution mechanism is more bug-free than code you will write yourself (no offense, but XSLT programs are used by thousands of people and thus easy bugs will be debugged) 执行机制比您自己编写的代码更不会出错(没有冒犯,但是XSLT程序被成千上万的人使用,因此容易的错误将被调试)
  • You can test transformations with numerous programs, by hardcoding the transformation you will have to write a testbench on your own. 您可以使用众多程序测试转换,通过对转换进行硬编码,您将必须自己编写一个测试平台。
  • In some cases XSLT will even run faster simply because programmers try to optimize execution. 在某些情况下,仅由于程序员尝试优化执行,XSLT的运行速度甚至会更快。

Using the DOM parser (without specifying the entire file, I hope you are familiar with DOM): 使用DOM解析器(不指定整个文件,希望您熟悉DOM):

NodeList nList = doc.getElementsByTagName("book");
for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        if(eElement.getAttribute("id").equals("1")) {
            doc.removeChild(eElement);
        }
    }
}

There is another simple solution available to parse the XML file altogether. 还有另一个简单的解决方案可用于完全解析XML文件。 Browse through the documentation of Xstream 浏览Xstream的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果有许多同名的父节点,如何从xml的父节点中选择所有子节点? - How to select all child nodes from a parent node from xml if there are many parents node with same name? 如何使用Java从XML读取父节点的子节点 - How to read child nodes of a parent node from XML using java 如何从子节点的值中获取父节点的名称? - How to get the name of the parent node from the values of child nodes? 删除节点的所有子节点 - Remove all child nodes of a node 如何在zk中的动态树中选择父节点时选择所有子节点? - How to select all child nodes on selection of parent in a dynamic tree in zk? 如何从树中的任何父节点获取所有叶节点 - How to get all leaf nodes from any parent node in a tree 为什么打印节点的父节点会返回父+子节点? - Why does printing parent node of a node returns parent + child nodes? 休眠:如何确保在父级的所有子实体都删除后不删除父级? - Hibernate: How to ensure that a parent is not deleted when all its child entities are? 如何在Firebase数据库Android上获取具有相似子节点的所有父节点? - How to fetch all parent nodes with similar child nodes on Firebase database Android? 如何在使用Hibernate删除父项时删除所有子行? - How to delete all child rows when parent is deleted using Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM