解析在Android的SQLite数据库本地XML文件和存储 [英] Parse a local XML file and store in sqlite database in Android
问题描述
我想任何人告诉我如何读取本地XML文件(maths.xml)和一个解析器解析它,然后获得存储到数据库sqllite(maths.db)的信息code和之后如何查询数据库对于给定的搜索词例如101
例如maths.xml如下:
<数学>< mathametician>
<&ID GT 101 LT; / ID>
<名称> K和LT; /名称>
<年龄> 75℃/年龄>
< / mathametician>< mathametician>
<&ID GT; 102 LT; / ID>
<名称>&大卫LT; /名称>
<年龄> 62 LT; /年龄>
< / mathametician>< /数学>
把你的XML文件导入到资源文件夹,你可以得到的输入流的形式XML
的InputStream原料= context.getApplicationContext()。getAssets()。打开(
maths.xml);
您可以通过分析不同的解析器像SAX此xml,大教堂这里是DOM帕西小code
文档DOM = builder.parse(生);
元根= dom.getDocumentElement()
节点列表mathametician = root.getElementsByTagName(mathametician);
的for(int i = 0; I< mathametician.getLength();我++)
和插入您的数据DB,你得到了XML标签
I would like anybody to show me the CODE on how to read a local xml file (maths.xml) and parse it with a parser and then store the information obtained into a sqllite database (maths.db) and after that how to query the database for a given search word example "101"
for example the maths.xml is as follows
<maths>
<mathametician>
<id>101</id>
<name>kurt</name>
<age>75</age>
</mathametician>
<mathametician>
<id>102</id>
<name>david</name>
<age>62</age>
</mathametician>
</maths>
put your xml file into asset folder and you can get xml in form of input stream
InputStream raw = context.getApplicationContext().getAssets().open(
"maths.xml");
you can parse this xml using different parser like SAX,Dom here is small code of dom parsee
Document dom = builder.parse(raw);
Element root = dom.getDocumentElement()
NodeList mathametician = root.getElementsByTagName("mathametician");
for (int i = 0; i < mathametician.getLength(); i++)
and insert your data to db as you got tag from xml
这篇关于解析在Android的SQLite数据库本地XML文件和存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!