您的位置:首页 > 移动开发 > Android开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: