views:

370

answers:

2

I know that browsers do support XML with DOM approach

I have an application that uses GWT and it uploads file at the server, when the uploading completes, i need the server to respond back to the client with a Bean since this is a file upload, the response is handled by a servlet.

I am able to read a string at the client by reading the output produced by the servlet. I plan to convert my bean into an XML like structure that would be converted back into an object at the client.

So, is it possible for my client to treat this response as XML and iterate through it ?

+2  A: 

see more at http://gwt.components.googlepages.com/simplexmlparser

private void parseMessage(String messageXml) {
  try {
    // parse the XML document into a DOM
    Document messageDom = XMLParser.parse(messageXml);

    // find the sender's display name in an attribute of the <from> tag
    Node fromNode = messageDom.getElementsByTagName("from").item(0);
    String from = ((Element)fromNode).getAttribute("displayName"); 
    fromLabel.setText(from);

    // get the subject using Node's getNodeValue() function
    String subject = messageDom.getElementsByTagName("subject").item(0).getFirstChild().getNodeValue();
    subjectLabel.setText(subject);

    // get the message body by explicitly casting to a Text node
    Text bodyNode = (Text)messageDom.getElementsByTagName("body").item(0).getFirstChild();
    String body = bodyNode.getData();
    bodyLabel.setText(body);    

  } catch (DOMException e) {
    Window.alert("Could not parse XML document.");
  }
}
Tzury Bar Yochay
A: 

you can also parse xml using jquery, and its easy to wrap the jquery call with jsni in GWT. much less verbose as demonstrated here http://marcgrabanski.com/article/jquery-makes-parsing-xml-easy

edit: GwtQuery might even do it? not sure, since its not a straight port.

Chii