我正在尝试在客户端上创建一些XML文件,然后将它们发送到服务器(没有什么特别的,就像<root><blabla>...</blabla>...</root>
那样。)
手动执行此操作是可能的,但非常不灵活,我发现自己犯了很多错误。所以我在GWT中寻找一个XML生成器,找到了“com.google.gwt.xml.client”包。遗憾的是,我无法找到如何使用它创建XML文档的示例。任何人都可以给我一个例子(或者是一个考试)吗?
祝你好运, 斯蒂芬
答案 0 :(得分:7)
这是一个例子。要生成以下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());
}
答案 1 :(得分:3)
好吧,你的anser工作但有些东西要追加。
首先你必须包括
<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;
答案 2 :(得分:1)
接受的答案是对的,但是有一点错误, node1 和 node2 应该链接到 root ,而不是<强>文档强>
所以这一行:
doc.appendChild(node1);
应该是:
root.appendChild(node1);