views:

219

answers:

2

Hello All...

Currently I have a ready design for blackberry application.

Now, I need to call the web service in my app, and that web service will give me some xml response.

So, I need to parse that response from xml to some POJO.

So, for parsing the xml response should I go with the basic DOM praser, or should I use any other J2ME specific prasing concept ?

If anybody have any sample tutorial link for the same then it would be very much useful to me.

Thanks in advance....

+1  A: 

Add your xml file data in to strXML

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputStream inputStream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); Document document = builder.parse( inputStream ); Element rootElement = document.getDocumentElement(); rootElement.normalize(); blnViewReport=false; listNodes(rootElement); // use this function to parse the xml inputStream.close();

void listNodes(Node node) { Node tNode; String strData; String nodeName = node.getNodeName();

    if( nodeName.equals("Tagname"))
    {
tNode=node.getFirstChild();
        if(tNode.getNodeType() == Node.TEXT_NODE)
        {
    // here you get the specified tag value
        }
   }
  else if(nodeName.equals(“Tag name 2”))
       .....
       .....

    NodeList list = node.getChildNodes();       
    if(list.getLength() > 0)
    {                  
        for(int i = 0 ; i<list.getLength() ; i++) 
        {
           listNodes(list.item(i));     
        }
    }

}

Karthick
+1  A: 

It depends on what your web service serves.

If it is REST-based, you're likely responsible to parse the XML yourself, with a library. I've only ever used kXml 2, a J2ME library that can be used on BlackBerry devices. To use it, it's best to link to the source (otherwise, you have to preverify the jar and export it and that never seems to work for me). It's a forward-only pull parser, similar to XmlReader in .NET, if you're familiar with that.

If your web service is WS*-based (i.e. it uses SOAP), you can use a stub generator to generate a client class that you can use. BlackBerry supports JSR 172, the web services API for J2ME. The WTK has a stub generator that works well. Just point the generator to your web service's wsdl file. A web search should clarify how to use it.

ageektrapped
@ageektrapped Thanks for the response... Actually I have a case of SOAP in my web service... So, i will study JSR 172 as you suggest.
Nirmal