【问题标题】:How to convert XML file with multiple tag to string in Android?如何在Android中将具有多个标签的XML文件转换为字符串?
【发布时间】:2011-12-08 09:42:44
【问题描述】:

如何在Android中将具有多个标签的XML文件转换为字符串? 我们如何将 XML 文件转换为 String 并根据条件访问内部标签的数据?

【问题讨论】:

    标签: android xml string


    【解决方案1】:

    我在下面给出了一个例子。我认为本指南会对您有所帮助。 Link

    在您的 res/rw/sql.xml 中:

    <sql>
    <statement>
    CREATE TABLE IF NOT EXISTS employee (
            _id INTEGER PRIMARY KEY AUTOINCREMENT, 
            firstName VARCHAR(50), 
            lastName VARCHAR(50), 
            title VARCHAR(50), 
            department VARCHAR(50), 
            managerId INTEGER, 
            city VARCHAR(50), 
            officePhone VARCHAR(30), 
            cellPhone VARCHAR(30), 
            email VARCHAR(30), 
            picture VARCHAR(200))
    </statement>
    <statement>INSERT INTO employee VALUES(1,'Ryan','Howard','Vice President, North East', 'Management', NULL, 'Scranton','570-999-8888','570-999-8887','ryan@dundermifflin.com','howard.jpg')</statement>
    <statement>INSERT INTO employee VALUES(2,'Michael','Scott','Regional Manager','Management',1,'Scranton','570-888-9999','570-222-3333','michael@dundermifflin.com','scott.jpg')</statement>
    <statement>INSERT INTO employee VALUES(3,'Dwight','Schrute','Assistant Regional Manager','Management',2,'Scranton','570-444-4444','570-333-3333','dwight@dundermifflin.com','schrute.jpg')</statement>
    <statement>INSERT INTO employee VALUES(4,'Jim','Halpert','Assistant Regional Manager','Manage',2,'Scranton','570-222-2121','570-999-1212','jim@dundermifflin.com','halpert.jpg')</statement>
    <statement>INSERT INTO employee VALUES(5,'Pamela','Beesly','Receptionist','',2,'Scranton','570-999-5555','570-999-7474','pam@dundermifflin.com','beesly.jpg')</statement>
    <statement>INSERT INTO employee VALUES(6,'Angela','Martin','Senior Accountant','Accounting',2,'Scranton','570-555-9696','570-999-3232','angela@dundermifflin.com','martin.jpg')</statement>
    <statement>INSERT INTO employee VALUES(7,'Kevin','Malone','Accountant','Accounting',6,'Scranton','570-777-9696','570-111-2525','kmalone@dundermifflin.com','malone.jpg')</statement>
    <statement>INSERT INTO employee VALUES(8,'Oscar','Martinez','Accountant','Accounting',6,'Scranton','570-321-9999','570-585-3333','oscar@dundermifflin.com','martinez.jpg')</statement>
    <statement>INSERT INTO employee VALUES(9,'Creed','Bratton','Quality Assurance','Customer Services',2,'Scranton','570-222-6666','333-8585','creed@dundermifflin.com','bratton.jpg')</statement>
    <statement>INSERT INTO employee VALUES(10,'Andy','Bernard','Sales Director','Sales',2,'Scranton','570-555-0000','570-546-9999','andy@dundermifflin.com','bernard.jpg')</statement>
    <statement>INSERT INTO employee VALUES(11,'Phyllis','Lapin','Sales Representative','Sales',10,'Scranton','570-141-3333','570-888-6666','phyllis@dundermifflin.com','lapin.jpg')</statement>
    <statement>INSERT INTO employee VALUES(12,'Stanley','Hudson','Sales Representative','Sales',10,'Scranton','570-700-6666','570-777-6666','shudson@dundermifflin.com','hudson.jpg')</statement>
    <statement>INSERT INTO employee VALUES(13,'Meredith','Palmer','Supplier Relations','Customer Services',2,'Scranton','570-555-8888','570-777-2222','meredith@dundermifflin.com','palmer.jpg')</statement>
    <statement>INSERT INTO employee VALUES(14,'Kelly','Kapoor','Customer Service Rep.','Customer Services',2,'Scranton','570-123-9654','570-125-3666','kelly@dundermifflin.com','kapoor.jpg')</statement>
    </sql>
    

    在你的助手类中:

    public void onCreate(SQLiteDatabase db) {
                    String s;
                    try {
                            Toast.makeText(context, "1", 2000).show();
                            InputStream in = context.getResources().openRawResource(R.raw.sql);
                            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                            Document doc = builder.parse(in, null);
                            NodeList statements = doc.getElementsByTagName("statement");
                            for (int i=0; i<statements.getLength(); i++) {
                                    s = statements.item(i).getChildNodes().item(0).getNodeValue();
                                    db.execSQL(s);
                            }
                    } catch (Throwable t) {
                            Toast.makeText(context, t.toString(), 50000).show();
                    }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 2014-06-16
      • 2019-02-07
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多