//解析本地文件XML内容(放在assets下)
Android
Android Studio
IOS
XCode
//布局activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//布局layout\list_item.xml
android:layout_height="match_parent"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//MainActivity
package com.example.xml;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private SQLiteDatabase db;
private String id;
private String name;
private String ide;
private ListView listView;
private ArrayList
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
// TextView tv_name = (TextView) findViewById(R.id.tv_name);
listView = (ListView) findViewById(R.id.listView);
// 调用方法
MyHelper my = new MyHelper(this);
db = my.getReadableDatabase();
ulist = new ArrayList
MyBaseadpter myadpter = new MyBaseadpter(this, ulist);
listView.setAdapter(myadpter);
try {
// 得到工厂
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 得到builder
DocumentBuilder builder = factory.newDocumentBuilder();
// 打开文件的目录
Document document = builder.parse(getAssets()
.open("languagess.xml"));
// 得到目录的元素
Element element = document.getDocumentElement();
// 获取文件的属性
String categroy = element.getAttribute("categroy");
// 输出
// tv_name.append(categroy + "\n");
// 得到子节点
NodeList list = element.getElementsByTagName("item");
for (int i = 0; i < list.getLength(); i++) {
Element element2 = (Element) list.item(i);
id = element2.getAttribute("id");
name = element2.getElementsByTagName("name").item(0)
.getTextContent();
ide = element2.getElementsByTagName("ide").item(0)
.getTextContent();
// tv_name.append("id:" + id);
// tv_name.append("name:" + name);
// tv_name.append("ide:" + ide);
ContentValues values = new ContentValues();
values.put("_id", id);
values.put("name", name);
values.put("ide", ide);
db.insert("t_user", null, values);
// add();
}
} catch (Exception e) {
e.printStackTrace();
}
cha();
}
private void cha() {
Cursor cursor = db.query("t_user", null, null, null, null, null, null,
null);
while (cursor.moveToNext()) {
User u = new User();
u.setId(cursor.getString(cursor.getColumnIndex("_id")));
u.setName(cursor.getString(cursor.getColumnIndex("name")));
u.setIde(cursor.getString(cursor.getColumnIndex("ide")));
ulist.add(u);
}
}
private void add() {
ContentValues values = new ContentValues();
values.put("_id", id);
values.put("name", name);
values.put("ide", ide);
db.insert("t_user", null, values);
}
}
//适配器类MyBaseadpter
package com.example.xml;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyBaseadpter extends BaseAdapter {
private Context context;
private List
public MyBaseadpter(Context context, List
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item, null);
TextView id = (TextView) view.findViewById(R.id.id);
TextView name = (TextView) view.findViewById(R.id.name);
TextView ide = (TextView) view.findViewById(R.id.ide);
id.setText(list.get(position).getId());
name.setText(list.get(position).getName());
ide.setText(list.get(position).getIde());
return view;
}
}
//工具类MyHelper
package com.example.xml;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "xml.db3", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table t_user(_id varchar(0),name varchar(0),ide varchar(0))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
//Bean包User
package com.example.xml;
public class User {
private String id;
private String name;
private String ide;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIde() {
return ide;
}
public void setIde(String ide) {
this.ide = ide;
}
public User(String id, String name, String ide) {
super();
this.id = id;
this.name = name;
this.ide = ide;
}
public User() {
super();
}
}