gwt将新的子节点添加到xml文件中

时间:2013-06-26 11:25:04

标签: java xml gwt mgwt

iam用mgwt构建移动应用程序。此应用程序包含列出哪些内容来自xml文件。 我正在尝试通过应用程序将新内容添加到这些xml文件中。

这是xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <options>
            <option>c1 A</option>
            <option>c1 B</option>
    </options>
 </root>

我试图添加新的

<option>c1 C </option>

标签内的&lt;选项&gt;父母,结果就是这样。

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <options>
            <option>c1 A</option>
            <option>c1 B</option>
            <option>c1 C </option>
    </options>
 </root>

我尝试使用以下代码,但没有任何反应。

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getHostPageBaseURL()+"folderName/fileName.xml");
    try{
        builder.sendRequest(null, new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                Document document = XMLParser.parse(response.getText());


                Element elementRoot = document.getDocumentElement();

                Element options = (Element)elementRoot.getElementsByTagName("options").item(0);

                Element optionElement = document.createElement("option");
                optionElement.appendChild(document.createTextNode("c1 C"));

                options.appendChild(optionElement);

            }

            @Override
            public void onError(Request request, Throwable exception) {

            }
        });
    }catch(RequestException e){
        Window.alert("Unable to build the request.");
    }

有什么不对?请帮忙。

1 个答案:

答案 0 :(得分:1)

如果您在phonegap容器中运行代码,GWT.getHostPageBaseURL()将指向您设备上的file:// somepath。我猜你想从服务器加载数据。您需要使用正确的网址。

另一方面,您应该知道浏览器中的XML处理速度非常慢,应考虑使用JSON作为替代格式。