SimpleXMLElement file does not load xmlString

2019-03-05 21:05发布

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条回答
Juvenile、少年°
2楼-- · 2019-03-05 21:26

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
查看更多
登录 后发表回答