2011-09-29 26 views
67

我在使用simplexml_load_file在php中閱讀xml。但是嘗試加載它顯示警告'xmlParseEntityRef:no name'在將xml加載到php文件時出現警告

Warning: simplexml_load_file() [function.simplexml-load-file]: <project orderno="6" campaign_name="International Relief & Development" project in /home/bluecard1/public_html/test.php on line 3  
Warning: simplexml_load_file() [function.simplexml-load-file]:^in /home/bluecard1/public_html/test.php on line 3  
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3 

Warning: simplexml_load_file() [function.simplexml-load-file]: ional Relief & Development" project_id="313" client_name="International Relief & in /home/bluecard1/public_html/test.php on line 3  
Warning: simplexml_load_file() [function.simplexml-load-file]:^in /home/bluecard1/public_html/test.php on line 3  
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3 

列表的XML我如何糾正消除這些警告?

(XML從URL http://..../index.php/site/projects &加載到test.php的變量產生的。我沒有寫權限來的index.php)

+0

的XML無效。您可能無法加載它。錯誤可以通過在'simplexml_load_file'前面添加'@'或者通過添加一個標誌來抑制,參見'simplexml_load_file'的手冊頁以獲得更多信息,並且請刪除你的問題,這是重複的。 – hakre

+0

我可以看到,我的回答得到了很多關注,如果這實際上是解決方案:您能否將其標記爲「正確答案」?謝謝。 – ricricucit

回答

95

的XML是最有可能無效。

的問題可能是「&」

$text=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $text); 

將擺脫的「&」,並與它的HTML代碼版本來替換它......試試看。

+1

謝謝。你救了我的一天! – Saim

+0

使用XML的最佳做法是確保沒有衝突的字符,並且你應該在parsin之前替換它們 –

+0

謝謝,這個問題的主要觀點是因爲xml無效 – yussan

5

XML無效。

<![CDATA[ 
{INVALID XML} 
]]> 

CDATA應圍繞所有特殊的XML字符來包裹,按W3C

54

發現這個here ...

問題: XML解析器將返回錯誤「xmlParseEntityRef:NONAME」

原因:有一個流浪'&'(和號字符)在XML文本中的某處。一些文字&一些文字

解決方案:

  • 解決方案1:刪除符號。
  • 解決方案2:編碼和號(即將'&'字符替換爲'& amp;')。記得在閱讀XML 文本時解碼。
  • 解決方案3:使用CDATA節(解析器將忽略CDATA節中的文本)。 !< [CDATA [一些文本&一些 文]]>

注:「&「<「>「都會產生問題,如果處理不正確。

+6

今天救了我。 – Bwire

+0

我們知道這是爲什麼嗎?另外,CDATA部分是否仍然會被能夠呈現某些數據的瀏覽器拾取?我的XML標籤中有一些HTML標籤,我需要將它們呈現給最終用戶以供編輯工具使用。 – skeletalbassman

+0

@skeletalbassman原因是答案的一部分。 –

5

我使用組合的版本:

strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&amp;",$textorhtml)) 
+1

這是一個完美的工作。你只是缺少結束右括號 – myh34d

9

嘗試自行清潔HTML首先使用此功能:

$html = htmlspecialchars($html); 

特殊字符通常是在HTML代表的不同,它可能是編譯器混淆。像&變成&amp;

+0

有人可以解釋爲什麼這是downvoted? 'htmlspecialchars()'是在元素數據中精確地將'&,「,<, >'字符轉換成的函數。 – jacobross85

+0

完全適合我... –

+0

,因爲解釋不清楚並且很容易閱讀 –

0

這解決了我的problème:

$description = strip_tags($value['Description']); 
$description=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $description); 
$description= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $description); 
$description=str_replace(' & ', ' &amp; ', html_entity_decode((htmlspecialchars_decode($description)))); 
4

問題

  • PHP函數simplexml_load_file拋出分析錯誤parser error : xmlParseEntityRef嘗試從一個URL加載XML文件。由URL返回

原因

  • XML是不是一個有效的XML。它包含&值 而不是&amp;。在這個時候很可能還有其他一些不明顯的錯誤。

THINGS超出我們的控制

  • 理想的情況下,我們應該確保有效的XML被送入PHP simplexml_load_file功能,但看起來我們沒有任何控制如何創建XML。
  • 它也不可能強迫simplexml_load_file來處理 無效的XML文件。它不會給我們留下很多選擇,除了 固定XML文件本身。

可能的解決方案

轉換XML無效到有效的XML。它可以通過PHP tidy extension來完成。進一步的指令可以從http://php.net/manual/en/book.tidy.php

發現一旦你確信該擴展存在,或安裝,請執行下列操作。

/** 
* As per the question asked, the URL is loaded into a variable first, 
* which we can assume to be $xml 
*/ 
$xml = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<project orderno="6" campaign_name="International Relief & Development for under developed nations"> 
    <invalid-data>Some other data containing & in it</invalid-data> 
    <unclosed-tag> 
</project> 
XML; 

/** 
* Whenever we use tidy it is best to pass some configuration options 
* similar to $tidyConfig. In this particular case we are making sure that 
* tidy understands that our input and output is XML. 
*/ 
$tidyConfig = array (
    'indent' => true, 
    'input-xml' => true, 
    'output-xml' => true, 
    'wrap' => 200 
); 

/** 
* Now we can use tidy to parse the string and then repair it. 
*/ 
$tidy = new tidy; 
$tidy->parseString($xml, $tidyConfig, 'utf8'); 
$tidy->cleanRepair(); 

/** 
* If we try to output the repaired XML string by echoing $tidy it should look like. 

<?xml version="1.0" encoding="utf-8"?> 
<project orderno="6" campaign_name="International Relief &amp; Development for under developed nations"> 
     <invalid-data>Some other data containing &amp; in it</invalid-data> 
     <unclosed-tag></unclosed-tag> 
</project> 

* As you can see that & is now fixed in campaign_name attribute 
* and also with-in invalid-data element. You can also see that the 
* <unclosed-tag> which didn't had a close tag, has been fixed too. 
*/ 
echo $tidy; 

/** 
* Now when we try to use simplexml_load_string to load the clean XML. When we 
* try to print_r it should look something like below. 

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [orderno] => 6 
      [campaign_name] => International Relief & Development for under developed nations 
     ) 

    [invalid-data] => Some other data containing & in it 
    [unclosed-tag] => SimpleXMLElement Object 
     (
     ) 

) 

*/ 
$simpleXmlElement = simplexml_load_string($tidy); 
print_r($simpleXmlElement); 

注意

開發商應儘量無效的XML與有效的XML(由整齊產生)相比較,看有沒有不良副作用,使用後整齊。 Tidy做得非常出色,但它從視覺上看並不會很痛苦,並且100%確定。在我們的例子中,它應該像比較$ xml和$ tidy一樣簡單。

0

如果你正在就此問題與Opencart的嘗試編輯

目錄/控制器/擴展/飼料/ google_sitemap.php 欲瞭解更多信息,以及如何做到這一點請參閱本:xmlparseentityref-no-name-error