java 如何使用java在文档中附加新节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10789066/
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-10-31 02:34:57  来源:igfitidea点击:

how to append new node in document using java

javaxpath

提问by Manoj Singh

I have below updateFile code, here I am trying to add new node when there is no publicationid in my xml file.

我有下面的 updateFile 代码,在这里我试图在我的 xml 文件中没有 Publicationid 时添加新节点。

public static void UpdateFile(String path, String publicationID, String url) {
        try {

            File file = new File(path);
            if (file.exists()) {
                DocumentBuilderFactory factory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(file);
                document.getDocumentElement().normalize();
                 XPathFactory xpathFactory = XPathFactory.newInstance();
                 // XPath to find empty text nodes.
                 String xpath = "//*[@n='"+publicationID+"']"; 
                 XPathExpression xpathExp = xpathFactory.newXPath().compile(xpath);  
                 NodeList nodeList = (NodeList)xpathExp.evaluate(document, XPathConstants.NODESET);
                //NodeList nodeList = document.getElementsByTagName("p");
                 if(nodeList.getLength()==0)
                 {
                     Node node = document.getDocumentElement();
                     Element newelement = document.createElement("p");
                     newelement.setAttribute("n", publicationID);
                     newelement.setAttribute("u", url);
                     newelement.getOwnerDocument().appendChild(newelement);
                     System.out.println("New Attribute Created");
                 }
                 System.out.println();

                //writeXmlFile(document,path);
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }

In above code I am using XPathExpression and all the matched node are added in NodeList nodeList = (NodeList)xpathExp.evaluate(document, XPathConstants.NODESET);

在上面的代码中,我使用 XPathExpression 并且所有匹配的节点都添加到 NodeList nodeList = (NodeList)xpathExp.evaluate(document, XPathConstants.NODESET);

Here I am checking if (nodeList.getLength()==0) then that means I don't have any node with the publicationid passed.

在这里我检查是否 (nodeList.getLength()==0) 那么这意味着我没有任何节点通过了publishedid。

And if there is no node as such I want to create a new node.

如果没有这样的节点,我想创建一个新节点。

In this line newelement.getOwnerDocument().appendChild(newelement); its giving error (org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. ).

在这一行 newelement.getOwnerDocument().appendChild(newelement); 它给出的错误(org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: 尝试在不允许的地方插入节点。)。

Please suggest!!

请建议!!

回答by Jon Skeet

You're currently calling appendChildon the document itself. That would end up creating multiple root elements, which obviously you can't do.

您当前正在调用appendChild文档本身。这最终会创建多个根元素,这显然是你做不到的。

You need to find the appropriate element where you want to add the node, and add it to that. For example, if you wanted to add the new element tothe root element, yo ucould use:

您需要找到要在其中添加节点的适当元素,并将其添加到该元素中。例如,如果您想将新元素添加根元素,您可以使用:

document.getDocumentElement().appendChild(newelement);