当前位置: 首页 > 面试题库 >

为什么不能删除刚刚找到的子元素?NOT_FOUND_ERR

邢俊悟
2023-03-14
问题内容

我正在构建一个脚本,该脚本必须修补XML文件,包括用一个元素列表替换另一个元素列表。以下函数将补丁(涉及到具有相同名称的元素的可能为空的列表)应用于父元素具有相同名称的元素的列表(也可能为空的列表)。(这只是修补逻辑的一小部分)。

为什么在运行代码时出现以下错误?

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(ParentNode.java:503)
    at com.sun.org.apache.xerces.internal.dom.ParentNode.removeChild(ParentNode.java:484)
    at CombineSweeps$PTReplaceNodeList.apply(CombineSweeps.java:514)

(下面标记了514行。)据我了解,我刚刚验证了该元素存在(因为NodeList是活动的,因此它的第一个条目将始终是下一个匹配项或为null)。有趣的是,这并不总是一个问题。

private static class PTReplaceNodeList extends PTBase {
    private final String name;
    private final String nextElement;
    private final List<Node> childList;

    ...

    int apply(Document document, Node parent, Node node_unused) {
        NodeList nodes;
        // A marker for where to insert our nodes.
        // We make a guess using nextElement (if null, means at end).
        Node refNode = null;
        if (parent instanceof Document) {   // root element
            Document parDoc = (Document) parent;
            nodes = parDoc.getElementsByTagName(name);
            if (nextElement != null) {
                refNode = parDoc.getElementsByTagName(nextElement).item(0);
            }
        } else {
            Element parElt = (Element) parent;
            nodes = parElt.getElementsByTagName(name);
            if (nextElement != null) {
                refNode = parElt.getElementsByTagName(nextElement).item(0);
            }
        }

        while (true) {
            // iterate through the list of nodes
            Node node = nodes.item(0);
            if (node == null) {
                break;
            }

            // Reliable guess: insert before node following last in list
            refNode = node.getNextSibling();

            parent.removeChild(node);  // line 514
        }

        for (Node child : childList) {
            Node imported = document.importNode(child, true);
            parent.insertBefore(imported, refNode);
        }
        return childList.size();
    }
}

编辑:我用以下功能代替getElementsByTagName()(请参阅接受的答案)。

/** Returns all direct children of node with name name.
 *
 * Note: not the same as getElementsByTagName(), which finds all descendants. */
static List<Node> getChildNodes( Node node, String name ){
    ArrayList<Node> r = new ArrayList<Node>();
    NodeList children = node.getChildNodes();
    int l = children.getLength();
    for( int i = 0; i < l; ++i ){
        if( name.equals( children.item(i).getNodeName() ) )
            r.add( children.item(i) );
    }
    return r;
}

问题答案:

这是因为当您执行parent.removeChild(node)时,parent不一定是节点的父级,因为getElementsByTagName()正在进行递归搜索。



 类似资料:
  • 问题内容: 如果所有数据被意外删除,是否可以将表恢复为上一次数据。 问题答案: 还有另一种解决方案,如果服务器上有活动的二进制日志,则可以使用 用它生成一个sql文件 然后搜索您缺少的行。如果您没有激活它,则没有其他解决方案。下次进行备份。

  • 在解封XML时得到一个UnmarshalException(意外元素),我刚刚通过Marshaller运行了该元素。我看起来像是封送处理生成了XML,而XML无法解封。 以下UnmarshalException是由代码Unmarshaller.Unmarshal(reader)的最后一行引发的: 生成的XML如下所示: 为什么抛出UnmarshalException?

  • 尝试对Git存储库进行简单更改,该存储库由GitHub私有托管。我克隆了存储库,进行了更改,并将其推上进行审阅。

  • 我试图找到这个按钮并点击它。但我得到了这个错误: 我注意到它正在寻找不同的字符串bc它在类名前面添加了这个点。这是问题吗? 非常感谢。

  • https://hub.docker.com/_/python/tags?page=1&name=3.10-bullseye 上面是 docker hub 中,python:3.10-bullseye 的 digest 信息 下面是刚刚执行 docker pull python:3.10-bullseye 输出的 digest 信息 可以看到,本地拉取的 Digest 是 e917e3e93525

  • 问题内容: 有谁知道如何修理它。我正在使用Mac OS 10.8.2 问题答案: 在Python 3中是一个函数;它应该是: 正确安装或使用新版本(如果存在错误)。 在Python 3.3上工作正常。