php 读xml的两种方式

<?xml version="1.0" encoding="ISO-8859-1"?>
<st>
<stu>
<name>sunzhiyan</name>
<sex>men</sex>
<age>20</age>
</stu>

<stu>
<name>sunxin</name>
<sex>women</sex>
<age>18</age>
</stu>
<stu>
<name>wanghuilin</name>
<sex>men</sex>
<age>20</age>
</stu>
</st>

 第一个,用DOM读取

<?php 


$xml = new DoMDocument();

$xml->load("xml.xml");
$root = $xml->getElementsByTagName("st")->item(0);

$stu = $xml->createElement("stu");
//name
$stu_name = $xml->createElement("name");
$stu_name->nodeValue = "wanghuilin";
//sex
$stu_sex = $xml->createElement("sex");
$stu_sex->nodeValue = "men";
//age
$stu_age = $xml->createElement("age");
$stu_age->nodeValue = "20";
$stu->appendChild($stu_name);
$stu->appendChild($stu_sex);
$stu->appendChild($stu_age);
$root->appendChild($stu);

$xml->save("xml.xml");

?>

 第二章用simpxml

<?
 $xml = simplexml_load_file('xml.xml');
 
$name = $xml->stu;
echo $name[0]->name;

?>

 

posted on 2015-03-26 21:13  手撕高达的村长  阅读(176)  评论(0编辑  收藏  举报

导航