您当前的位置: 首页 > 学无止境 > XML与XSLT 网站首页XML与XSLT
12.用simplexml处理xml
发布时间:2017-04-30 12:54:59编辑:雪饮阅读()
php处理xml最简单便捷的一种方式
php:
<?php
//加载xml文件
$sim=simplexml_load_file("./08.xml");
//$sim将是一个对象,该对象包含整个dom结构,使用对象的方式来操作则会更加灵活
//echo "<pre>";
//print_r($sim);
//echo "</pre>";
//获取节点值
//echo "<h1>age</h1>";
//echo $sim->student->age;
//echo "<h1>attribute</h1>";
//获取属性值
$attributes=$sim->student->name->attributes();
//echo $attributes["a"];
//添加元素
$sim->student->addChild("sex","nan");
//添加属性
$sim->student->addAttribute("nikename","xy");
//输出xml(设置header时之前的所有输出echo语句都要关闭)
header("content-type:text/xml;charset=utf-8");
echo $sim->asXML();
?>
xml:
<?xml version="1.0" encoding="utf-8"?>
<students>
<student>
<name a="b">tom</name>
<age>23</age>
<school>xx</school>
</student>
</students>
关键字词:php,simplexml,xml
上一篇:11.dtd的实体使用
下一篇:xml专题