来自ASSETS的Android-XML数据库

时间:2013-05-09 17:49:48

标签: android android-xml android-assets

我想从放置在assets文件夹中的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返回&lt;开始&gt; 3,2 LT; /开始&gt; &LT;一个&GT; 1,1 LT; /一个取代。有一个问题 - 我不知道该怎么做 请帮我 // ------------------------------------------------ --------------------------------------- 编辑: 我收到错误请检查我的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 "";
  } 
    }

1 个答案:

答案 0 :(得分:0)

只需按照以下教程解释您需要的一切

http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/