Php 无法获取新的SimpleXMLElement来处理文件
我正在使用以下代码:Php 无法获取新的SimpleXMLElement来处理文件,php,xml,simplexml,Php,Xml,Simplexml,我正在使用以下代码: $str_xml = file_get_contents('pricetest1.xml'); $library = New SimpleXMLElement('srt_xml'); print_r $library; $str_xml应该稍后从URL加载,放置的xml只是一个示例 XML如下所示: <?xml version="1.0" encoding="iso-8859-1"?> <products> <product
$str_xml = file_get_contents('pricetest1.xml');
$library = New SimpleXMLElement('srt_xml');
print_r $library;
$str_xml
应该稍后从URL加载,放置的xml只是一个示例
XML如下所示:
<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<product id="809809">
<name>LongJohn</name>
<brand>7</brand>
<product_url>https://www.example.com/producturl.html</product_url>
<image_url>https://www.example.com/product.jpg</image_url>
<price>369.00</price>
<former_price>369.00</former_price>
<in>Y</in>
<sum>110297</sum>
</product>
</products>
</xml>
朗约翰
7.
https://www.example.com/producturl.html
https://www.example.com/product.jpg
369
369
Y
110297
我真的不知道如何从中获得任何错误,因为这只是使页面无法加载到我的浏览器中
我想可能是XML在ISO中,而我的页面在UTF中,但我不确定这是否真的重要,如果我错了,请纠正我
此外,由于每个产品内部都有一个“id”,这是否导致需要处理某种异常
最后,我将通过feed循环并将它们打印到数据库中,但由于我甚至无法从中获得一些错误,我担心我的知识还不够
我可以毫无问题地打印出$str_xml
,以便正确加载文件
我很感激能得到的任何帮助 查看php站点;您为SimpleXMLElement提供了一个根本不是xml的字符串
$str_xml = file_get_contents('pricetest1.xml');
$library = New SimpleXMLElement($str_xml);
print_r $library;
哦,;并且您的xml是无效的 xml无效,对
simplexmlelement
的调用使用的是字符串而不是变量
$str_xml='<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<product id="809809">
<name>LongJohn</name>
<brand>7</brand>
<product_url>https://www.example.com/producturl.html</product_url>
<image_url>https://www.example.com/product.jpg</image_url>
<price>369.00</price>
<former_price>369.00</former_price>
<in>Y</in>
<sum>110297</sum>
</product>
</products>';
$library = new SimpleXMLElement( $str_xml );
print_r( $library );
$str\u xml='
朗约翰
7.
https://www.example.com/producturl.html
https://www.example.com/product.jpg
369
369
Y
110297
';
$library=新的simplexmlement($str_xml);
印刷(图书馆);
尝试删除结束标记
因为它不是有效的XML。