SimpleXMLElement file does not load xmlString

天涯浪子 提交于 2019-12-02 15:52:47

问题


I am trying to work on an XML Based problem. So far, it's proving to be a bummer for some reason. My code is only two lines and for some reason nothing is loading in.

The code:

$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')

The functions do not load, and this I can tell by var_dumping $xmlObject, which returns boolean false.

file_get_contents does load the string $xmlString and var_dumping it returns the string, so location of the file path is not an issue here.

Anybody know what's happening here?

Thanks


回答1:


Your XML is probably invalid. Look at the fine PHP manual that describes how to handle those errors:

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

You can also validate your XML with command line tools like xmllint:

$ xmllint --noout /path/to/file.xml
file.xml:1: parser error : XML declaration allowed only at the start of the document


来源:https://stackoverflow.com/questions/10927399/simplexmlelement-file-does-not-load-xmlstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!