hrishi
hrishi

Reputation: 1656

php simplexml_load_string not working on XML string

I am trying to read XML string using PHP function simplexml_load_string but I get below errors

Message: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found

Message: simplexml_load_string(): &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

Message: simplexml_load_string(): ^

I get XML sting in variable as below (var_dum). I get this XML string from another file using fgetc I also tried '/xml' at end but it gives same error

     string(3155) "<?xml version="1.0" encoding="UTF-8"?>
    <LEIRecord xmlns:lei="http://www.gleif.org/data/schema/leidata/2016">
          <LEI>029200013A5N6ZD0F605</LEI>
          <Entity>
            <LegalName xml:lang="en">AFRINVEST SECURITIES LIMITED</LegalName>
            <OtherEntityNames>
              <OtherEntityName xml:lang="fr" type="ALTERNATIVE_LANGUAGE_LEGAL_NAME">AFRINVEST SECURITIES LIMITED</OtherEntityName>
            </OtherEntityNames>
            <LegalAddress xml:lang="en">
              <FirstAddressLine>27 GERRARD ROAD</FirstAddressLine>
              <AdditionalAddressLine>IKOYI</AdditionalAddressLine>
              <City>LAGOS</City>
              <Region>NG-LA</Region>
              <Country>NG</Country>
              <PostalCode>23401</PostalCode>
            </LegalAddress>
            <LegalJurisdiction>NG</LegalJurisdiction>
            <LegalForm>
              <EntityLegalFormCode>8888</EntityLegalFormCode>
              <OtherLegalForm>LIMITED</OtherLegalForm>
            </LegalForm>
            <EntityStatus>ACTIVE</EntityStatus>
          </Entity>
        </LEIRecord>
    "

Code :

json_decode(json_encode((array)simplexml_load_string($data)), TRUE);

Upvotes: 0

Views: 767

Answers (1)

BadPiggie
BadPiggie

Reputation: 6379

It seems like the $data is escaped from HTML chars...

Try to decode before pass it to simplexml_load_string..

Instead of

json_decode(json_encode((array)simplexml_load_string($data)), TRUE);

Try this...

json_decode(json_encode((array)simplexml_load_string(htmlspecialchars_decode($data))), TRUE);

Upvotes: 1

Related Questions