0

我正在尝试解决基于 XML 的问题。到目前为止,由于某种原因,它被证明是一个无赖。我的代码只有两行,由于某种原因没有加载任何内容。

编码:

$xmlString = file_get_contents('file.xml');
$xmlObject = simplexml_load_string($xmlString); 
// also in the above line I have tried
// 1. new SimpleXMLElement('file.xml') 
// 2. simplexml_load_file('file.xml')

函数不加载,我可以通过 var_dumping $xmlObject 来判断,它返回布尔值 false。

file_get_contents 确实加载了字符串 $xmlString 并且 var_dumping 它返回了字符串,因此文件路径的位置在这里不是问题。

有人知道这里发生了什么吗?

谢谢

4

1 回答 1

1

您的 XML 可能无效。查看描述如何处理这些错误的精美 PHP 手册:

http://php.net/manual/en/simplexml.examples-errors.php

您还可以使用 xmllint 等命令行工具验证您的 XML:

$ xmllint --noout /path/to/file.xml
file.xml:1: parser error : XML declaration allowed only at the start of the document
于 2012-06-07T07:57:06.203 回答