Home > Backend Development > PHP Tutorial > PHP xml file operation code (1)_PHP tutorial

PHP xml file operation code (1)_PHP tutorial

WBOY
Release: 2016-07-21 15:47:04
Original
733 people have browsed it

example.xml文件:

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //创建SimpleXML对象
print_r($xml); //输出XML
?>

复制代码 代码如下:




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





复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //创建SimpleXML对象
var_dump($xml); //输出XML
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //读取XML文件
foreach($xml->depart as $a) //循环读取XML数据中的每一个depart标签
{
echo "$a->name
"; //输出其中的name属性
}
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //读取XML文件
echo $xml->depart->name[0]; //输出节点
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml');
foreach ($xml->depart->children() as $depart) //循环读取depart标签下的子标签
{
var_dump($depart); //输出标签的XML数据
}
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //读取XML文件
$result = $xml->xpath('/departs/depart/employees/employee/name'); //定义节点
var_dump($result); //输出节点
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //读取XML
$xml->depart->name[0] = "Human Resource"; //修改节点
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //读取XML数据
echo $xml->asXML(); //标准化XML数据
?>

复制代码 代码如下:

$xml = simplexml_load_file('example.xml'); //Read XML data
$newxml = $xml->asXML(); //Standardize XML Data
$fp = fopen("newxml.xml", "w"); //Open the file to write XML data
fwrite($fp, $newxml); //Write XML data
fclose($fp); //Close the file
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320041.htmlTechArticleexample.xml file: Copy the code as follows: ?php $xml = simplexml_load_file('example.xml'); //Create SimpleXML object print_r($xml); //Output XML ? Copy the code as follows: ?xml...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template