从文件加载时使用 SimpleXMLElement
Using SimpleXMLElement when loading from file
我有一个问题让我摸不着头脑,我确信这是一个简单的解决方案,但对于我来说,我找不到它。
我正在尝试使用 PHP 中的 XMLReader 计算 XML 文件的节点数。到目前为止没有什么新鲜事,这里有很多关于如何做到这一点的例子。问题是所有示例都是从表示整个 XML 文件 (simplexml_load_string) 的字符串加载的。那很好用。但是,如果我尝试从文件加载,我会得到 SimpleXML.
的 "Start tag expected, '<' not found" 异常
代码如下:
$xml = simplexml_load_file($fname); //$fname is a valid xml file, it loads fine
$elem = new SimpleXMLElement($xml); //it chokes on this line
虽然可以从字符串中加载它,但我确实想从文件中加载它:
$xml = simplexml_load_string($xmlstring); //$xmlstring is the content of the same xml file
我错过了什么?谢谢你的帮助。
simplexml_load_file 将 XML 文件解释为 xml 对象,因此 $xml 本身就是一个 xml 对象,访问它的元素只需使用 $xml 对象属性,无需创建新对象 $elm
simplexml_load_file returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.
我有一个问题让我摸不着头脑,我确信这是一个简单的解决方案,但对于我来说,我找不到它。
我正在尝试使用 PHP 中的 XMLReader 计算 XML 文件的节点数。到目前为止没有什么新鲜事,这里有很多关于如何做到这一点的例子。问题是所有示例都是从表示整个 XML 文件 (simplexml_load_string) 的字符串加载的。那很好用。但是,如果我尝试从文件加载,我会得到 SimpleXML.
的 "Start tag expected, '<' not found" 异常代码如下:
$xml = simplexml_load_file($fname); //$fname is a valid xml file, it loads fine
$elem = new SimpleXMLElement($xml); //it chokes on this line
虽然可以从字符串中加载它,但我确实想从文件中加载它:
$xml = simplexml_load_string($xmlstring); //$xmlstring is the content of the same xml file
我错过了什么?谢谢你的帮助。
simplexml_load_file 将 XML 文件解释为 xml 对象,因此 $xml 本身就是一个 xml 对象,访问它的元素只需使用 $xml 对象属性,无需创建新对象 $elm
simplexml_load_file returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.