Maison > php教程 > php手册 > le corps du texte

php中simplexml_load_file函数用法实例

WBOY
Libérer: 2016-06-06 20:17:48
original
1529 Les gens l'ont consulté

这篇文章主要介绍了php中simplexml_load_file函数用法,以实例形式详细的讲述了simplexml_load_file函数读取XML文件的具体方法,非常具有实用价值,需要的朋友可以

本文实例讲述了php中simplexml_load_file函数用法。分享给大家供大家参考。具体用法分析如下:

在php中simplexml_load_file() 函数把 XML 文档载入对象中之后我们就可以利用由此函数返回的对象进行相关的操作了,下面我们看几个测试实例.

例子,XML文件代码如下:

复制代码 代码如下:

 

George
John
Reminder
Don't forget the meeting!


PHP 代码如下:

复制代码 代码如下:

if (file_exists('test.xml')) 

  $xml = simplexml_load_file('test.xml'); 
  var_dump($xml); 

else 

  exit('Error.'); 

?>


 
运行输出结果如下: 

复制代码 代码如下:


object(SimpleXMLElement)#1 (4) {
  ["to"]=>
  string(6) "George"
  ["from"]=>
  string(4) "John"
  ["heading"]=>
  string(8) "Reminder"
  ["body"]=>
  string(25) "Don't forget the meeting!"
}


假如有一个“iciba.xml”文件,其内容如下:

复制代码 代码如下:

 
 
 天空 
  
 Array;Array; 
  
  The church tower stood against the sky like a finger pointing towards heaven. 
  教堂的尖塔在天空的映衬下宛如指向天空的手指。 
 
 
  
  A balloon floated across the sky. 
  气球飘过天空。 
 
 
  
  A bolt of lightning lit up the sky. 
  (一道)闪电照亮了天空。 
 
 
  
  A bright moving object appeared in the sky at sunset. 
  日落西山时,天空出现了一个移动的发亮物体。 
 
 
  
  A bright rainbow arched above. 
  一弯明亮的彩虹悬挂在天空。 
 
 


在PHP语言中我们可以用以下方法取得我们想要的值: 

复制代码 代码如下:

$xmldata = simplexml_load_file("iciba.xml"); 
 
header("Content-Type: text/html; charset=UTF-8"); 
print_r($xmldata); //第一部分 
 
$listcount = count($xmldata->sent); 
 
for($i=0;$i  $dictlist = $xmldata->sent[$i]; 
 echo "
例句:".$dictlist->orig; 
 echo "
翻译:".$dictlist->trans; 

?>

“第一部分”将输出: 

复制代码 代码如下:


SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [num] => 219
            [id] => 219
            [name] => 219
        )

    [key] => 天空
    [pos] => SimpleXMLElement Object
        (
        )

    [acceptation] => Array;Array;
    [sent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [orig] => The church tower stood against the sky like a finger pointing towards heaven.
                    [trans] => 教堂的尖塔在天空的映衬下宛如指向天空的手指。
                )

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!