[英]gwt add new child node into xml file
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>
標簽在<options>父母中,所以結果就是這樣。
<?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.");
}
怎么了? 請幫忙。
如果您在電話差距容器中運行代碼,則GWT.getHostPageBaseURL()將指向設備上的file:// somepath。 我猜您想從服務器加載數據。 您將需要使用適當的網址。
另一方面,您應該意識到瀏覽器中的XML處理非常慢,應該考慮使用JSON作為替代格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.