Search code examples
phpxmlxmldocument

Unable to read XML from API call


I have the following code to read xml data from 3rd party

$supplier_response = getsupplierresponse($argument1);
$resultxml = simplexml_load_string($supplier_response);
$test = getXMLNode($resultxml, "FormattedName");
var_dump($test); //no result

function getXMLNode($xmlobj, $path, $nodenumber=0){
  return $xmlobj->getElementsByTagName($path)->item(0)->childNodes->item($nodenumber)->nodeValue;
}

If I read the same xml from a local file, the code works and [var_dump($test);] gives proper result. Is there anything specifically done if we retrieve XML from a third party service in order to read the data?

Here is the xml I retrieved calling a 3rd party service.

<?xml version="1.0" encoding="utf-8"?>
<Resume lang="fr" src="xxx-CVX schema:2.0.29 release:0.23.1.52647 rdate:2017-11-01">
  <StructuredXMLResume>
    <ContactInfo>
      <PersonName id="2">
        <FormattedName>Boris Becker</FormattedName>
        <GivenName>Boris</GivenName>
        <FamilyName>Becker</FamilyName>
      </PersonName>
      <ContactMethod>
        <Mobile id="4">
          <InternationalCountryCode>33</InternationalCountryCode>
          <SubscriberNumber>777777777</SubscriberNumber>
          <FormattedNumber>+33 777777777</FormattedNumber>
        </Mobile>

        <PostalAddress id="3" type="main">
          <CountryCode>FR</CountryCode>
          <Municipality>Paris</Municipality>

        </PostalAddress>
      </ContactMethod>
    </ContactInfo>

































    <RevisionDate id="1">2018-02-22</RevisionDate>
  </StructuredXMLResume>
  <NonXMLResume>

    <TextResume>

      <span inprof="n" id="1" class="date">{{DOCUMENT_DATESTAMP}} 2018-02-22 {{END_DOCUMENT_DATESTAMP}}</span>

      <span inprof="y" id="2" class="nominal">Boris Becker</span>
      <span inprof="y" id="3" class="location">Paris</span>
      <span inprof="y" id="4" class="telephone">0777777777</span>

    </TextResume>

  </NonXMLResume>



  <UserArea>
    <xResumeUserArea>
      <AdditionalPersonalData>
        <ExperienceSummary>
          <ExecutiveBrief>Boris Becker is a resident of Paris, France. He has a track record with no real experience.</ExecutiveBrief>
        </ExperienceSummary>
      </AdditionalPersonalData>
      <ParserInfo>
        <ParserConfiguration>{max_len=50000} {tel_flag=} {send_zip=} {fast_conv=} {DEF_LOCAL=} {sdate=0} {no_email_body=0} {do_clever_zoning=0} {keep_zone_span=0} {keep_span=1} {complex=0} {accept_langs=} {not_accept_langs=} {prefer_lang_cv=} {pers_only=0} {projects_off=0}
          {tree_search_on=0} {all_skills=0} {turbo=0} {split_language=0} {picture=0} {picture_inline=0} {debug=0} {ocr_allowed=1} {name_space=0} {charset=} {hrxml_upgrade_edu_hist=0}{hrxml_add_languages_section=1}{spool=} {docID=} {user=nottx02} </ParserConfiguration>
        <ParserRelease>0.23.1.52647</ParserRelease>
        <ParserReleaseDate>2017-11-01</ParserReleaseDate>
        <ParserSchema>2.0.29</ParserSchema>
        <ParserSchemaLocation>http://cvxdemo.daxtra.com/cvx/cvx_schema/candidate/2.0.29/Resume.xsd</ParserSchemaLocation>
        <ConverterRelease>0.16.2.53041</ConverterRelease>
        <ConverterReleaseDate>2017-12-07</ConverterReleaseDate>
      </ParserInfo>
      <FileStruct filename="/tmp/soap_617785094628694">

        <attachment fname="0439984814400614" ftype="docx" conv="yes" doc_type="cv" lang="">/tmp/soap_617785094628694</attachment>

      </FileStruct>
    </xResumeUserArea>
  </UserArea>
  <preserveWhiteSpace></preserveWhiteSpace>
  <formatOutput>1</formatOutput>
</Resume>


Solution

  • The only way I could fix is this is to save locally and read it again. I am not sure if this is a perfect answer but something worked for me. The xml is made readable by calling the below function.

    function cleanse_supplier_response($supplier_response){
        $result = simplexml_load_string($supplier_response);
        $xml_string = $result->saveXML();
        $thisxml_object = new DOMDocument();
        $thisxml_object->loadXML($xml_string);
        return $thisxml_object;
    }
    

    Hope this helps those who are curious about an option to fix the issue.