gpt4 book ai didi

java - 尝试删除 xml 节点 - 错误

转载 作者:行者123 更新时间:2023-12-01 12:19:35 24 4
gpt4 key购买 nike

    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: 尝试引用上下文中不存在的节点。 在 com.sun.org.apache.xerces.internal.dom.ParentNode.internalRemoveChild(来源未知) 在 com.sun.org.apache.xerces.internal.dom.ParentNode.removeChild(来源未知) 在 com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.removeChild(来源未知) 在 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>

最佳答案

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

我还要添加以下建议:

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

在最后一个子句中,返回 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;
};

关于java - 尝试删除 xml 节点 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com