简体   繁体   English

解析本地XML文件并存储在Android的sqlite数据库中

[英]Parse a local XML file and store in sqlite database in Android

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" 我希望任何人向我展示有关如何读取本地xml文件(maths.xml)并使用解析器对其进行解析,然后将获取的信息存储到sqllite数据库(maths.db)中的代码,然后再查询该代码。给定搜索词示例“ 101”的数据库

for example the maths.xml is as follows 例如maths.xml如下

<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 将您的xml文件放入资产文件夹中,就可以以输入流的形式获取xml

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 您可以使用不同的解析器(例如SAX)解析此xml,Dom这是dom parsee的小代码

Document dom = builder.parse(raw);
Element root = dom.getDocumentElement()
NodeList mathametician = root.getElementsByTagName("mathametician");

for (int i = 0; i < mathametician.getLength(); i++) 对于(int i = 0; i <mathametician.getLength(); i ++)

and insert your data to db as you got tag from xml 并从xml中获取标记时将数据插入db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM