2014-01-07 69 views
0

我有一個問題,當我嘗試讀取與PHP函數使用simplexml_load_file一個XML文檔()這是警告我得到PHP:SimpleXML的警告分析器錯誤:AttValue:「或」預期

Warning: simplexml_load_file(): URL:81: parser error : AttValue: " or ' expected in URL on line 15 
Warning: simplexml_load_file(): <img src=elite.png><br> in URL on line 15 
Warning: simplexml_load_file():^in URL on line 15 
Warning: simplexml_load_file(): URL:81: parser error : attributes construct error in URL on line 15 
Warning: simplexml_load_file(): <img src=elite.png><br> in URL line 15 
Warning: simplexml_load_file():^in URL on line 15 
Warning: simplexml_load_file(): URL:81: parser error : Couldn't find end of Start Tag img >line 81 in URL on line 15 
Warning: simplexml_load_file(): <img src=elite.png><br> in URL on line 15 
Warning: simplexml_load_file():^in URL on line 15 
Warning: simplexml_load_file(): URL:82: parser error : Opening and ending tag mismatch: br line 81 and programbeskrivelse in URL on line 15 
Warning: simplexml_load_file(): er vurderet og rådgivet af de bedste affiliates i branchen.</programbeskrivelse in URL on line 15 
Warning: simplexml_load_file():^in URL on line 15 
Warning: simplexml_load_file(): URL:91: parser error : Opening and ending tag mismatch: br line 80 and program in URL on line 15 
Warning: simplexml_load_file(): </program> in URL on line 15 
Warning: simplexml_load_file():^in URL on line 15 

由於!你看,我認爲這是在XML文件中的<img src=elite.png>標籤,它的問題是:

<program><programid>ID</programid> 
<programnavn>NAME</programnavn> 
<programurl>URL</programurl> 
<programbeskrivelse>SOME TEXT 
<br> 
<img src=elite.png><br> 
SOME MORE TEXT</programbeskrivelse> 
<programbetingelser></programbetingelser> 
<kategoriid>5</kategoriid> 
<kategorinavn>Tøj, mode, livsstil o.l.</kategorinavn> 
<feed>ja</feed> 
<kliksats>0.00</kliksats> 
<leadsats>0.00</leadsats> 
<provision>10.00</provision> 
<affiliatelink>N/A</affiliatelink> 
</program> 

但它是一個外部XML文件,所以我不能只是讓XML文件中的變化有什麼。如何忽略標籤,在使用SimpleXML之前讓我出來?還是有其他方法可以做到嗎?

注:我的所有URL都rewriten與URL

我曾嘗試使用路徑上stripslashes()函數,而不是那的問題!

回答

1

我想出了基於Chrashspeeders意見的解決方案:

$remove = file_get_contents(stripslashes("URL")); 

$remove = preg_replace("/<programbeskrivelse>[\d\D]*?<\/provision>/", "", $remove); 

$remove = str_replace(array("&amp;", "&"), array("&", "&amp;"), $remove); 

$xml = simplexml_load_string($remove); 

我不知道性能什麼,但它的工作原理。

$remove = str_replace(array("&amp;", "&"), array("&", "&amp;"), $remove); 

是針對鏈接中的&字符。

0

問題是XML格式錯誤。如果您要修復錯誤(將引號添加到屬性值),那麼在讀取<programbeskrivelse>節點的值時會導致其他問題。 SimpleXML將忽略字符串內的標籤,使值爲SOME TEXT。該節點的值應該很可能在CDATA內。您可能能夠檢測字符串內的標籤並將節點值包裝在CDATA中,但這可能會非常棘手。 Offhand我想不出一個快速的解決方案。

+0

我唯一感興趣的價值觀,是 所以我不需要價值 – mschadegg

+0

您可以用[file_get_contents()函數(HTTP加載XML文件的內容: //us1.php.net/manual/en/function.file-get-contents.php)並刪除那些你不關心的值的節點,然後使用[simplexml_load_string()](http: //us1.php.net/manual/en/function.simplexml-load-string.php)。只要確保你保留根節點(在這種情況下,''和'')。 – Crashspeeder

+0

當我有超過7000行的XML文件時,性能會不會是無效的? – mschadegg