11.PHP生成XML数据,android解析XML案例简介

来源:互联网 发布:南风知我意1百度云 编辑:程序博客网 时间:2024/05/21 05:05

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>

原创粉丝点击