解析器错误:字符串未开始期待 '或“在 PHP 中

发布于 2024-10-17 18:32:19 字数 5040 浏览 4 评论 0原文

将 XML 文件转换为关联数组,

$xmlUrl = '../products.xml';
$xmlStr = file_get_contents($xmlUrl); 
$xmlObj = simplexml_load_string($xmlStr);  print_r ($xmlObj);exit;
$arrXml = objectsIntoArray($xmlObj);

您好,我正在尝试使用以下代码和包含以下内容的 Product.xml

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <sku>p750h3</sku>
    <category>Plans: Vodafone Unlimited Cap</category>
    <price>$0</price>
    <totalmonthlycost>$129</totalmonthlycost>
    <totalmincost>$3096</totalmincost>
    <upfront>$0</upfront>
    <imageurl>http://store.vodafone.com.au/Images/Upload/nokia-6260-slide-front_118x307.png</imageurl>
    <threedurl>http://store.vodafone.com.au/handset-nokia-6260-slide.aspx#3d</threedurl>
    <smallimageurl>http://store.vodafone.com.au/Images/Upload/nokia-6260-slide-front_23x60.png</smallimageurl>
    <name>Nokia 6260 Slide $129 Unlimited Cap - 24 Months</name>
    <description></description>
    <ctppage>http://store.vodafone.com.au/handset-nokia-6260-slide.aspx</ctppage>
    <features>
      <![CDATA[
                Exclusive to Vodafone, this advanced all &ndash; in - one device has advanced web and navigation features plus a handy 360 degree Navi key to help you stay in control.<br/><ul>
        <li>5 MP camera with Carl Zeiss optics and Flash</li>
        <li>WiFi, HSDPA and HSUPA</li>
        <li>Integrated GPS Navigation</li>
        <li>3G (<a href="/whatis3g-popup.aspx" onclick="window.open(this.href,'','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=600,height=590,status'); return false"><u>What's this?</u></a>)</li>
        </ul>
            ]]>
    </features>
    <available>Yes</available>
    <shippingcost>$0.0</shippingcost>
    <dimensions></dimensions>
    <manufacturer>Nokia</manufacturer>
    <modelnumber>6260 Slide</modelnumber>
    <currency>AUD</currency>
    <devicekeypoints>&lt;ul&gt;

      &lt;li&gt;5 MP camera with Carl Zeiss optics and Flash&lt;/li&gt;
      &lt;li&gt;WiFi, HSDPA and HSUPA&lt;/li&gt;
      &lt;li&gt;Integrated GPS Navigation&lt;/li&gt;
      &lt;li&gt;3G (&lt;a href="/whatis3g-popup.aspx" onclick="window.open(this.href,'','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=600,height=590,status'); return false"&gt;&lt;u&gt;What's this?&lt;/u&gt;&lt;/a&gt;)&lt;/li&gt;

      &lt;/ul&gt;</devicekeypoints>
    <deviceTagline></deviceTagline>
    <deviceColor>Black</deviceColor>
    <deviceSpecialOffers></deviceSpecialOffers>
    <deviceMonthlyHandsetCost>0.0</deviceMonthlyHandsetCost>
    <deviceRecommendedPlan>$129 Unlimited Cap - 24 Months</deviceRecommendedPlan>
    <deviceOverallRating></deviceOverallRating>
    <plan>
      <term>24</term>
      <monthlyCapCost>$129</monthlyCapCost>
      <getMonthly>
        <![CDATA[Monthly credit amount - UNLIMITED<br/>Monthly data - 4GB<sup>3</sup><br/>Vodafone to Vodafone Calls - UNLIMITED<br/>Also includes - Voicemail retrieval <br/><br/>Flexi credit amount - NA<br/>Standard national voice calls - UNLIMITED<br/>Standard txt and pxt - UNLIMITED<br/>]]>
      </getMonthly>
    </plan>
  </product>...etc

但它返回一条错误消息,如下所示

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : String not started expecting ' or " in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=\"1.0\" encoding=\"utf-8\"?> in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Malformed declaration expecting version in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=\"1.0\" encoding=\"utf-8\"?> in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24......etc

我一整天都在尝试解决此问题并通过网络搜索解决方案,但我无法得到 。请告诉我原因?

Hi I'm trying to convert the XML file into associative array by using the following code

$xmlUrl = '../products.xml';
$xmlStr = file_get_contents($xmlUrl); 
$xmlObj = simplexml_load_string($xmlStr);  print_r ($xmlObj);exit;
$arrXml = objectsIntoArray($xmlObj);

and product.xml containing

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <sku>p750h3</sku>
    <category>Plans: Vodafone Unlimited Cap</category>
    <price>$0</price>
    <totalmonthlycost>$129</totalmonthlycost>
    <totalmincost>$3096</totalmincost>
    <upfront>$0</upfront>
    <imageurl>http://store.vodafone.com.au/Images/Upload/nokia-6260-slide-front_118x307.png</imageurl>
    <threedurl>http://store.vodafone.com.au/handset-nokia-6260-slide.aspx#3d</threedurl>
    <smallimageurl>http://store.vodafone.com.au/Images/Upload/nokia-6260-slide-front_23x60.png</smallimageurl>
    <name>Nokia 6260 Slide $129 Unlimited Cap - 24 Months</name>
    <description></description>
    <ctppage>http://store.vodafone.com.au/handset-nokia-6260-slide.aspx</ctppage>
    <features>
      <![CDATA[
                Exclusive to Vodafone, this advanced all – in - one device has advanced web and navigation features plus a handy 360 degree Navi key to help you stay in control.<br/><ul>
        <li>5 MP camera with Carl Zeiss optics and Flash</li>
        <li>WiFi, HSDPA and HSUPA</li>
        <li>Integrated GPS Navigation</li>
        <li>3G (<a href="/whatis3g-popup.aspx" onclick="window.open(this.href,'','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=600,height=590,status'); return false"><u>What's this?</u></a>)</li>
        </ul>
            ]]>
    </features>
    <available>Yes</available>
    <shippingcost>$0.0</shippingcost>
    <dimensions></dimensions>
    <manufacturer>Nokia</manufacturer>
    <modelnumber>6260 Slide</modelnumber>
    <currency>AUD</currency>
    <devicekeypoints><ul>

      <li>5 MP camera with Carl Zeiss optics and Flash</li>
      <li>WiFi, HSDPA and HSUPA</li>
      <li>Integrated GPS Navigation</li>
      <li>3G (<a href="/whatis3g-popup.aspx" onclick="window.open(this.href,'','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=600,height=590,status'); return false"><u>What's this?</u></a>)</li>

      </ul></devicekeypoints>
    <deviceTagline></deviceTagline>
    <deviceColor>Black</deviceColor>
    <deviceSpecialOffers></deviceSpecialOffers>
    <deviceMonthlyHandsetCost>0.0</deviceMonthlyHandsetCost>
    <deviceRecommendedPlan>$129 Unlimited Cap - 24 Months</deviceRecommendedPlan>
    <deviceOverallRating></deviceOverallRating>
    <plan>
      <term>24</term>
      <monthlyCapCost>$129</monthlyCapCost>
      <getMonthly>
        <![CDATA[Monthly credit amount - UNLIMITED<br/>Monthly data - 4GB<sup>3</sup><br/>Vodafone to Vodafone Calls - UNLIMITED<br/>Also includes - Voicemail retrieval <br/><br/>Flexi credit amount - NA<br/>Standard national voice calls - UNLIMITED<br/>Standard txt and pxt - UNLIMITED<br/>]]>
      </getMonthly>
    </plan>
  </product>...etc

but it returns a error message as follows

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : String not started expecting ' or " in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=\"1.0\" encoding=\"utf-8\"?> in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Malformed declaration expecting version in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=\"1.0\" encoding=\"utf-8\"?> in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\EBU\xampp\htdocs\biglinks\include\productUpdate.php on line 24......etc

I am trying to fix this issue whole day and searching solution over the net but i could not get . Please let me know the reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丘比特射中我 2024-10-24 18:32:20

这可能是由于 magic_quotes_runtime 在调用 file_get_contents 时添加反斜杠造成的。尝试以下操作:

$xmlUrl = '../products.xml';
$xmlStr = file_get_contents($xmlUrl); 
if (get_magic_quotes_runtime())
{
    $xmlStr = stripslashes($xmlStr);
}
$xmlObj = simplexml_load_string($xmlStr);  print_r ($xmlObj);exit;
$arrXml = objectsIntoArray($xmlObj);    $xmlStr = file_get_contents($xmlUrl);

或者,您可以通过 .htaccess 在 PHP 配置中禁用 magic_quotes_runtime (尽管这可能会对脚本中的其他位置产生影响),或者在脚本顶部附近添加以下内容:

set_magic_quotes_runtime(false);

This is probably caused by magic_quotes_runtime adding backslashes when you call file_get_contents. Try the following:

$xmlUrl = '../products.xml';
$xmlStr = file_get_contents($xmlUrl); 
if (get_magic_quotes_runtime())
{
    $xmlStr = stripslashes($xmlStr);
}
$xmlObj = simplexml_load_string($xmlStr);  print_r ($xmlObj);exit;
$arrXml = objectsIntoArray($xmlObj);    $xmlStr = file_get_contents($xmlUrl);

Alternatively you could disable magic_quotes_runtime (although this may have effects elsewhere in your script) in your PHP configuration, via .htaccess, or by adding the following near the top of your script:

set_magic_quotes_runtime(false);
柒夜笙歌凉 2024-10-24 18:32:20

只是出于好奇,你为什么不使用 simplexml_load_file()< /a>?

Just out of curiosity, why aren't you using simplexml_load_file()?

夏末染殇 2024-10-24 18:32:20

您应该修复您的 XML 字符串,它无效。
如果您想保持 XML 不变并避免验证错误,请使用 simplexml_load_string() 的第三个参数来设置正确的选项,请参阅 http://php.net/manual/fr/function.simplexml-load-string.phphttp://www.php.net/manual/fr/libxml.constants.php

顺便说一句,我不明白为什么显示带有转义引号的 XML,它是否在源文件中转义了?

You should fix your XML string, it's not valid.
If you want to keep the XML as is and avoid validation errors, use the 3rd argument of simplexml_load_string() to set the right options, see http://php.net/manual/fr/function.simplexml-load-string.php and http://www.php.net/manual/fr/libxml.constants.php

Btw I don't understand why it shows your XML with escaped quotes, is it escaped in the source file?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文