java - 删除 XML 节点

标签 java xml dom

        Boolean remove(String path, String S) throws ParserConfigurationException, SAXException, IOException{
        File fXmlFile = new File(path);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document XMLbook = dBuilder.parse(fXmlFile);
        Node root = XMLbook.getFirstChild(); //(1)
        Node book = XMLbook.getElementsByTagName("BOOK").item(0);//(2)
        NodeList chapterNodes = ((Element)book).getElementsByTagName("Chapter");
        Node subChapterNode=null;
        if(chapterNodes != null && chapterNodes.getLength() > 0) {
            Node chapterNode = chapterNodes.item(chapterNodes.getLength() - 1);
            NodeList subChapterNodes = ((Element)chapterNode).getElementsByTagName("Subchapter");
            if(subChapterNodes != null && subChapterNodes.getLength() > 0) {
                subChapterNode = subChapterNodes.item(subChapterNodes.getLength() - 1);
//            System.out.println(subChapterNode.getNodeName());
                Node toRemoveString=subChapterNode.getLastChild();
                XMLbook.removeChild(toRemoveString);    
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    };

我想删除我的最后一个节点paragraph,但它给了我错误:

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 com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.removeChild(Unknown Source) at Books.SubChapter.remove(BookElement.java:92) at Books.Book.main(Book.java:22) 

值得一提的是 System.out.println(subChapterNode.getNodeName()); 打印得很好! “Subchapter” 所以问题出在最后两行 XML 文件是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><ROOT name="r">
    <TITLE>Portocalia ca zapada</TITLE>
    <AUTOR> I.C. Popovici </AUTOR>
    <AUTOR> Marin Eminescu </AUTOR>
    <BOOK name="b">
        <Chapter name="1">
            <Subchapter name="1.1">
                <paragraph>Primul paragraf!</paragraph>
                <paragraph>Al doilea paragraf.</paragraph>
                <paragraph>Al treilea paragraf</paragraph>
            </Subchapter>
            <Subchapter name="1.2"> 
                <paragraph>Primul paragraf, fata!</paragraph>
            </Subchapter>   
        </Chapter>
        <Chapter name="2">
            <Subchapter name="2.1">
                <paragraph>A fost o data ca niciodata</paragraph>
                <paragraph>o fata cu parul brunet si matasos</paragraph>
                <paragraph>Pe care o chema Alba ca Zapada.</paragraph>
            </Subchapter>
            <Subchapter name="2.2">
                <paragraph>In luna a13a ea s-a maritat.</paragraph>
            </Subchapter>
            <Subchapter name="2.3">
                <paragraph>In continuare, bineinteles</paragraph> 
                <paragraph>Am reusit!!</paragraph>
            </Subchapter>
        </Chapter>
    </BOOK>
</ROOT>

最佳答案

要删除节点,您需要使用父节点而不是文档

替换:

XMLbook.removeChild(toRemoveString);

作者:

subChapterNode.removeChild(toRemoveString);

关于java - 删除 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798838/

相关文章:

java - 对列表进行排序的分配

java - GET - 目前您无法在开发模式下引入新注释。请重新启动

xml - 为 logback(.xml) 导入外部属性

java - 使用 Java 中的自定义对象实现将 XML 解析为 DOM 树

java - 无法在 Spring Boot 测试中加载类 [pg-uuid]

java - 有关如何创建 Java 纸牌游戏的提示

ios - 用于 MobileConfig 文件的 OS X 终端中的 XML 解析

java - 关于注解的说明

javascript - 不使用 Jquery 检查所有单选按钮

javascript - 在 ReactJS 中集成 sharethis