在GWT客户端创建XML文档

Ste*_*fan 3 xml gwt xml-generation

我正在尝试在客户端上创建一些XML文件,然后将它们发送到服务器(没什么特别的,就像这样<root><blabla>...</blabla>...</root>).

手工完成这项工作是可能的,但非常不灵活,我发现自己犯了很多错误.所以我在GWT中寻找一个XML生成器,找到了"com.google.gwt.xml.client"包.遗憾的是,我无法找到如何使用它创建XML文档的示例.任何人都可以给我一个例子(或者是一个考试)吗?

最好的问候,Stefan

小智 7

这是一个例子.要生成以下xml:

<root>
  <node1 attribute="test">
     my value
  </node1>
  <node2 attribute="anothertest"/>
</root>
Run Code Online (Sandbox Code Playgroud)

您必须在Java客户端编写以下代码:

import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;

public static void main(String[] args) {
    Document doc = XMLParser.createDocument();

    Element root = doc.createElement("root");
    doc.appendChild(root);

    Element node1 = doc.createElement("node1");
    node1.setAttribute("attribute","test");
    node1.appendChild(doc.createTextNode("my value"));
    doc.appendChild(node1);

    Element node2 = doc.createElement("node2");
    node2.setAttribute("attribute","anothertest");
    doc.appendChild(node2);

    System.out.println(doc.toString());
}
Run Code Online (Sandbox Code Playgroud)