2011-06-21 9 views
0

Androidアプリケーションで課金システムにAPIを使用しようとしていますが、返されるXMLを解析する方法がわかりません。ここまで私の機能はこれまでのように見えます...AndroidでXMLを解析する

public void ParseData(String xmlData) 
{ 
    try 
    { 
     // Document Builder 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = factory.newDocumentBuilder(); 

     // Input Stream 
     InputSource inStream = new InputSource(); 
     inStream.setCharacterStream(new StringReader(xmlData)); 

     // Parse Document into a NodeList 
     Document doc = db.parse(inStream); 
     NodeList nodes = doc.getElementsByTagName("ticket"); 

     // Loop NodeList and Retrieve Element Data 
     for(int i = 0; i < nodes.getLength(); i++) 
     { 
      Node node = nodes.item(i); 

      if (node instanceof Element) 
      { 
       Element child = (Element)node; 
       String id = child.getAttribute("id"); 
      } 
     } 
    } 
    catch(SAXException e) 
    { 

    } 
} 

ここでは、XMLデータが返されたようです。それぞれをループして各要素を引き出す必要がありますが、DOMパーサでどのように行うかはわかりません。

<whmcsapi> 
<action>gettickets</action> 
<result>success</result> 
<totalresults>1</totalresults> 
<startnumber>0</startnumber> 
<numreturned>1</numreturned> 
<tickets> 
    <ticket> 
    <id>1</id> 
    <tid>557168</tid> 
    <deptid>1</deptid> 
    <userid>1</userid> 
    <name><![CDATA[Array]]></name> 
    <email></email> 
    <cc></cc> 
    <c>TmDEga5v</c> 
    <date>2009-08-03 23:14:32</date> 
    <subject><![CDATA[Test Ticket]]></subject> 
    <message><![CDATA[This is a test ticket> 

    ---------------------------- 
    IP Address: xxx.xxx.xxx.xxx]]></message> 
    <status>Open</status> 
    <priority>Medium</priority> 
    <admin></admin> 
    <attachment></attachment> 
    <lastreply>2009-08-04 12:14:18</lastreply> 
    <flag>0</flag> 
    <service></service> 
    </ticket> 
</tickets> 
</whmcsapi> 

答えて

0

はいSAXパーサは、ソリューションであり、ここであなたが始めるための基本的なコードです:

void parseExampleFunction(){ 
    SAXParserFactory spf = SAXParserFactory.newInstance(); 
    SAXParser sp = spf.newSAXParser(); 
    XMLReader xr = sp.getXMLReader(); 

    File myFile = new File(//the XML file which you need to parse); 
    myFile.createNewFile(); 

    FileInputStream fOut = new FileInputStream(myFile); 
    BufferedInputStream bos = new BufferedInputStream(fOut); 

    /** Create handler to handle XML Tags (extends DefaultHandler) */ 

    MessagesXMLHandler myXMLHandler = new MessagesXMLHandler(context); 
    xr.setContentHandler(myXMLHandler); 
    xr.parse(new InputSource(bos)); 
} 

// the class where the parsing logic needs to defined.This preferably can be in a different .java file 
public class MessagesXMLHandler extends DefaultHandler{ 

    //this function is called automatically when a start tag is encountered 
    @Override 
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException 
    //variable localName is the name of the tag 

    //this function is called autiomatically when an end tag is encountered 
    @Override 
    public void endElement(String uri, String localName, String qName) throws SAXException { 
    } 

    //this function gets called to return the value stored betweeen the closing and opening tags 
    @Override 
    public void characters(char[] ch, int start, int length) throws SAXException { 
     //now variable value has the value stored between the closing and opening tags 
     String value=new String(ch,start,length); 
    } 
} 
+0

これは、DOMパーサに関して私の質問とはまったく関係ありませんでした。 –

+0

私の答えの始めにSAX Parserを使うことができるのはなぜなのでしょう? – Nitin

0

アンドロイドの最良の方法で解析XMLのは、SAXParserのを使用することです。私はデモで腹を立てて説明しました....

まず、bellwのようなアクティビティクラスを作成します。

public class ActivityForSax extends ListActivity { 
private ProgressDialog pDialog; 
private ItemXMLHandler myXMLHandler; 
private String rssFeed = "https://www.dropbox.com/s/t4o5wo6gdcnhgj8/imagelistview.xml?dl=1"; 
private TextView textview; 
private ListView mListView; 
private ArrayList<HashMap<String, String>> menuItems; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.xml_dom); 

    textview = (TextView)findViewById(R.id.textView1); 
    doParsing(); 

    mListView = getListView(); 
} 

public void doParsing(){ 

    if (isNetworkAvailable()) { 
     textview.setText("Loading...Please wait..."); 
     new AsyncData().execute(rssFeed); 
    } else { 
     showToast("No Network Connection!!!"); 
    } 
} 
class AsyncData extends AsyncTask<String, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     menuItems = new ArrayList<HashMap<String, String>>(); 
     pDialog = new ProgressDialog(ActivityForSax.this); 
     pDialog.setTitle("Loading...."); 
     pDialog.setMessage("Please wait..."); 
     pDialog.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 

     try { 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      myXMLHandler = new ItemXMLHandler(); 
      xr.setContentHandler(myXMLHandler); 

      URL _url = new URL(params[0]); 

      xr.parse(new InputSource(_url.openStream())); 

     } catch (ParserConfigurationException pce) { 
      Log.e("SAX XML", "sax parse error", pce); 
     } catch (SAXException se) { 
      Log.e("SAX XML", "sax error", se); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     textview.setText("Done!!!"); 
     if (pDialog != null && pDialog.isShowing()) { 
      pDialog.dismiss(); 
     } 

     ArrayList<Bean> itemsList = myXMLHandler.getItemsList(); 

     for (int i = 0; i < itemsList.size(); i++) { 
      Bean objBean = itemsList.get(i); 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      // adding each child node to HashMap key => value 
      map.put("TITLE :: ", objBean.getTitle()); 
      map.put("DESC :: ", objBean.getDesc()); 
      map.put("PUBDATE :: ", objBean.getPubDate()); 

      // adding HashList to ArrayList 
      menuItems.add(map); 
     } 

     // Adding menuItems to ListView 
     ListAdapter adapter = new SimpleAdapter(ActivityForSax.this, menuItems, 
       R.layout.list_item, 
       new String[] { "TITLE :: ", "DESC :: ", "PUBDATE :: " }, new int[] { 
       R.id.name, R.id.email, R.id.mobile }); 

     mListView.setAdapter(adapter); 

    } 
} 

public void showToast(String msg) { 
    Toast.makeText(ActivityForSax.this, msg, Toast.LENGTH_LONG).show(); 
} 

public boolean isNetworkAvailable() { 
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity == null) { 
     return false; 
    } else { 
     NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
     if (info != null) { 
      for (int i = 0; i < info.length; i++) { 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
        return true; 
       } 
      } 
     } 
    } 
    return false; 
} 
} 

xmlデータを解析するためのデフォルトハンドラクラスを作成する必要があります。

public class ItemXMLHandler extends DefaultHandler { 

Boolean currentElement = false; 
String currentValue = ""; 
Bean item = null; 
private ArrayList<Bean> itemsList = new ArrayList<Bean>(); 

public ArrayList<Bean> getItemsList() { 
    return itemsList; 
} 

// Called when tag starts 
@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 

    currentElement = true; 
    currentValue = ""; 

    if (localName.equals("item")) { 
     item = new Bean(); 
    } 

} 

// Called when tag closing 
@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 

    currentElement = false; 

    if (localName.equals("id")) { 
     item.setId(currentValue); 
    } else if (localName.equals("title")) { 
     item.setTitle(currentValue); 
    } else if (localName.equals("desc")) { 
     item.setDesc(currentValue); 
    } else if (localName.equals("pubDate")) { 
     item.setPubDate(currentValue); 
    } else if (localName.equals("link")) { 
     item.setLink(currentValue); 
    } else if (localName.equals("item")) { 
     itemsList.add(item); 
    } 
} 

// Called to get tag characters 
@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 

    if (currentElement) { 
     currentValue = currentValue + new String(ch, start, length); 
    } 
} 
} 

、最終的にはあなたのBeanクラスなどのような...

public class Bean { 

private String id; 
private String title; 
private String desc; 
private String pubDate; 
private String link; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getDesc() { 
    return desc; 
} 

public void setDesc(String desc) { 
    this.desc = desc; 
} 

public String getPubDate() { 
    return pubDate; 
} 

public void setPubDate(String pubDate) { 
    this.pubDate = pubDate; 
} 

public String getLink() { 
    return link; 
} 

public void setLink(String link) { 
    this.link = link; 
} 
} 
0
  1. は、ライブラリフォルダに
  2. コンパイルファイル( 'libs/java-json.jar')//をjava-json.jarを追加あなたのビルドには、この行を追加します

012に返信するxmlへのコードです。応答:

JSONObject jsonObj = null; 
try { 
     jsonObj = XML.toJSONObject(response.toString()); 
} catch (JSONException e) { 
     Log.e("JSON exception", e.getMessage()); 
     e.printStackTrace(); 
} 
関連する問題