Java 尝试删除xml节点-错误
我想删除我拥有的最后一个节点段落,但它给出了错误: 线程main org.w3c.dom.domeException中的异常:未找到\u错误:尝试在不存在节点的上下文中引用该节点。 位于com.sun.org.apache.xerces.internal.dom.ParentNode.internalRemoveChildUnknown Source 位于com.sun.org.apache.xerces.internal.dom.ParentNode.removeChildUnknown Source 位于com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.removeChildUnknown Source 在Books.SubChapter.removeBookElement.java:92 at Books.Book.mainBook.java:22 提到System.out.printlnsubChapterNode.getNodeName;打印得很好!!分章 所以问题出在最后两行 XML文件是:Java 尝试删除xml节点-错误,java,xml,dom,null,Java,Xml,Dom,Null,我想删除我拥有的最后一个节点段落,但它给出了错误: 线程main org.w3c.dom.domeException中的异常:未找到\u错误:尝试在不存在节点的上下文中引用该节点。 位于com.sun.org.apache.xerces.internal.dom.ParentNode.internalRemoveChildUnknown Source 位于com.sun.org.apache.xerces.internal.dom.ParentNode.removeChildUnknown So
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;
};
您应该将最后3行代码移到if子句中。 您可能还希望直接从其父级中删除子级,而不是从初始根标记XMLbook中删除 我还要补充以下建议: 将代码命名为更具体的名称,如RemovelastParague。 创建一个局部布尔变量,并在each else子句下将其设置为false。 在最后一个子句中,返回一个false以符合函数的返回值
<?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>
嗯,看起来可能是Java。但我不应该猜。请编辑您的问题并添加合适的语言标记,此外,您是否尝试使用subChapterNodes.removeChildtoRemoveString;而不是我在建议中所说的XMLBook?我必须强制转换它,它会给我错误…说我不能强制转换它NodesubChapterNodes.removeChildtoRemoveString;我只是做了一点小小的改动,在发布答案之前跳过了。很抱歉,我正在尝试复制错误。同时,已经发布了一个类似的解决方案,我认为它反映了与您相同的问题。
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;
};