2

How do I parse an XML file in GWT on the server side? I can't use XMLParser because that works only on the client. I need to read an XML file on the server side, convert the contents into a List and return them to the client using GWT-RPC.

Thanks in advance.

DFB
  • 861
  • 10
  • 25
  • This entirely depends what application stack is running on the server side. It could be a Java EE WAR container. It could be a PHP framework. It could be Python running on wsgi running Django. Or any other web framework. – Santa May 04 '11 at 16:59
  • Note that client-side GWT is basically JavaScript. It makes no assumption about the server, AFAIK. – Santa May 04 '11 at 17:00
  • @Santa - Thanks, that helped. I'm using GAE and tried to use XStream, but it had problems with GAE. I should have mentioned that in my OP. I'm now trying to use Apache Digester based on the suggestions below. – DFB May 05 '11 at 18:22

2 Answers2

1

Your question is a little unclear. What do you mean "convert the contents into a list"?

If all you are passing to the client is a list of serializable/IsSerializable POJOs, then how you get these POJOs shouldn't matter to the client.

You can use any Java compatible third party parser or something like Apache Digester to build the objects

Uri
  • 88,451
  • 51
  • 221
  • 321
  • Thanks for your response. Yes, it's a list of serializable POJOs. I used Apache Digester and it worked. But reading a file with just 600 records seemed a bit slow, though I haven't done much testing. On a different note, I'm using GAE and trying to weigh the option of storing the records in XML vs app engine datastore. Do you know which would be better approach from performance perspective as far as fetching records is concerned? – DFB May 05 '11 at 18:19
  • How big is the file that you're reading? And are you sure that the slowness is in reading the file rather than in allocating the POJOs? – Uri May 06 '11 at 17:56
  • I'm not too familiar with the GAE. I'm a big fan of XML, but it's one technology that's barely used internally at Google. My impression is that the stuff available in app engine should be better for large files, but I'm not sure. – Uri May 06 '11 at 17:57
1

I use JAXB from the Apache Project. http://jaxb.java.net/

It is a little different from the GWT XML library. Instead of writing code to crawl through the xml tree and do your own processing, you define the file definition using xsd, run a program to create a bunch of data transfer classes and then parse the xml file(s) all at once.

A few caveats:

Make sure the JAXB jars are above the jre libraries in your build path. There are issues with the latest versions and GAE, so if you are using GAE stick with the 20090708 version.

  • Thanks, I'm going to explore this more shortly. – DFB May 05 '11 at 18:23
  • Re: Memcache If you are updating the xml via a redeploy of the application, that will automatically flush memcache. Otherwise you can write a little GWT service that would allow you to force a re-read. Finally, if you want something really sophisticated you can have a version check when deciding whether to pull from memcache or from the file system. – Bradley Gottfried May 07 '11 at 17:18
  • I see what you are saying. I have 2 scenarios. 1. My current default is v5 and I redeploy v5. 2. Current default is v5, I upload v6 and make v6 as the default. Would memcache be flushed in both cases? BTW, I ended up using JAXB for parsing and it's working well so far. Thanks for your help. – DFB May 08 '11 at 02:54
  • AFAICT when you deploy a new version of your program (e.g. from inside eclipse) the memcache is always completely flushed. Furthermore if you have a new version, that is never going to share a memcache version from with an older version. So if you are updating the XML by putting it in your jar file and simply redeploying that there isn't going to be any cache issues. The issue would arise if you were putting the new xml somewhere else, which had a separate update process. Then you'd have to manually flush the cache. I have a very similar setup so I'm happy to help. – Bradley Gottfried May 09 '11 at 03:49
  • My XML is included in the application jar. So, from what you said, I should be fine. I'm giving it a shot and will put some log entries in the code initially to test the exact behavior. I'll get back on this. Thanks for sharing your experience. – DFB May 09 '11 at 07:36