Jump to content

XML Parsing Assistance


michael.davis

Recommended Posts

The issue is if you have a variable number of child elements under each time interval you can't access the values directly (because you may not know what they are). So what is the best method of extracting the values from the time interval child elements in a form that can be used as outlined above?

 

Why not access them directly? The OP seemed to have no problem with knowing what (the element names) he wanted to get information from.  Anyway, you already know how to get values from the child elements whose names you don't know (like the gauges). 

Link to comment
Share on other sites

You're using the wrong feed on your example. The second feed provided was a bit more complex:

 

$xml = new SimpleXMLElement('http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGaugesAllIntervals', 0, true);

 

Well the desire is to iterate over the entire xml document and for each place name pull out the child elements of the intervals in a form that can be:

 

a) directly echoed to the page

b) stored in a database

 

The issue is if you have a variable number of child elements under each time interval you can't access the values directly (because you may not know what they are).

 

So what is the best method of extracting the values from the time interval child elements in a form that can be used as outlined above?

 

Sorry I don't mean to sound deliberately obtuse (or to be awkward), its just that using get_object_vars appears to do the above but you suggest it's incorrect. So I genuinely want to understand how it's possible to achieve the above without using get_object_vars or type casting if you don't know the end precise end element name.

 

SimpleXML provides iteration through the class. There's no reason to convert it to an array only to iterate through it.

 

Unknown values can be looped through.

 

<?php

$xml = new SimpleXMLElement('http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGauges15m', 0, true);

header('content-type: text/plain');

foreach( $xml as $location => $data ) {
echo "$location\n";
foreach( $data->clsRainGaugeReading as $subdata ) {
	foreach( $subdata as $key => $value ) {
		echo "\t$key -> $value\n";
	}
}
}

?>

Link to comment
Share on other sites

The concept is still the same.

 

<?php

$xml = new SimpleXMLElement('http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGaugesAllIntervals', 0, true);
header('content-type: text/plain');

foreach( $xml as $location => $data ) {
echo "$location\n";
foreach( $data as $time => $subdata ) {
	echo "\t$time\n";
	foreach( $subdata as $key => $value ) {
		echo "\t\t$key -> $value\n";
	}
}
}

?>

Link to comment
Share on other sites

Yeah I realise this has gone a little beyond the original solution but I really am just trying to understand what was wrong with extracting the values because I cant see a solution if you don't.

 

Lets say I did this (without extracting the object values):

 

<?php

$data_url = 'http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGaugesAllIntervals';
$package  = new SimpleXMLElement($data_url, NULL, TRUE);

$topLevel = array();

foreach($package->children() as $k=>$gauge) {

$topLevel[$k]  = array(

		'FifteenMinute'		=>  $gauge->FifteenMinute, 
		'ThirtyMinute'		=> $gauge->ThirtyMinute, 
		'OneHour'				=> $gauge->OneHour, 
		'TwoHour'				=> $gauge->TwoHour, 
		'FourHour'			=> $gauge->FourHour, 
		'SixHour'				=> $gauge->SixHour, 
		'TwelveHour'			=> $gauge->TwelveHour, 
		'TwentyfourHour'	=> $gauge->TwentyfourHour

	);

}



echo "<pre>";


print_r($topLevel);



?>

 

 

I then get an array as follows:

 

Array
(
    [bntCrk] => Array
        (
            [FifteenMinute] => SimpleXMLElement Object
                (
                    [sampleDate] => 8-28-2012
                    [sampleTime] => 20:20:14
                    [GaugeName] => BntCrk
                    [sampleValue] => 0
                )

            [ThirtyMinute] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8-28-2012
                    [sampleDateEnd] => 8-28-2012
                    [sampleTimeStart] => 20:5:14
                    [sampleTimeEnd] => 20:20:14
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

            [OneHour] => SimpleXMLElement Object
                (
                    [sampleDate] => 8 - 28 - 2012
                    [sampleTime] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValue] => 0
                )

            [TwoHour] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8 - 28 - 2012
                    [sampleDateEnd] => 8 - 28 - 2012
                    [sampleTimeStart] => 19:20:15
                    [sampleTimeEnd] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

            [FourHour] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8 - 28 - 2012
                    [sampleDateEnd] => 8 - 28 - 2012
                    [sampleTimeStart] => 17:20:15
                    [sampleTimeEnd] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

            [sixHour] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8 - 28 - 2012
                    [sampleDateEnd] => 8 - 28 - 2012
                    [sampleTimeStart] => 15:20:15
                    [sampleTimeEnd] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

            [TwelveHour] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8 - 28 - 2012
                    [sampleDateEnd] => 8 - 28 - 2012
                    [sampleTimeStart] => 4:20:15
                    [sampleTimeEnd] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

            [TwentyfourHour] => SimpleXMLElement Object
                (
                    [sampleDateStart] => 8 - 27 - 2012
                    [sampleDateEnd] => 8 - 28 - 2012
                    [sampleTimeStart] => 16:20:15
                    [sampleTimeEnd] => 20:20:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

        )

   
      )

)

// Stripped out other values in array as it exceeds character limit on post

 

Ok so now we have an array of xml objects that I haven't tampered with by extracting the oject values.

 

Now because that's an array of xml objects how do you access them directly? So if i want to echo one of those values to the page or use it to insert into a database how do you do that?

 

 

 

The issue is if you have a variable number of child elements under each time interval you can't access the values directly (because you may not know what they are). So what is the best method of extracting the values from the time interval child elements in a form that can be used as outlined above?

 

Why not access them directly? The OP seemed to have no problem with knowing what (the element names) he wanted to get information from.  Anyway, you already know how to get values from the child elements whose names you don't know (like the gauges).

Link to comment
Share on other sites

Your array contains the same data as your object. What was the point of turning it into an array?

 

If you want to access individual values

<?php

$xml = new SimpleXMLElement('http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGaugesAllIntervals', 0, true);
header('content-type: text/plain');

echo $xml->BntCrk->FifteenMinute->SampleDate;

echo "\n";

if( isset($xml->BntCrk->TwoHour->SampleValueAccumulation) )
echo $xml->BntCrk->TwoHour->SampleValueAccumulation;
else
echo 'value does not exist';


?>

 

Turning that into an array will not make anything easier, you'll just have the same data in memory twice.

Link to comment
Share on other sites

Yes I realise that hehe...

 

But I only did it that way because i was told doing this was wrong:

 

foreach($package->children() as $k=>$gauge) {


$topLevel[$k] = array(

		'FifteenMinute'		=>  get_object_vars($gauge->FifteenMinute), 
		'ThirtyMinute'		=>get_object_vars($gauge->ThirtyMinute), 
		'OneHour'				=>get_object_vars($gauge->OneHour), 
		'TwoHour'				=>get_object_vars($gauge->TwoHour), 
		'FourHour'			=>get_object_vars($gauge->FourHour), 
		'SixHour'				=>get_object_vars($gauge->SixHour), 
		'TwelveHour'			=>get_object_vars($gauge->TwelveHour), 
		'TwentyfourHour'	=>get_object_vars($gauge->TwentyfourHour)

	);

}

 

which gives an array like:

 

[bntCrk] => Array
        (
            [FifteenMinute] => Array
                (
                    [sampleDate] => 8-28-2012
                    [sampleTime] => 20:40:15
                    [GaugeName] => BntCrk
                    [sampleValue] => 0
                )

            [ThirtyMinute] => Array
                (
                    [sampleDateStart] => 8-28-2012
                    [sampleDateEnd] => 8-28-2012
                    [sampleTimeStart] => 20:25:15
                    [sampleTimeEnd] => 20:40:15
                    [GaugeName] => BntCrk
                    [sampleValueAccumulation] => 0
                )

 

Which does give me a plain old array of the values as opposed to an array that is a direct replica of the object. So i think my whole confusion is centering around the fact that the above was pointed out as being wrong.

 

Sorry guys i don't mean to be an idiot about this I just find it easier to deal with a large xml file if it's put into a simple array. But maybe i have it all backwards??

 

 

 

 

Your array contains the same data as your object. What was the point of turning it into an array?

 

If you want to access individual values

<?php

$xml = new SimpleXMLElement('http://www.amecim.com/metronashvillewater/metronashvilleraingaugeservice.asmx/getAllGaugesAllIntervals', 0, true);
header('content-type: text/plain');

echo $xml->BntCrk->FifteenMinute->SampleDate;

echo "\n";

if( isset($xml->BntCrk->TwoHour->SampleValueAccumulation) )
echo $xml->BntCrk->TwoHour->SampleValueAccumulation;
else
echo 'value does not exist';


?>

 

Turning that into an array will not make anything easier, you'll just have the same data in memory twice.

Link to comment
Share on other sites

Ok i can see what you're saying.  I think i need to go away and practice with simplexml a bit more because through being inexperienced with simpleXML I am clearly trying to revert back to what seems logical to me (i.e. creating an array out of the data), but as you've pointed out, its probably replicating and bypassing what simpleXML was designed to do  :(

 

Thanks for your patience guys!

 

haha think i will refrain from trying to give answers to posts when i only undestand  half the story from now on...

 

 

SimpleXML allows you to deal with an XML file as if it were an array.

 

What you're doing is redundant.

 

$xml['node']['subnode'] = 'value' isn't much different than $xml->node->subnode = 'value'

Link to comment
Share on other sites

haha think i will refrain from trying to give answers to posts when i only undestand  half the story from now on...

 

You're getting there. And trying to answer questions is a great way of learning! Every day is a school day.

Link to comment
Share on other sites

Thanks Salathe - well i've certainly learned a lot from this post :)

 

haha think i will refrain from trying to give answers to posts when i only undestand  half the story from now on...

 

You're getting there. And trying to answer questions is a great way of learning! Every day is a school day.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.