Warning: simplexml_load_string() expects parameter 1 to be string, array given

4.1k views Asked by At

I have this error :Warning: simplexml_load_string() expects parameter 1 to be string, object given in on line 195

and also this error: Fatal error: Call to a member function xpath() on a non-object in C:\wamp\

so my code in following :

foreach ( $GetJobResult->JobReferences as $jobreference_index => $JobReferences ) {
            if (isset ( $GetJobResult->JobReferences )) {

                $xmlJobReference = simplexml_load_string ( $JobReferences );

                $JobReferencetitle = $xmlJobReference->xpath ( "//JobReference[Title ='N°  ANCIEN DOSSIER']/Reference" );


                var_dump($GetJobResult->JobReferences);

            }
        }

var_dump($GetJobResult->JobReferences);

object(stdClass)[149]
  public 'JobReference' => 
    array (size=7)
      0 => 
        object(stdClass)[150]
          public 'Type' => string 'STANDARD' (length=8)
          public 'Title' => string 'N° cde client' (length=14)
          public 'Reference' => string '' (length=0)
          public 'ValueType' => string 'ALPHANUMERIC' (length=12)
          public 'ValueLength_Minimum' => int 0
          public 'ValueLength_Maximum' => int 20
      1 => 
        object(stdClass)[151]
          public 'Type' => string 'STANDARD' (length=8)
          public 'Title' => string 'N°  ANCIEN DOSSIER' (length=19)
          public 'Reference' => string '' (length=0)
          public 'ValueType' => string 'NUMERIC' (length=7)
          public 'ValueLength_Minimum' => int 0
          public 'ValueLength_Maximum' => int 6
2

There are 2 answers

0
IMSoP On

Based on your var_dump output, the objects you are looping over haven't actually been produced by SimpleXML - they are stdClass objects, which is PHP's notation for "objects with no class".

You can't simply give an object to simplexml_load_string (which, as its name suggests, and the Warning confirms, expects a string value) and then have all the methods of SimpleXML such as ->xpath() magically available.

If this was in fact XML, you need to find where in the code is parsing it into this set of stdClass objects, and replace the current parser with SimpleXML. If it wasn't XML, you need to find some other method of finding the child nodes, rather than XPath.

0
venca On

simplexml_load_string transforms string to SimpleXmlElement. You are passing object. Thats the problem.