调用Web服务并解析黑莓中的xml响应

时间:2010-06-03 09:09:56

标签: java xml blackberry blackberry-eclipse-plugin

目前我已经为黑莓应用做好了设计。

现在,我需要在我的应用程序中调用Web服务,该Web服务将为我提供一些xml响应。

因此,我需要将该响应从xml解析为某个POJO。

因此,对于解析xml响应,我应该使用基本的DOM Praser,还是应该使用任何其他J2ME特定的prasing概念?

如果有人有相同的示例教程链接,那么对我来说非常有用。

提前致谢....

3 个答案:

答案 0 :(得分:2)

这取决于您的网络服务的用途。

如果它是基于REST的,那么您可能有责任使用库自己解析XML。我只使用了kXml 2,这是一个可以在BlackBerry设备上使用的J2ME库。要使用它,最好链接到源(否则,你必须预先验证jar并导出它,这似乎永远不适合我)。它是一个只向前拉的解析器,类似于.NET中的XmlReader,如果你熟悉它。

如果您的Web服务是基于WS *的(即它使用SOAP),您可以使用存根生成器生成可以使用的客户端类。 BlackBerry支持JSR 172,即J2ME的Web服务API。 WTK有一个运行良好的存根发生器。只需将生成器指向Web服务的wsdl文件即可。网络搜索应该说明如何使用它。

答案 1 :(得分:1)

将您的xml文件数据添加到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));     
            }
        }

 }

答案 2 :(得分:0)

我相信您已收到请求对象。

我将提供用于从XML解析请求对象的代码。

_value是对象

System.out.println("value="+_value);

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser parser = null;   // create a parser

try {
      parser = factory.newSAXParser();
    } 
catch (ParserConfigurationException e1) 
    {
      System.out.println("ParserConfigurationException"+e1.getMessage());   
     }
catch (SAXException e1) 
     {
    System.out.println("SAXException"+e1.getMessage()); 

     }

        // instantiate our handler
        PharmacyDataXMLHandler  pharmacydataXMLHandler= new PharmacyDataXMLHandler();

        ByteArrayInputStream objBAInputStream = new java.io.ByteArrayInputStream(_value.getBytes());
        InputSource inputSource = new InputSource(objBAInputStream);

        // perform the synchronous parse           
        try {
            parser.parse(inputSource, pharmacydataXMLHandler);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        _pharmacydataList =  pharmacydataXMLHandler.getpharmacydataList();

}



public class PharmacyDataXMLHandler extends DefaultHandler
{


    private Vector _pharmacyDataList = new Vector();
    PharmacyData _pharmacydata;
    StringBuffer _sb = null;

    public void warning(SAXParseException e) {
        System.err.println("warning: " + e.getMessage());

    }

    public void error(SAXParseException e) {
        System.err.println("error: " + e.getMessage());
    }

    public void fatalError(SAXParseException e) {
        System.err.println("fatalError: " + e.getMessage());

    }


    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        try{
            _sb = new StringBuffer("");
            if(localName.equals("Table"))
            {

                _pharmacydata= new PharmacyData();
            }
        }catch (Exception e) {
            System.out.println(""+e.getMessage());
        }
    }

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException
    { 
        try{
            if(localName.equals("ID"))
            {
                // System.out.println("Id :"+sb.toString());
                this._pharmacydata.setId(_sb.toString());             
            }

            else if(localName.equals("Name"))
            {
                //System.out.println("name :"+sb.toString());
                this._pharmacydata.setName(_sb.toString());           
            }

            else if(localName.equals("PharmacyID"))
            {
                // System.out.println("pharmacyId :"+sb.toString());
                this._pharmacydata.setPharmacyId(_sb.toString());             
            }

            else if(localName.equals("Password"))
            {
                // System.out.println("password :"+sb.toString());
                this._pharmacydata.setPassword(_sb.toString());           
            }

            else if(localName.equals("Phone"))
            {
                // System.out.println("phone:"+sb.toString());
                this._pharmacydata.setPhone(_sb.toString());              
            }

            else if(localName.equals("Transmit"))
            {
                //System.out.println("transmit"+sb.toString());
                this._pharmacydata.setTransmit(_sb.toString());           
            }

            else if(localName.equals("TimeZone"))
            {
                // System.out.println("timeZone"+sb.toString());
                this._pharmacydata.setTimeZone(_sb.toString());           
            }

            else if(localName.equals("FaxModem"))
            {
                // System.out.println("faxModem"+sb.toString());
                this._pharmacydata.setFaxModem(_sb.toString());           
            }

            else if(localName.equals("VoicePhone"))
            {
                // System.out.println("voicePhone"+sb.toString());
                this._pharmacydata.setVoicePhone(_sb.toString());             
            }

            else if(localName.equals("ZipCode"))
            {
                //   System.out.println("zipCode"+sb.toString());
                this._pharmacydata.setZipCode(_sb.toString());            
            }

            else if(localName.equals("Address"))
            {
                //   System.out.println("address"+sb.toString());
                this._pharmacydata.setAddress(_sb.toString());            
            }   

            else if(localName.equals("City"))
            {
                // System.out.println("city"+sb.toString());
                this._pharmacydata.setCity(_sb.toString());           
            }   

            else if(localName.equals("State"))
            {
                //   System.out.println("state"+sb.toString());
                this._pharmacydata.setState(_sb.toString());              
            }   

            else if(localName.equals("WebInterface"))
            {
                //   System.out.println("webInterface"+sb.toString());
                this._pharmacydata.setWebInterface(_sb.toString());           
            }   
            else if(localName.equals("NABPnumber"))
            {
                //   System.out.println("nabPnumber"+sb.toString());
                this._pharmacydata.setNabPnumber(_sb.toString());             
            }   

            else if(localName.equals("ServiceType"))
            {
                //   System.out.println("serviceType:"+sb.toString());
                this._pharmacydata.setServiceType(_sb.toString());            
            }   

            else if(localName.equals("Mobile"))
            {
                //   System.out.println("mobile:"+sb.toString());
                this._pharmacydata.setMobile(_sb.toString());             
            }   

            else if(localName.equals("Table"))
            {
                //   System.out.println("end table:"+sb.toString());
                _pharmacyDataList.addElement(_pharmacydata);
            }
        }catch (Exception e) {
            System.out.println(""+e.getMessage());
        }
    }


    public void characters(char ch[], int start, int length) {
        String theString = new String(ch, start, length);
        _sb.append(theString);
    }


    /**
     * @return the PharmacyDataList
     */
    public Vector getpharmacydataList() 
    {
        return _pharmacyDataList;
    }

}