2013-05-09 69 views
-1

我想從放置在assets文件夾中的xml數據庫中提取數據。 //來自ASSETS的Android- XML數據庫

<?xml version="1.0" encoding="utf-8" ?> 
    <Levels> 

    <level id ="0"> 
    <start>3,2</start> 
    <one>1,1</one> 
    </level> 
    <level id ="1"> 
<start>3,2</start> 
    <one>4,1</one> 
    </level> 
    </Levels> 

我想,該數據上拉將通過ID例如數據庫的樣本ID = 0返回<開始> 3,2 < /啓動> <一個> 1,1 < /一個>。有一個問題 - 我不知道該怎麼做 請幫助我 // ------------------------------- -------------------------------------------------- ------ 編輯: IM得到一個錯誤,請檢查我的XMLParser的類:

package com.example.com.worfield.barak; 
    import java.io.IOException; 
import java.io.StringReader; 

    import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.apache.http.impl.client.DefaultHttpClient; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import android.content.Context; 
import android.sax.Element; 
    import android.util.Log; 


public class XMLParser 
{ 
Context context; 

    public XMLParser(Context c) 
    { 

    context = c; 
} 

public Document getDomElement() 
{ 
    Document doc = null; 
    try 
    { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    doc = db.parse(context.getAssets().open("Level5X5Three.xml")); 

    } 
    catch(Exception ex) 
    { 

     Log.e("xml parse error", "eror in getdomelement()"); 
    } 
     return doc; 
} 
public String getValue(Element e, String str) 
{  
    NodeList n = ((Document) e).getElementsByTagName(str);  //problem 
    return this.getElementValue(n.item(0)); 
} 

public final String getElementValue(Node elem) 
{ 
     Node child; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
        if(child.getNodeType() == Node.TEXT_NODE ){ 
         return child.getNodeValue(); 
        } 
       } 
      } 
     } 
     return ""; 
    } 
    } 

回答