PHP 使用PHP解析XML文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 使用PHP解析XML文件相关的知识,希望对你有一定的参考价值。

<?php
// this is a sample xml string
$xml_string="<?xml version='1.0'?>
<text>
    <article id='Article1'>
        <title>Title 1</title>
        <content>..text here..</content>
    </article>
    <article id='Article2'>
        <title>Title 2</title>
        <content>..text here..</content>
    </article>
</text>";
 
// load this xml string using simplexml function
$xml = simplexml_load_string($xml_string);
 
// loop through the each node
foreach($xml->article as $key){
   // attribute are accessted by
   echo $key['id'].' ';
   // node are accessted by -> operator
   echo $key->title.' ';
   echo $key->content.'<br />';
}
?>

以上是关于PHP 使用PHP解析XML文件的主要内容,如果未能解决你的问题,请参考以下文章