简体   繁体   English

如何将数据从xml文件导入sqlite数据库

[英]how to import data from xml file to sqlite database

I have two XML files for the android project first consist approx 550 records of districts in India and second file consists of approx 40 records of states of India我有两个 android 项目的 XML 文件,第一个文件包含大约 550 条印度地区的记录,第二个文件包含大约 40 条印度各州的记录

Now I want to import it in an SQLite database which consists of two fields first district and second state and each record from XML should insert to separate row of SQLite.现在我想将它导入到 SQLite 数据库中,该数据库由两个字段组成,第一个区和第二个州,XML 中的每条记录都应插入到 SQLite 的单独行中。

I googled but not found any solution can anyone suggest a way or any tool for doing it.我用谷歌搜索但没有找到任何解决方案,任何人都可以提出一种方法或任何工具来做到这一点。

You probably have found a solution, but for others who may be looking for a similar solution.您可能已经找到了解决方案,但对于其他可能正在寻找类似解决方案的人来说。

Assuming you have data in XML as:假设您有 XML 格式的数据:

<rows>
    <row>
        <state>Mississippi</state>
        <district>Holmes</district>
    </row>

    <row>
        <state>Texas</state>
        <district>Cayuga ISD</district>
    </row>
    ...
</rows>

You can use this code that I used somewhere:您可以使用我在某处使用的这段代码:

public class MySQLPullParser {

    private String currTag = null;  
    private boolean firstTag = true;

    public String parseXML(Context ctx) {
        try {
            StringBuilder sb = new StringBuilder(500);  // give sufficient length to start with
            sb.append("INSERT INTO " + yourTableName + " VALUES (");

            XmlPullParserFactory xppFactory = XmlPullParserFactory.newInstance();
            xppFactory.setNamespaceAware(true);
            XmlPullParser xpp = xppFactory.newPullParser();

            URL yourXmlPath = new URL(url);
            InputStream is = yourXmlPath.openConnection().getInputStream();

                    xpp.setInput(is,null);

                    int e = xpp.getEventType();
            while (e != XmlPullParser.END_DOCUMENT)
            {
                if(e == XmlPullParser.START_TAG) {
                        currTag = xpp.getName();
                }
                else if (e == XmlPullParser.END_TAG) {
                        currTag = null;
                }
                else if (e == XmlPullParser.TEXT) {
                    if ( currTag.equals("state") ) {    // first table column
                            if (firstTag)
                                sb.append( xmlText + "(" ); // for first row insert
                            else
                                sb.append( xmlText + ",(" );
                    }

                    else if ( currTagType.equals("district") ){
                        sb.append( "'" + xmlText + "')" );  // last table column should have a closing paran ")"
                    }
                }
                e = xpp.next();
            }

        }   catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        return sb.toString();
    }
}

parseXML() will return the SQL INSERT statement that you can execute: parseXML()将返回您可以执行的 SQL INSERT 语句:

MySQLPullParser spp = new MySQLPullParser();
db.execSQL( spp.parseXML(mContext) );

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

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