Forcing a Simplexml Object to a String, Regardless of Context

Forcing a SimpleXML Object to a string, regardless of context

Typecast the SimpleXMLObject to a string:

$foo = array( (string) $xml->channel->item->title );

The above code internally calls __toString() on the SimpleXMLObject. This method is not publicly available, as it interferes with the mapping scheme of the SimpleXMLObject, but it can still be invoked in the above manner.

SimpleXML returns an object instead of a string

Cast it to a string (or an int) to get the contents of the <SESSIONID> tag:

$this->sessionId = (string) $obj->Body->RESULT->SESSIONID;

SimpleXML Output as a String

SimpleXML is simple for a reason. When echoing an element, it automatically converts the object to a string. You can also explicitly cast it to one:

echo (string)$product->artikelnummer;

Getting actual value from PHP SimpleXML node

Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so

$value = (string) $xml->someNode->innerNode;

Compare SimpleXml Object

The problem here, as so often with SimpleXML, is in the fact that a SimpleXMLElement is not a "normal" PHP object. SimpleXML is not a parser which spits out fully-formed PHP objects with properties and methods, but a "live" API linked to an internal representation of an XML document.

The manual page on Comparing Objects states that "Two object instances are equal if they have the same attributes and values, and are instances of the same class." When you run print_r() or var_dump() over a SimpleXMLElement it appears to have properties representing the child nodes and attributes, which would be the same for two objects built from identical XML. However, the actual implementation contains only a pointer into a memory structure created when the XML was parsed, which will be different even if you parse the same string twice. Thus simply comparing two SimpleXMLElement objects with == will never return true.

The actual solution depends on what exactly you want to compare:

  • if you want to see if a particular fragment of the XML is 100% identical between the two documents, you could use ->asXML() to get an XML string for that part of the document; e.g. $objDbXml->Reise->Z_LEISTUNGEN->asXML() == $objApiXml->Reise->Z_LEISTUNGEN->asXML()
  • if there are a few specific properties which you want to compare, you may be better off selecting those out and comparing them individually, so that the test returns true even if they appear in a slightly different order, or with special characters encoded slightly differently

Can not extract data from an simplexml object

First, read and decode xmlData and then get FiscalCode

$xml = simplexml_load_string($str);

$xml1 = simplexml_load_string(html_entity_decode($xml->xmlData));
echo $xml1->row->FiscalCode;

Listing SimpleXml Object

According to provided xml structure you can do something like:

foreach ($xml->Record->ResultData as $element) { 
foreach ($element as $key => $val) {
echo $val['ElementID'].' '.$key.PHP_EOL;
// iterate over children of current element `$val`
foreach ($val as $key1 => $element1) {
if ($key1 === 'Status') {
echo '- '.$key1.PHP_EOL;
} elseif ($key1 === 'ResultItem') {
echo '- '.$element1['ElementID'].' '.$key1.PHP_EOL;
}
}
}
}


Related Topics



Leave a reply



Submit