Home > Backend Development > PHP Tutorial > php读取xml中某个元素的内容(PHP5以上才支持)

php读取xml中某个元素的内容(PHP5以上才支持)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-19 14:05:31
Original
933 people have browsed it

假设xml文件名为class.xml,内容如下:



 
    3
    张三
 

 
    5
    李四
 

1、载入xml文件并生成一个simpleXml对象

//假设xml文件在当前路径

$xml = simplexml_load_file(’class.xml’);

2、根据元素的名字,一层一层找到那个元素,并获取元素内容

//假设获取的是第二个student中的子元素中,第一个name的内容,也就是输出李四。注意元素的索引从0开始

echo $xml->student[1]->name[0];

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