0
    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;
        };

I want to delete the last node "paragraph" I have, but it gives me the error: 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) To mention that System.out.println(subChapterNode.getNodeName()); prints well!! "Subchapter" So the problem is at the last two lines The XML file is:

<?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>
Lulu
  • 175
  • 1
  • 2
  • 12

1 Answers1

1

You should move the last 3 lines of code into the if clause. And you might also want to remove the child directly from its parent, and not from the initial root tag XMLbook.

I would also add the following suggestions:

  • Name your code to something more specific like removeLastParagraph.
  • Create a local Boolean variable and set it to false under each else clause.

In the final clause, return a false to be compliant with the return value of your function.

    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;
    };
Thirisangu Ramanathan
  • 614
  • 1
  • 11
  • 20
Jorge Garita
  • 173
  • 8
  • Did you try it with subChapterNodes.removeChild(toRemoveString); instead of XMLBook as I said in the suggestions? – Jorge Garita Nov 07 '14 at 08:12
  • I had to cast it and it gives me error...saying that I cannot cast it ((Node)subChapterNodes).removeChild(toRemoveString); – Lulu Nov 07 '14 at 08:17
  • I just made a slight change to it that I skipped before posting the answer. Sorry about that – Jorge Garita Nov 07 '14 at 08:21
  • I'm trying to reproduce the error. Meanwhile, there is a similar solution already posted which I think reflects the same problem you are having. http://stackoverflow.com/questions/3719384/why-can-i-not-remove-a-child-element-ive-just-found-not-found-err – Jorge Garita Nov 07 '14 at 08:48