11.PHP生成XML数据,android解析XML案例简介
2013-06-20 00:10
721 查看
PHP生成XML数据:
<?php
$dom = new DOMDocument("1.0"); //生成XML头部
header("Content-Type: text/plain"); //确定页面类型
$persons = $dom->createElement("persons"); //生成节点元素
$dom->appendChild($persons); //添加为子节点,此子节点是头节点
$person = $dom->createElement("person");
$persons->appendChild($person); //添加子节点
$id=$dom->createAttribute("id"); //生成属性节点
$person->appendChild($id);
$id_v=$dom->createTextNode("1"); //生成属性值
$id->appendChild($id_v);
$name=$dom->createElement("name");
$person->appendChild($name);
$name_v=$dom->createTextNode("cai"); 生成节点值
$name->appendChild($name_v);
$phone=$dom->createElement("phone");
$person->appendChild($phone);
$data=$dom->createTextNode("11111");
$phone->appendChild($data);
echo $dom->saveXML(); //输出XML
?>
结果:
<?xml version="1.0"?>
-<persons>
-<person id="1">
<name>cai</name>
<phone>11111</phone>
</person>
-<person id="2">
<name>tou</name>
<phone>22222</phone>
</person>
</persons>
android 解析XML数据:
package cn.caicai.xml;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class parsexml {
public static List<people> getlastnews() throws Exception {
String path = "http://192.168.0.117/testxml/xml.php"; //远程XML数据
URL uri = new URL(path);
HttpURLConnection conn=(HttpURLConnection) uri.openConnection();
conn.setConnectTimeout(5000); //连接过期时间5S
conn.setRequestMethod("GET"); //使用get请求
if(conn.getResponseCode()==200){ //成功状态200
InputStream instream= conn.getInputStream(); //获取数据流
return pasexml(instream);
}
return null;
}
private static List<people> pasexml (InputStream instream) throws Exception {
List<people> peoples=new ArrayList<people>();
people p=null;
XmlPullParser parser=Xml.newPullParser(); //生成解析器对象
parser.setInput(instream,"UTF-8");
int event=parser.getEventType(); //获取解析事件类型,即解析到那个节点
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("person".equals(parser.getName())){
int id=new Integer(parser.getAttributeValue(0)); //获取属性值
p=new people();
p.setId(id);
}else if("name".equals(parser.getName())){
p.setName(parser.nextText()); //获取节点值
}else if("phone".equals(parser.getName())){
p.setPhone(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
peoples.add(p);
p=null;
}
break;
default:
break;
}
event=parser.next();
}
return peoples;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
package cn.caicai.xml;
public class people {
int id;
String name;
String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public people() {
}
public people(int id,String name, String phone) {
this.id=id;
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
/////////////////////////////////////////////////////////
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
</LinearLayout>
/////////////////////////////////////////////////////////
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
/>
</LinearLayout>
///////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.caicai.xml"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".PullxmlActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?php
$dom = new DOMDocument("1.0"); //生成XML头部
header("Content-Type: text/plain"); //确定页面类型
$persons = $dom->createElement("persons"); //生成节点元素
$dom->appendChild($persons); //添加为子节点,此子节点是头节点
$person = $dom->createElement("person");
$persons->appendChild($person); //添加子节点
$id=$dom->createAttribute("id"); //生成属性节点
$person->appendChild($id);
$id_v=$dom->createTextNode("1"); //生成属性值
$id->appendChild($id_v);
$name=$dom->createElement("name");
$person->appendChild($name);
$name_v=$dom->createTextNode("cai"); 生成节点值
$name->appendChild($name_v);
$phone=$dom->createElement("phone");
$person->appendChild($phone);
$data=$dom->createTextNode("11111");
$phone->appendChild($data);
echo $dom->saveXML(); //输出XML
?>
结果:
<?xml version="1.0"?>
-<persons>
-<person id="1">
<name>cai</name>
<phone>11111</phone>
</person>
-<person id="2">
<name>tou</name>
<phone>22222</phone>
</person>
</persons>
android 解析XML数据:
package cn.caicai.xml;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class parsexml {
public static List<people> getlastnews() throws Exception {
String path = "http://192.168.0.117/testxml/xml.php"; //远程XML数据
URL uri = new URL(path);
HttpURLConnection conn=(HttpURLConnection) uri.openConnection();
conn.setConnectTimeout(5000); //连接过期时间5S
conn.setRequestMethod("GET"); //使用get请求
if(conn.getResponseCode()==200){ //成功状态200
InputStream instream= conn.getInputStream(); //获取数据流
return pasexml(instream);
}
return null;
}
private static List<people> pasexml (InputStream instream) throws Exception {
List<people> peoples=new ArrayList<people>();
people p=null;
XmlPullParser parser=Xml.newPullParser(); //生成解析器对象
parser.setInput(instream,"UTF-8");
int event=parser.getEventType(); //获取解析事件类型,即解析到那个节点
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("person".equals(parser.getName())){
int id=new Integer(parser.getAttributeValue(0)); //获取属性值
p=new people();
p.setId(id);
}else if("name".equals(parser.getName())){
p.setName(parser.nextText()); //获取节点值
}else if("phone".equals(parser.getName())){
p.setPhone(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
peoples.add(p);
p=null;
}
break;
default:
break;
}
event=parser.next();
}
return peoples;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
package cn.caicai.xml;
public class people {
int id;
String name;
String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public people() {
}
public people(int id,String name, String phone) {
this.id=id;
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
/////////////////////////////////////////////////////////
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
</LinearLayout>
/////////////////////////////////////////////////////////
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
/>
</LinearLayout>
///////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.caicai.xml"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".PullxmlActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
相关文章推荐
- php/thinkphp使用PHPExcel解析表格数据,生成表格案例
- Android开发案例:网络交互&XML与json数据解析&HttpUtil优化
- Android进阶——使用Pull解析和生成轻量级数据XML
- Android中pull解析和生成xml数据到一个字符串中
- 在Android中操作XML数据-读取与解析XML数据/生成与输出XML数据
- Android 测试、数据存储与访问、XML解析与生成
- Android_Xml 文件(数据) 生成 与 解析
- Android中pull解析和生成xml数据到一个字符串中
- android-利用xml解析和生成数据
- Android-Sax解析与生成xml数据
- Android中XML和JSON数据的解析小案例
- PHP实现生成XML数据和解析XML格式数据
- Android学习之XML数据的三种解析方式以及生成XML文件
- Android - 获取网络数据,Bitmap,ANR,主线程模型,消息机制,UserAgent,乱码问题,xml生成与解析,JSON解析,MVC,SmartImageView
- Android基础——XML数据的三种解析方式
- android之json数据和xml数据的回传和解析
- Android 中 XML 数据解析详解
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- Android提高第七篇之XML解析与生成
- Android 4000 解析XML数据