1
votes

i'm reading a xml in php but i've got an error on the parse:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : error parsing attribute name

my xml string is:

$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message>
  <date>1352210570</date>
  <from>Rui <[email protected]></from>
  <subject>Mensagem de Teste</subject>
  <url>test.com</url>
</message>
</messages>';

i parse the xml with simplexml_load_string($xmlString);

how can i escape the < and > on the from node??

1

1 Answers

0
votes
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message>
  <date>1352210570</date>
  <from><![CDATA[Rui <[email protected]>]]></from>
  <subject>Mensagem de Teste</subject>
  <url>test.com</url>
</message>
</messages>';

The addition to your code was the inclusion of <![CDATA[ after your <from> tag and a ]]> before your </from> tag.

Note that within the CDATA tag, there is no quotes to denote a string. This tripped me up initially, so I'm trying to make sure it doesn't happen to others.

While &lt; and &gt; are probably a bit speedier in terms of performance, if you're writing potentially dynamic XML strings, The <![CDATA[someText]]> is much more of a "blanket escape"