We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

How do I use the object that XML Parser Class creates?

Development and Programming

Kevin Smith's avatar
Kevin Smith
4,784 posts
15 years ago
Kevin Smith's avatar Kevin Smith

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?

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
15 years ago
Pascal Kriete's avatar Pascal Kriete

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.