使用SimpleXML从文件和字符串加载xml

1.common.xml
<?xml version="1.0"?>
<books>
<book index="1">
<name year="1892">The Adventrue os </name>
<story>
<title>A scandal in bohmina</title>
<quote>You see,dddd</quote>
</story>

<story>
<title>b</title>
<quote>b1</quote>
</story>

<story>
<title>c</title>
<quote>c1</quote>
</story>
</book>
<book index="2">
<name year="1927">asfaf</name>
<story>
<title>A scandal in bohmina</title>
<quote>You see,dddd</quote>
</story>

<story>
<title>b</title>
<quote>b1</quote>
</story>

<story>
<title>c</title>
<quote>c1</quote>
</story>
</book>
<book index="3">
<name year="1978">The Advenasfrue os </name>
<story>
<title>A scandal in bohmina</title>
<quote>You see,dddd</quote>
</story>

<story>
<title>b</title>
<quote>b1</quote>
</story>

<story>
<title>c</title>
<quote>c1</quote>
</story>
</book>

</books>
2.xml.php
<?php
libxml_use_internal_errors(true);//抑制错误自行处理
$objxml=simplexml_load_file('../common.xml');
if(!$objxml){
     $errors=libxml_get_errors();
     foreach ($errors as $error){
          echo $error->message.'<br/>';
     }
}else{
     foreach ($objxml as $book){
          echo $book->name;
     }
}
posted @ 2014-04-04 17:25  wint  Views(151)  Comments(0)    收藏  举报