0

現在、ブラックベリー アプリケーションの準備が整っています。

ここで、アプリで Web サービスを呼び出す必要があります。その Web サービスは、xml 応答を返します。

そのため、xml から POJO への応答を解析する必要があります。

では、xml 応答を解析するには、基本的な DOM praser を使用する必要がありますか、それとも他の J2ME 固有の prasing コンセプトを使用する必要がありますか?

誰かが同じチュートリアルのサンプル リンクを持っている場合、それは私にとって非常に役立ちます。

前もって感謝します....

4

3 に答える 3

2

それはあなたのウェブサービスが何を提供するかに依存します。

RESTベースの場合は、ライブラリを使用してXMLを自分で解析する必要があります。私はこれまで、BlackBerryデバイスで使用できるJ2MEライブラリであるkXml2のみを使用しました。これを使用するには、ソースにリンクするのが最善です(そうでない場合は、jarを事前に確認してエクスポートする必要がありますが、それは私にはうまくいかないようです)。これは、.NETのXmlReaderに似ている、フォワード専用のプルパーサーです。

WebサービスがWS*ベースの場合(つまり、SOAPを使用している場合)、スタブジェネレーターを使用して、使用できるクライアントクラスを生成できます。BlackBerryは、J2MEのWebサービスAPIであるJSR172をサポートしています。WTKには、適切に機能するスタブジェネレーターがあります。ジェネレーターをWebサービスのwsdlファイルにポイントするだけです。ウェブ検索はそれを使用する方法を明らかにする必要があります。

于 2010-06-04T13:49:42.503 に答える
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));     
            }
        }

 }
于 2010-06-04T12:03:05.950 に答える
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;
    }

}
于 2011-01-21T12:14:48.317 に答える