2012-04-26 52 views
0

我正在使用soap webService並使用它收到了xsd模式格式的響應。我不知道如何解析它,我試過了代碼,但它不工作,有人可以幫助我。如何解析android中的xsd模式

我的主類是

public class Mylearning extends ListActivity { 
    //ArrayList<cat> list = null; 
    private static final String SOAP_ACTION="http://yyy.mobi/GetLearningPortalsList"; 
    private static final String METHOD_NAME ="GetLearningPortalsList"; 
    private static final String NAMESPACE ="http://yyy.mobi/"; 
    private static final String URL = "http://webservices.yyy.mobi/MobileLMSServices.asmx"; 
    private Bundle bundleResult = new Bundle(); 
    private JSONObject JSONObj; 
    private JSONArray JSONArr; 
    //private ArrayList<HashMap<String, Object>> myList; 
    SoapObject request; 
    TextView tv; 
    TextView tv1; 
    TextView tv2;    
    ListView mainListView; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mylearning); 

     //mainListView = (ListView) findViewById(R.id.main_listview); 
     request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("SiteURL","http://www.yyy.mobi/"); 
     request.addProperty("PageID","1"); 
     request.addProperty("SearchText",""); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     SoapObject result = null; 
     envelope.setOutputSoapObject(request); 
     AndroidHttpTransport sab = new AndroidHttpTransport(URL); 
     sab.debug = true; 
     try { 
      sab.call(SOAP_ACTION, envelope); 
      if (envelope.getResponse() != null) { 
       result = (SoapObject) envelope.bodyIn; 
       String[] values = new String[result.getPropertyCount()]; 
       int j = result.getPropertyCount(); 
       String repons=result.toString(); 
      // Log.d("result",repons.toString()); 
       Document doc = XMLfunctions.XMLfromString(repons); 
       int numResults = XMLfunctions.numResults(doc); 

       if((numResults <= 0)){ 
        Toast.makeText(Mylearning.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 

       NodeList nodes = doc.getElementsByTagName("result"); 
       for (int i = 0; i < nodes.getLength(); i++) {       
        HashMap<String, String> map = new HashMap<String, String>(); 
        Element e = (Element)nodes.item(i); 
        map.put("Course", XMLfunctions.getValue(e, "Course")); 
        map.put("Description", "Description:" + XMLfunctions.getValue(e, "Description")); 
        map.put("icon", "icon: " + XMLfunctions.getValue(e, "icon")); 
        mylist.add(map);    
       } 

       ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.rowmylearning, 
         new String[] { "Course", "Description","icon" }, 
         new int[] { R.id.txt1, R.id.txt2,R.id.img1 }); 

       setListAdapter(adapter); 
       final ListView lv = getListView(); 

       lv.setTextFilterEnabled(true); 
       lv.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
         @SuppressWarnings("unchecked") 
         HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
         Toast.makeText(Mylearning.this, "Course '" + o.get("Course") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        } 
       }); 

      } 
     } 

       catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 
} 

我Xmlfunction類是

public class XMLfunctions { 

    public final static Document XMLfromString(String repons){ 

     Document doc = null; 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(repons)); 
      // Log.d("message",repons.toString()); 
      doc = db.parse(is); 
      Log.d("doc", doc.toString()); 
     } catch (ParserConfigurationException e) { 
      System.out.println("XML parse error: " + e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      System.out.println("Wrong XML file structure: " + e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      System.out.println("I/O exeption: " + e.getMessage()); 
      return null; 
     } 

     return doc; 

    } 

    public final static String getElementValue(Node elem) { 
     Node kid; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()){ 
        if(kid.getNodeType() == Node.TEXT_NODE ){ 
         return kid.getNodeValue(); 
        } 
       } 
      } 
     } 
     return ""; 
    } 
    public static int numResults(Document doc){  
     Node results = doc.getDocumentElement(); 
     int res = -1; 

     try{ 
      res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue()); 
     }catch(Exception e){ 
      res = -1; 
     } 

     return res; 
    } 

    public static String getValue(Element item, String str) {  
     NodeList n = item.getElementsByTagName(str);   
     return XMLfunctions.getElementValue(n.item(0)); 
    } 
} 

回答