3

I am trying to parse data from the XML below (I shortened the data a lot to give an example of what the data looks like).

For each attribute, I would need to store the data in a separate array.

XML File

<report>
    <title>Resolution Times (Jun 07 00:21)</title>
    <sets>
        <set>
            <legend>Solved in Less than 2 Hours</legend>
            <values>
                <value data="8702" date="2012-05-24"/>
                <value data="8741" date="2012-05-25"/>
                <value data="8741" date="2012-05-26"/>
                <value data="8741" date="2012-05-27"/>
            </values>
        </set>
        <set>
            <legend>Solved in Less than 24 Hours</legend>
            <values>
                <value data="36990" date="2012-05-24"/>
                <value data="37094" date="2012-05-25"/>
                <value data="37096" date="2012-05-26"/>
                <value data="37144" date="2012-05-27"/>
            </values>
        </set>
    </sets>
</report>

Below is some test code I am doing to try and read in the data. For testing purposes I am just printing out to see what data is actually pulled.

$verifyReport = new SimpleXMLElement('305262.xml', null, true);
$testing = $verifyReport->sets->set->values->value;

echo '<ol>';
foreach($testing as $data)
{
    echo '<li>',
    $data['data'].PHP_EOL;
    echo '</li>';
}

echo '</ol>';

$testing1 = $verifyReport->sets->sets->values->value;

echo '<ol>';
foreach($testing1 as $data2)
{
    echo '<li>',
    $data2['data'].PHP_EOL;
    echo '</li>';
}

echo '</ol>';

Below is out the output of the data

1. 8702
2. 8741
3. 8741
4. 8741

Warning: Invalid argument supplied for foreach() in 
/Applications/XAMPP/xamppfiles/htdocs/tango/test.php on line 23

I am able to pull in the first set (Solved in Less than 2 hours) ok, but when I try to pull the data from the second set (Solved in Less than 24 Hours), I get the above error.

Can anyone help correct this issue?

1 Answer 1

3

In the second block you have $testing1 = $verifyReport->sets->sets->values->value; So I guess the second sets has to be set instead. But anyway, why don't you iterate over $verifyReport->sets and read the further values inside of your for loop?

new SimpleXMLElement('305262.xml', null, true);
foreach ($verifyReport->sets->set as $set) {
    echo '<ol>';
    foreach ($set->values->value as $data) {
        echo '<li>',
        $data['data'] . PHP_EOL;
        echo '</li>';
    }
    echo '</ol>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks! Now, I'm just trying to figure out how to store each set of data in separate arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.