Skip to content Skip to sidebar Skip to footer

Simplexml_load_string(): Entity: Line 18: Parser Error : Entity 'nbsp' Not Defined

My error is: simplexml_load_string(): Entity: line 18: parser error : Entity 'nbsp' not defined My problem is that I get this error while reading an xml file from e-commerce site w

Solution 1:

The content of //product[1]/note element is encoded HTML but it appears on the question as plain HTML.

The problem might be in the source of the document.

Downloading it with wget and querying with the mentioned XPath expression. Will give a PHP example also after this.

xmllint --xpath '//product[1]/note' tmp.xml

Result:

<note>&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'&gt;&#xDC;r&#xFC;n &#x130;smi:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style="font-family: &amp;quot;Open Sans&amp;quot;,sans-serif; font-size: 15px; color: #000;"&gt;Ks Games 500 Par&#xE7;a Art Gallery Garden Sea Jin Park Puzzle 11377
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 15px; color: #000;'&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'&gt;&#xDC;r&#xFC;n Hakk&#x131;nda Bilgi:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style="font-family: &amp;quot;Open Sans&amp;quot;,sans-serif; font-size: 15px; color: #000;"&gt;(-)&lt;/span&gt;&lt;/p&gt;</note>

No errors. The content is just a string from XML perspective.

Worth noting that opening the URL on a browser shows the "rendered" content but not the document source

<note><pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'>Ürün İsmi:</span></p><pstyle="margin: 0"><spanstyle="font-family: &quot;Open Sans&quot;,sans-serif; font-size: 15px; color: #000;">Ks Games 500 Parça Art Gallery Garden Sea Jin Park Puzzle 11377 </span></p><pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 15px; color: #000;'>&nbsp;</span></p><pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'>Ürün Hakkında Bilgi:</span></p><pstyle="margin: 0"><spanstyle="font-family: &quot;Open Sans&quot;,sans-serif; font-size: 15px; color: #000;">(-)</span></p></note>

PHP7 parses the document without issues but print_r(), var_dump(), print() also shows rendered content :-(

php7 -r '$xml= simplexml_load_file("tmp.xml"); print($xml->product[0]->note);' 

Result

<pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'>Ürün İsmi:</span></p><pstyle="margin: 0"><spanstyle="font-family: &quot;Open Sans&quot;,sans-serif; font-size: 15px; color: #000;">Ks Games 500 Parça Art Gallery Garden Sea Jin Park Puzzle 11377
</span></p><pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 15px; color: #000;'>&nbsp;</span></p><pstyle="margin: 0"><spanstyle='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'>Ürün Hakkında Bilgi:</span></p><pstyle="margin: 0"><spanstyle="font-family: &quot;Open Sans&quot;,sans-serif; font-size: 15px; color: #000;">(-)</span></p>

Using SimpleXMLElement::asXML would show raw content

php7 -r '$xml = simplexml_load_file("tmp.xml"); printf("%s\n",$xml->product[0]->note->asXML());'

Result:

<note>&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'&gt;Ürün İsmi:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style="font-family: &amp;quot;Open Sans&amp;quot;,sans-serif; font-size: 15px; color: #000;"&gt;Ks Games 500 Parça Art Gallery Garden Sea Jin Park Puzzle 11377
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 15px; color: #000;'&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style='font-family: "Open Sans",sans-serif; font-size: 19px; font-weight:600; color: #1e55fa'&gt;Ürün Hakkında Bilgi:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0"&gt;&lt;span style="font-family: &amp;quot;Open Sans&amp;quot;,sans-serif; font-size: 15px; color: #000;"&gt;(-)&lt;/span&gt;&lt;/p&gt;</note>

Post a Comment for "Simplexml_load_string(): Entity: Line 18: Parser Error : Entity 'nbsp' Not Defined"