Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create XML document on GWT client side

I'm trying to create some XML files on the client and then send them to the server (nothing special, just something like <root><blabla>...</blabla>...</root>).

Doing this by hand would be possible but extremely inflexible and I see myself making a lot of mistakes. So I was looking for a XML generator in GWT and found the "com.google.gwt.xml.client" Package. Sadly I cant find examples how to create XML documents with it. Can anybody provide me an example (or linke ot an examle)?

Best regards, Stefan

like image 448
Stefan Avatar asked Mar 14 '26 23:03

Stefan


2 Answers

Here is an example. To generate the following xml :

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

You have to write the following code on the Java client side :

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());
}
like image 90
bichehype Avatar answered Mar 17 '26 20:03

bichehype


Well ok, your anser works but some things to append.

First you have to include

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

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

second you use the following namespaces:

import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;
like image 40
Stefan Avatar answered Mar 17 '26 22:03

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!