0

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);
3
  • 1
    Show your simplexml_load_string function calling code Commented Nov 12, 2019 at 10:28
  • updated in question Commented Nov 12, 2019 at 10:37
  • The $data is escaped from HTML chars... Commented Nov 12, 2019 at 10:41

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.