Home  >  Article  >  Backend Development  >  php如何读取xml文件

php如何读取xml文件

PHPz
PHPzOriginal
2016-06-02 11:29:172550browse

php如何读取xml文件

php读取xml文件的方法:

假设myxml.xml文件中的内容如下

    <resource>
        <res>this is value</res>
        <sound>sound.ogg</sound>
    </resource>

首先我们读取这个xml文件的内容

$xml = file_get_contents(&#39;myxml.xml&#39;);

最重要的一步:解析这个xml文件

$xml_obj = simplexml_load_string($xml);

好了,我们现在有了$xml_obj,我们就可以按照xml文件的结构读取数据了:

//输出 "this is value"
echo $xml_obj->res; 
//输出 "sound.ogg"
echo $xml_obj->sound;

想了解更多编程学习,敬请关注php培训栏目!

Statement:
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