尝试删除xml节点 - 错误

时间:2014-11-07 07:18:29

标签: java xml dom null

    Boolean remove(String path) 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 true;
        };

我想删除我的最后一个节点“段落”,但它给了我错误:     线程“main”中的异常org.w3c.dom.DOMException:NOT_FOUND_ERR:尝试在不存在的上下文中引用节点。         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)         在Books.Book.main(Book.java:22)    提一下System.out.println(subChapterNode.getNodeName());打印得很好!! “分章” 所以问题出在最后两行 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>

1 个答案:

答案 0 :(得分:1)

您应该将最后3行代码移到if子句中。 您可能还希望直接从其父级删除子级,而不是从初始根标记XMLbook中删除。

我还会添加以下建议:

  • 将代码命名为更具体的内容,例如removeLastParagraph
  • 创建一个本地Boolean变量,并在每个else子句下将其设置为false。

在final子句中,返回false以符合函数的返回值。

    Boolean remove(String path) 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();
            subChapterNodes.removeChild(toRemoveString);    
            return Boolean.TRUE;
        }
    }
    return Boolean.FALSE;
    };