How can I parse XML data using PHP? I want to get the contents of just the element named
<profile xmlns="mysite.com">
<confirmationNumber>128686132</confirmationNumber>
<merchantRefNum>123456</merchantRefNum>
<customerEmail>test@test.com</customerEmail>
</profile>
I was trying to use this code
$sxe = new SimpleXMLElement($decodedMessage);
echo $sxe->getName() . "\n";
foreach ($sxe->children() as $child)
{
echo $child->getName() . "\n";
}
But I don’t know how to select a specific element.
You don’t need the traversal methods of SimpleXML. Just use
simplexml_load_string()if you already have it in one:Then accessing the content is as simple as: