【问题标题】:Call a web service and parse xml response in blackberry在黑莓中调用 Web 服务并解析 xml 响应
【发布时间】:2010-06-03 09:09:56
【问题描述】:

目前我有一个现成的黑莓应用程序设计。

现在,我需要在我的应用程序中调用 Web 服务,该 Web 服务会给我一些 xml 响应。

所以,我需要将该响应从 xml 解析为一些 POJO。

那么,为了解析 xml 响应,我应该使用基本的 DOM praser,还是应该使用任何其他 J2ME 特定的 prasing 概念?

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

提前谢谢....

【问题讨论】:

    标签: java xml blackberry blackberry-eclipse-plugin


    【解决方案1】:

    这取决于您的网络服务提供什么服务。

    如果它是基于 REST 的,那么您可能需要自己使用库来解析 XML。我只使用过kXml 2,这是一个可以在BlackBerry 设备上使用的J2ME 库。要使用它,最好链接到源代码(否则,您必须预先验证 jar 并将其导出,而这似乎对我不起作用)。如果您熟悉的话,它是一个只进的拉式解析器,类似于 .NET 中的 XmlReader。

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

    【讨论】:

    • 感谢您的回复...实际上我的 Web 服务中有一个 SOAP 案例...所以,我将按照您的建议学习 JSR 172。
    【解决方案2】:

    将您的 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));     
                }
            }
    
     }
    

    【讨论】:

      【解决方案3】:

      我相信你已经收到了请求对象。

      我将给出我用来从 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;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多