在GWT客户端创建XML文档



我试图在客户端上创建一些XML文件,然后将它们发送到服务器(没有什么特别的,只是<root><blabla>...</blabla>...</root>)。

手工操作是可能的,但非常不灵活,我看到自己犯了很多错误。因此,我在GWT中寻找XML生成器,并找到了"com.google.gwt.xml"。客户端"包。遗憾的是,我找不到如何使用它创建XML文档的示例。谁能给我一个例子(或一个例子的链接)?

最诚挚的问候,Stefan

下面是一个例子。生成以下xml:

<root>
  <node1 attribute="test">
     my value
  </node1>
  <node2 attribute="anothertest"/>
</root>

必须在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());
}

好吧,你的答案是有效的,但是有些东西需要追加。

首先你必须包含

<inherits name="com.google.gwt.xml.XML" />

在*gwt.xml文件(http://blog.elitecoderz.net/gwt-and-xml-first-steps-with-comgooglegwtxmlerste-schritte-mit-gwt-und-xml-unter-comgooglegwtxml/2009/05/)

使用以下命名空间:

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

接受的答案是正确的,但是上面有一点错误,node1node2应该链接到root,而不是链接到doc

所以这一行:

doc.appendChild(node1);

应该是:

root.appendChild(node1);

最新更新