简体   繁体   中英

gwt add new child node into xml file

iam building a mobile app with mgwt. This app consists of list which content comes from xml files. I am trying to add new content into these xml files through the app.

Here is the xml file:

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

Iam trying to add a new

<option>c1 C </option>

tag within the < options > parents, so the result would be this.

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

Iam trying it with the following code, but nothing happens.

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.");
    }

what is wrong? Please help.

If you are running your code inside a phonegap container GWT.getHostPageBaseURL() will point to file://somepath on your device. I am guessing that you want to load the data from the server. You will need to use a proper url.

On the other hand you should be aware that XML processing in a browser is pretty slow and should consider using JSON as an alternative format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM