XML仅允许一个根元素 [英] XML Only one root Element allowed

查看:519
本文介绍了XML仅允许一个根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试添加新的元素时,我在此代码中收到错误。

I get an Error within this Code while trying to add a new Element.

try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(openFileInput(filename));
                Node task = doc.getElementsByTagName("task").item(position);
                NodeList list = task.getChildNodes();

                for(int i = 0; i<list.getLength();i++){
                    Node node = list.item(i);

                    if("task_note".equals(node.getNodeName())){
                        node.setTextContent(taskItems.get(position).get("task_note"));
                    }
                }

                Node taskNote = doc.createElement("task_image");
                taskNote.setTextContent(taskItems.get(position).get("task_image"));
                doc.appendChild(taskNote);

                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                //DOMSource wandelt Zeichen wie < , > , & , "" automatisch in XML Sprache um
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(openFileOutput(filename,Context.MODE_PRIVATE));
                transformer.transform(source,result);
            } catch (ParserConfigurationException | IOException | SAXException | TransformerException e) {
                e.printStackTrace();
            }

这是错误:

Caused by: org.w3c.dom.DOMException: Only one root element 
        at de.appy.laluna.app.TasksList.onActivityResult(TasksList.java:161)

在for循环中,我编辑 task_note 元素。这一直有效,直到在for循环之后创建新的 Element 为止。
这会导致错误。

Inside the for loop, I edit the task_note Element. This works until I create a new Element after the for loop. This leads to the error.

出什么问题了,我该如何解决这个问题?

What is wrong and how can I solve the problem?

致谢

推荐答案

在XML文档中只能有1个根元素。

You can only have 1 root element in an XML document.

您似乎在这里加载现有的XML文档:

It looks like you are loading an existing XML document here:

Document doc = docBuilder.parse(openFileInput(filename));

并从此处获取该文档的现有节点:

And getting an existing node from that document here:

Node task = doc.getElementsByTagName("KEVOX_task").item(position);

因此,我假设它已经具有根元素。由于您从该文档中获取了元素 KEVOX_task ,因此它至少包含在根元素中。

So, I'm assuming it has a root element already. Since you are getting an element KEVOX_task from that document, it's at least contained within the root element.

稍后,您尝试附加到文档本身:

Later, you attempt to append to the document itself:

doc.appendChild(taskNote);

这将添加多个根元素。

您可能想做的是添加到您先前选择的任务元素中:

What you probably mean to do was add to your task element that you selected earlier:

task.appendChild(taskNote);

这篇关于XML仅允许一个根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆