Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用simplexml\u加载文件加载文件_Php_Simplexml - Fatal编程技术网

Php 使用simplexml\u加载文件加载文件

Php 使用simplexml\u加载文件加载文件,php,simplexml,Php,Simplexml,我不明白为什么这不起作用,但我可以回显test.xml <?php $xml = simplexml_load_file('test.xml'); $movies = new SimpleXMLElement($xml); echo $movies->movie[1]->plot; ?> 当您要加载XML数据时,有两种方法。您可以将XML文件的内容作为字符串加载,然后将该字符串传递给简单XML: $fileContents = file_get_contents(

我不明白为什么这不起作用,但我可以回显test.xml

<?php
$xml = simplexml_load_file('test.xml');

$movies = new SimpleXMLElement($xml);

echo $movies->movie[1]->plot;
?> 

当您要加载XML数据时,有两种方法。您可以将XML文件的内容作为字符串加载,然后将该字符串传递给简单XML:

$fileContents = file_get_contents('test.xml'); # reads the file and returns the string

$xml = simplexml_load_string($fileContents); # creates a Simple XML object from a string

print_r($xml); # output is a Simple XML object
…或者,直接将文件加载到简单的XML对象中:

$xml = simplexml_load_file('test.xml'); # Instantiates a new Simple XML object from the file, without you having to open and pass the string yourself

print_r($xml); # output is a Simple XML object
参考资料:


无需同时执行这两项操作,
simplexml\u加载文件
并创建一个新的
simplexml
对象。

已将XML文件解释为对象。(请记住,它不接受XML字符串)

或者,您可以直接将XML字符串加载到对象中

上述任一方法均可用于执行以下操作:

echo $movies->movie[0]->plot;

如果可以打印text.xml,则需要加载字符串,而不是文件。为什么$data=file_get_contents(')错误;这是正确的。我会给你一个比评论更好的回答。
$movies = new SimpleXMLElement(file_get_contents('test.xml'));
echo $movies->movie[0]->plot;