So, newb here in the world of OOP. I’m working on a plugin that receives data through an API call, and that call sends its return in the form of an XML file. I’ve successfully used the XML Parser Class to convert that XML data into an object, but then… I’m stuck. I’m not sure what to do now. I can see that all the data has been retrieved by using print_r() on the object, but I don’t know how to make use of it.
I know it’s probably swimmingly simple, but this is new territory for me. None of the tutorials I’ve found seem to help. Any guidance?
It’s pretty simple, but keeping track of where you are can be confusing. Basically, every xml tag becomes an object, with a tag name, a value, parameters, and an array of children.
You can grab those using standard object oriented code:
$xmlobj->tag; // name
$xmlobj->value; // value
$xmlobj->attributes; // attributes (associative array)
$xmlobj->children; // children (array)The parser returns the root (outer most) tag. That tag will have children that you can loop through, which again may have more children, etc. You work your way down.
So, for example - take the <emails> example xml from the docs. You could turn that into a regular array of emails by doing:
$emails = array();
// Loop through the root object's children
foreach($xml_obj->children as $email)
{
$email_info = array();
// Now look through the contents of the <email> tag
foreach($email->children as $info_field)
{
$email_info[$info_field->tag] = $info_field->value;
}
$emails[] = $email_info;
}Hope that helps.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.