1.xml解析技术和xml生成技术 DOM(Document Object Model,文档对象模型):它是由W3C定义的独立于语言与平台的XML处理接口规范,并在多种语言中得到了实现。XML文档结构类似于一棵树,而DOM规范正是将XML的树形结构映射到内存中的一个树形的树形结构上。在这棵树上,每个节点对应这XML文档树的一个节点,允许动态操作这个树模型的内容风格和结构。 SAX(Simple API for XML,XML的简单API)提供了一个事件驱动,顺序访问的机制来读取XML文档的内容。与DOM相比,SAX具有更快的处理速度和更低的内存需求,因此他被经常用于各类面向网络的应用。但是,采用SAX机制将比采用DOM接口要求更多的代码,因为它是事件驱动的,需要实现它的回调接口。 demo链接:http://xizhenyin.iteye.com/blog/377313 xml解析: private void saxXml() { Hander: public class MyDataHander extends DefaultHandler { 2.json数据传输技术和json数据解析操作技术 例子永远是最好的教程,下面我们来看个例子! 有这样一个JSON数据:"{"username":"zhangsan","password":"123456"}" 通过解析后对应的数据显示在相应的控件中: 就是上面这种效果。 在Android中使用json需要一个jar包,gson-1.7.jar;可以在google的网站上下载。把这个包加到项目的构建路径中就行了。 下面是这个项目的源码(源码中的类及方法可以参考API文档): AndroidManifest.xml: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gufengxiachen" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".json" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/username" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" ></EditText> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" ></EditText> <Button android:id="@+id/parse" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/parse" > </Button> </LinearLayout>
json.java:
package com.gufengxiachen; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class json extends Activity { /** Called when the activity is first created. */
private String name; private String ages;
public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAges() { return ages; } public void setAges(String ages) { this.ages = ages; } private EditText username=null; private EditText password=null; private Button parse=null;
private String jsonData = "[{\"username\":\"zhagnsan\",\"password\":\"123456\"}]"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); username = (EditText)findViewById(R.id.username); password = (EditText)findViewById(R.id.password); parse = (Button)findViewById(R.id.parse); parse.setOnClickListener(new parseListener()); } public class parseListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub ParseJson parseJson = new ParseJson(); json json = parseJson.parse(jsonData);
username.setText(json.getName());
password.setText(json.getAges());
} } }
ParseJson.java:
package com.gufengxiachen; import java.io.IOException; import java.io.StringReader; import com.google.gson.stream.JsonReader; public class ParseJson {
public json parse(String jsonData){ json json =new json(); JsonReader jsonReader = new JsonReader(new StringReader(jsonData)); try { jsonReader.beginArray(); while(jsonReader.hasNext()){ jsonReader.beginObject(); while(jsonReader.hasNext()){ String tagName = jsonReader.nextName(); if(tagName.equals("username")){
json.setName(jsonReader.nextString()); System.out.println(json.getName()); }else if(tagName.equals("password")){
json.setAges(""+jsonReader.nextInt());
System.out.println(json.getAges()); } } jsonReader.endObject(); } jsonReader.endArray();
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json; } } |
android xml解析技术 json数据传输技术
android xml解析技术 json数据传输技术