Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplexml_load_string() StartTag: invalid element name

Tags:

php

simplexml

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

like image 516
Macbernie Avatar asked Nov 03 '25 11:11

Macbernie


1 Answers

<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>
like image 98
Daniel W. Avatar answered Nov 06 '25 03:11

Daniel W.