java - 组织.jdom.IllegalAddException : The Content already has an existing parent "root"

标签 java xml split jdom

我正在尝试使用 jDOM 拆分一个大的 XML 文件 (500mb)(我知道我可能应该尝试 SAX 但是......)但是我得到 org.jdom.IllegalAddException: The Content already has an existing parent "root"异常如下面的代码所示。

示例 xml 和代码如下。我相信所有索引检查和其他琐碎的事情都是正确的。

谢谢!!!

首先对于大量的代码感到抱歉。

<root>
  <metadata><md1>...</md1><md2>...</md2><metadata>
  <someOtherInfo><soi_1>...</soi_1></someOtherInfo>
  <collection>
    <item id="1">...</item><item id="2">...</item><item id="2">...</item>
  </collection>
</root>

split() {
    final String[] nodeNames = XmlUtils.getNodeNames(elementXpath); // returns {root, collection, item}

    // creates tree of 
    //<root>
    //  <metadata><md1>...</md1><md2>...</md2><metadata>
    //  <someOtherInfo><soi_1>...</soi_1></someOtherInfo>
    //  <collection>

    final Element originalDestination = importNodes(sourceDocument, nodeNames);  

    Element destination = null;

    // traverses to "collection" element
    Element source = sourceDocument.getRootElement();
    for (int tempCount = 1; tempCount < nodeNames.length - 1; ++tempCount) {
        source = source.getChild(nodeNames[tempCount]);
    }

    // get all "collection/item" elements
    for (Object obj : source.getChildren(nodeNames[nodeNames.length - 1])) {
        // makes sure that each out file has batchSize no of elements
        if (groupCount % batchSize == 0) {
            if (destination != null) {
                // traverse and go back up to the root
                Element root = destination;
                while (root.getParentElement() != null) {
                    root = root.getParentElement();
                }

                // this is where I get -- org.jdom.IllegalAddException: The Content already has an existing parent "root" -- exception
                final Document destDocument = new Document(destination);

                // write file to disk and reset counters
            } else {
                // create complete clone of originalDestination so that even its parents are cloned
                destination = createClone(originalDestination, nodeNames);
            }
        }

        // add this "item" element to destination "collection" element
        final Element element = (Element) obj;
        destination.addContent(((Element) element.clone()));
        count++;
        groupCount++;
    }

    if (groupCount > 0) {
        // write remaining "items" to file
    }

}

private Element createClone(final Element source, final String[] nodeNames) {
    Element destination = source;
    while (destination.getParentElement() != null) {
        destination = destination.getParentElement();
    }
    destination = (Element) destination.clone();
    for (int tempCount = 1; tempCount < nodeNames.length - 1; ++tempCount) {
        destination = destination.getChild(nodeNames[tempCount]);
    }
    return destination;
}

private Element importNodes(final Document document,
    final String[] nodeNames) {

    Element source = document.getRootElement();
    if (!source.getName().equals(nodeNames[0])) {
        return null;
    }

    Element destination = null;

    for (int count = 0; count < (nodeNames.length - 1); count++) {
        if (count > 0) {
            source = source.getChild(nodeNames[count]);
        }
        final Element child = new Element(nodeNames[count]);
        if (destination != null) {
            destination.setContent(child);
        }
        destination = child;

        // copy attributes -- don't want to clone here since this is one of the ancestors of "item"
        for (Object objAttb : source.getAttributes()) {
            Attribute attb = (Attribute) objAttb;
            destination.setAttribute(attb.getName(), attb.getValue());
        }

        // this is for <metadata> and <soneInfo> elements
        for (Object obj : source.getChildren()) {
            final Element childToClone = (Element) obj;
            if (!childToClone.getName().equals(nodeNames[count + 1])
                    && (ignoreWhiteSpaceNodes ? !childToClone.getName()
                            .equals("#text") : true)) {
                final Element clone = (Element) childToClone.clone();
                destination.addContent(clone);
            }
        }

    }

    return destination;
}

最佳答案

在将元素插入另一个文档之前,您只需要将元素与其父元素分离()。

关于java - 组织.jdom.IllegalAddException : The Content already has an existing parent "root",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6851711/

相关文章:

c# - RSS 提要 XMLDataSource 过滤器 RSS 项目

java - 如何在 JSOUP 中禁用转义模式?

python - 在第三个浮点后的新行上分割字符串 - Python

c - 在c中使用分隔符分割字符串

java - 如何将图像加载到与类相同的包中?

java - 右键单击在 NetBeans 8.0.2 中不起作用

java - NotOLE2FileException : Invalid header signature; read 0x0000000000000000, 预期为 0xE11AB1A1E011CFD0 - 文件似乎不是有效的 OLE2 文档

java - MouseInfo.getPointerInfo() - 如何获取光标 View

java - 使用 XML 布局作为动态修改布局的模板

powershell - 在 PowerShell 1.0 中使用带有制表符的字符串拆分