php xml文件操作代码(一)

前端之家收集整理的这篇文章主要介绍了php xml文件操作代码(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

example.xml文件
<div class="codetitle"><a style="CURSOR: pointer" data="18540" class="copybut" id="copybut18540" onclick="doCopy('code18540')"> 代码如下:

<div class="codebody" id="code18540">
<?PHP
$xml = simplexml_load_file('example.xml'); //创建SimpleXML对象
print_r($xml); //输出XML
?>

<div class="codetitle"><a style="CURSOR: pointer" data="10254" class="copybut" id="copybut10254" onclick="doCopy('code10254')"> 代码如下:
<div class="codebody" id="code10254">
<?xml version='1.0'?>


production support


100001
Simon
24
1982-11-06
5000.00
1000.00


100002
Elaine
24
1982-01-01
6000.00
2000.00




testing center


110001
Helen
23
1983-07-21
5000.00
1000.00





<div class="codetitle"><a style="CURSOR: pointer" data="88228" class="copybut" id="copybut88228" onclick="doCopy('code88228')"> 代码如下:
<div class="codebody" id="code88228">
<?PHP
$xml = simplexml_load_file('example.xml'); //创建SimpleXML对象
var_dump($xml); //输出XML
?>

<div class="codetitle"><a style="CURSOR: pointer" data="41843" class="copybut" id="copybut41843" onclick="doCopy('code41843')"> 代码如下:
<div class="codebody" id="code41843">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML文件
foreach($xml->depart as $a) //循环读取XML数据中的每一个depart标签
{
echo "$a->name
"; //输出其中的name属性
}
?>

<div class="codetitle"><a style="CURSOR: pointer" data="23281" class="copybut" id="copybut23281" onclick="doCopy('code23281')"> 代码如下:<div class="codebody" id="code23281">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML文件
echo $xml->depart->name[0]; //输出节点
?>

<div class="codetitle"><a style="CURSOR: pointer" data="5527" class="copybut" id="copybut5527" onclick="doCopy('code5527')"> 代码如下:<div class="codebody" id="code5527">
<?PHP
$xml = simplexml_load_file('example.xml');
foreach ($xml->depart->children() as $depart) //循环读取depart标签下的子标签
{
var_dump($depart); //输出标签的XML数据
}
?>

<div class="codetitle"><a style="CURSOR: pointer" data="32230" class="copybut" id="copybut32230" onclick="doCopy('code32230')"> 代码如下:<div class="codebody" id="code32230">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML文件
$result = $xml->xpath('/departs/depart/employees/employee/name'); //定义节点
var_dump($result); //输出节点
?>

<div class="codetitle"><a style="CURSOR: pointer" data="52120" class="copybut" id="copybut52120" onclick="doCopy('code52120')"> 代码如下:<div class="codebody" id="code52120">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML
$xml->depart->name[0] = "Human Resource"; //修改节点
?>

<div class="codetitle"><a style="CURSOR: pointer" data="19008" class="copybut" id="copybut19008" onclick="doCopy('code19008')"> 代码如下:<div class="codebody" id="code19008">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML数据
echo $xml->asXML(); //标准化XML数据
?>

<div class="codetitle"><a style="CURSOR: pointer" data="18344" class="copybut" id="copybut18344" onclick="doCopy('code18344')"> 代码如下:<div class="codebody" id="code18344">
<?PHP
$xml = simplexml_load_file('example.xml'); //读取XML数据
$newxml = $xml->asXML(); //标准化XML数据
$fp = fopen("newxml.xml","w"); //打开要写入XML数据的文件
fwrite($fp,$newxml); //写入XML数据
fclose($fp); //关闭文件
?>

原文链接:https://www.f2er.com/php/29267.html

猜你在找的PHP相关文章