Java XML 添加与子元素相同的元素(获取 HIERARCHY_REQUEST_ERR)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9679341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:04:04  来源:igfitidea点击:

XML adding the same element as a child (Getting HIERARCHY_REQUEST_ERR)

javaxmldom

提问by rommel

How do I add the same element as a child. I want something like this:

如何添加与子元素相同的元素。我想要这样的东西:

<family>
  <parent name = "P1">
    <child name = "P1C1">
        <child name = "P1C1C1"/>
        <child name = "P1C1C2"/>
    </child>
    <child name = "P1C2"/>
    <child name = "P1C3">
      <child name = "P1C3C1"/>
    </child>
  </parent>
  <parent name = "P2">
    <child name = "P2C1">
        <child name = "P2C1C1"/>
    </child>
    <child name = "P2C2">
        <child name = "P2C2C1"/>
    </child>
  </parent>
</family>

If I try to create a child element and add another child to it I get:

如果我尝试创建一个子元素并向其中添加另一个子元素,我会得到:

HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.

HIERARCHY_REQUEST_ERR:尝试插入不允许的节点。

How can we generate the above XML tree?

我们如何生成上述 XML 树?

Also see the bigger picture for the solution I am building: Generate XML mapping of a recursive directory search

另请参阅我正在构建的解决方案的大图: 生成递归目录搜索的 XML 映射

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class ChildofChildXML {

    public static void main(String[] args) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            Element root = doc.createElement("family");   
            doc.appendChild(root);   

            Element parent = doc.createElement("parent");
            parent.setAttribute("name","P1");
            root.appendChild(parent);

            Element childElement = doc.createElement("child");
            childElement.setAttribute("name","P1C1");
            parent.appendChild(childElement);


            Element childElement2 = doc.createElement("child"); //Works
            childElement2.setAttribute("name","P1C1C1");
            childElement.appendChild(childElement2);

            /*
            childElement = doc.createElement("child");
            childElement.setAttribute("name","P1C1C1");

            // Not allowed - HIERARCHY_REQUEST_ERR: 
            childElement.appendChild(childElement); 
            */

            childElement = doc.createElement("child");
            childElement.setAttribute("name","P1C2");
            parent.appendChild(childElement);

            TransformerFactory tranFactory = TransformerFactory.newInstance();
            Transformer aTransformer = tranFactory.newTransformer();
            aTransformer.setOutputProperty("indent", "yes");

            Source src = new DOMSource(doc);
            Result dest = new StreamResult(System.out);
            aTransformer.transform(src, dest);  
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Now the code needs to be merged with a recursive 'expand archive' call and that is where I am wondering how to add child of child as I need to apply the solution to recursion - where I am expanding a top level archive file using java.util.zip. The here is equivalent of the folder that contains the top level archive that being the parent and subsequent nodes are equivalent of the contents of the archives recursively. So an ear could have a war or a jar. The war could have jsp under it and WEB-INF/lib/x.jar and WEB-INF/lib/x.jar needs to be expanded again and so on and this child --> child --> child relationship is to be built up from the parent.

现在代码需要与递归的“扩展存档”调用合并,这就是我想知道如何添加孩子的孩子,因为我需要将解决方案应用于递归 - 我正在使用 java 扩展顶级存档文件。实用工具.zip。这里相当于包含作为父节点的顶级存档的文件夹,后续节点递归地相当于存档的内容。所以一只耳朵可能有War或罐子。War下可以有jsp,WEB-INF/lib/x.jar和WEB-INF/lib/x.jar需要再扩展等等,这个child-->child-->child关系是从父母建立。

回答by Andreas Dolk

You can add a <child>node as a child to <parent>and other <child>nodes. It is not allowed to add it to <family>or to the document directly. (rough guess - I'd need to see the schema or DTD)

您可以将一个<child>节点作为子节点添加到<parent>其他<child>节点。不允许将其<family>直接添加到文档中。(粗略猜测 - 我需要查看架构或 DTD)



Second - if you create elements for a document, always use this pattern:

其次 - 如果您为文档创建元素,请始终使用此模式:

Element element = doc.createElement("child");

回答by Jon Skeet

This is the problem:

这就是问题:

childElement.appendChild(childElement);

You're trying to add a child element to itself. You can't do that. It doesn't make any sense to do that... an element can't be a child of itself. How would you expect that to be represented in the final XML?

您正在尝试向自身添加子元素。你不能那样做。这样做没有任何意义……一个元素不能是它自己的子元素。您希望在最终的 XML 中如何表示?

If you change that code to

如果您将该代码更改为

parent.appendChild(childElement);

or

或者

childElement2.appendChild(childElement);

then it's fine.

然后就好了。