Macbernie
Macbernie

Reputation: 1323

simplexml_load_string() StartTag: invalid element name

I use simplexml_load_string() to parse a XML API response.

$lien = "http://myresponseurl";
$page = simplexml_load_string($this->get_data($lien));

Everythink is working good, except in this case:

<libdivision><1500 Poule H</libdivision>

Where the text to get back should be "<1500 Poule H" but, the "<" caractère is also in XML the symbol of an opening tag...

What should I do to get back "<1500 Poule H" without any errors ?

Warning: simplexml_load_string(): Entity: line 4: parser error : StartTag: invalid element name
Warning: simplexml_load_string(): <libdivision><1500 Poule G</libdivision>
Warning: simplexml_load_string(): ^

Thanks for help

Upvotes: 4

Views: 6517

Answers (1)

Daniel W.
Daniel W.

Reputation: 32340

<libdivision><1500 Poule H</libdivision>

This is invalid XML. The provider of the API should fix this, this is a clear bug in the API and a client is not responsible for working with malformed data. There are two options:

Wrap it in a CDATA field:

<libdivision><![CDATA[<1500 Poule H]]></libdivision>

Or encode the symbol:

<libdivision>&lt;1500 Poule H</libdivision>

Upvotes: 4

Related Questions