移動架構34 Android中解析xml檔案

2021-08-11 01:57:41 字數 2182 閱讀 8074

android移動架構彙總​​​​​​​

在安卓中通常需要解析xml檔案,獲取想要的資料,例如:資料庫公升級時,對應不同版本有不同的sql語句,可以通過xml檔案儲存起來,那麼如何在android中解析xml檔案呢?

##一、將xml檔案轉換為io流

xml檔案assets/updatexml.xml:

create table if not exists tb_user(

name text,

password text,

loginname text,

lastlogintime long,

user_id integer primary key

);create table if not exists tb_photo(

time text,

path text,

to_user text,

sendtime text

);

io流解析,生成對應xml的api(document):

inputstream is = null;

document document = null;

try catch (ioexception e) catch (parserconfigurationexception e) catch (saxexception e) finally catch (ioexception e)

}}

##二、xml解析的api

1、整個xml文件:document獲取元素節點集合

nodelist nodelist = document.getelementsbytagname("updatestep");
//updatestep 節點名稱

2、元素節點element中獲取子節點集合

nodelist nodelist = ele.getelementsbytagname("updatedb");
//ele為element型別,updatedb表示子節點名稱

3、節點集合中獲取節點

nodelist nodelist = document.getelementsbytagname("updatestep");

for (int i = 0; i < nodelist.getlength(); i++)

3、獲取元素節點的屬性

string version = ele.getattribute("version");
4、獲取元素節點的屬性的內容

ele.gettextcontent();
##三、解析

###1、每乙個節點定義乙個對應的類,用來描述記錄其結構和相關屬性

###2、從分支的子元素節點定義模型,從最上層的document進行解析

##四、定義節點對應的類

public class updatedbxml

} }public listgetcreateversions()

public void setcreateversions(listcreateversions)

}

public class createversion

} }public string getversion()

public void setversion(string version)

public listgetcreatedbs()

public void setcreatedbs(listcreatedbs)

}

public class createdb

} }public string getname()

public void setname(string name)

public listgetsqlcreates()

public void setsqlcreates(listsqlcreates)

}

##四、使用

updatedbxml xml = new updatedbxml(document)

Android中bluetooth的架構

既然我們想要系統地進行分析一下 android的 bluetooth,那麼就有必要首先來從總體架構上看一下 bluetooth在 android中是處於哪些層次,做到手中有槍,心裡不慌。bluetooth從總體來看,基本的架構圖如下 這張圖是我拷貝的網上的乙份介紹bluetooth的ppt上,他應該...

Android中的MVP架構

model 業務邏輯和實體模型 controllor 是應用程式中處理使用者互動的部分,activity來充當。檢視 view 負責介面資料的展示,與使用者進行互動,就是activity 主導器 presenter 相當於協調者,是模型與檢視之間的橋梁,將模型與檢視分離開來。通過presenter進...

Android中架構模式 MVC

參考 android當中的mvc m 資料庫層,資料模型層,網路請求管理,資料儲存類的東西,都可以屬於m層,簡單點理解就是資料層,或者叫基礎層。v 這一層在android當中其實是xml,也就是res資源檔案下的layout們。可不是activity。c 這一層在android當中才是activit...