Page 1 of 1

Retrieving XML values from node of complex document with PHP

Posted: Sun Apr 18, 2010 3:20 pm
by prince.pangaea
well, this is the first time i have posted something like this. My first post on devnetwork!
I have been fighting this for about 5 days and have gotten nowhere.

i have this url: http://waterservices.usgs.gov/WOF/Insta ... riod=PT51M

which returns this XML:
<timeSeriesResponse xsi:schemaLocation="http://www.cuahsi.org/waterML/1.0/ http://waterservices.usgs.gov/WOF/WaterML-1.0.xsd">
<queryInfo>
<creationTime>2001-01-01T22:22:22</creationTime>
<queryURL>
location=08025350&variable=00062&startDate=2010-04-18T14:21:42.169&endDate=2010-04-18T15:12:42.169&period=PT51M&mode=2&token=unknown
</queryURL>
<criteria>
<locationParam>08025350</locationParam>
<variableParam>00062</variableParam>
<timeParam>
<beginDateTime>2010-04-18T14:21:42.169</beginDateTime>
<endDateTime>2010-04-18T15:12:42.169</endDateTime>
</timeParam>
</criteria>
<note title="RequestId">be848300-4b26-11df-8c98-00144f6f9e5a</note>
<note title="DataSource">webnaddeb01</note>
</queryInfo>
<timeSeries name="NWIS Time Series Instantaneous Values">
<sourceInfo xsi:type="SiteInfoType">
<siteName>Toledo Bd Res nr Burkeville, TX</siteName>
<siteCode agencyCode="USGS" siteID="241" network="NWIS">08025350</siteCode>
<geoLocation>
<geogLocation xsi:type="LatLonPointType" srs="EPSG:4326">
<latitude>31.1962984</latitude>
<longitude>-93.5721192</longitude>
</geogLocation>
</geoLocation>
</sourceInfo>
<variable>
<variableCode vocabulary="NWISUV">00062</variableCode>
<variableName>Reservoir elevation</variableName>
<variableDescription>
Elevation of reservoir water surface above datum, feet
</variableDescription>
<dataType>Instantaneous</dataType>
<units unitsType="Flow" unitsAbbreviation="ft"/>
<NoDataValue>-999999</NoDataValue>
</variable>
<values count="1">
<value dateTime="2010-04-18T14:30:00.000-05:00" qualifiers="P">170.64</value>
<qualifier vocabulary="uv_rmk_cd" network="USGS" qualifierCode="P">Provisional data subject to revision.</qualifier>
<method methodID="0">
<MethodDescription>/Reservoir elevation</MethodDescription>
</method>
</values>
</timeSeries>
</timeSeriesResponse>



All i want to do is take the text in "<value dateTime="2010-04-18T14:30:00.000-05:00" qualifiers="P">170.64</value>" and display it in a block on a drupal site.
so when i get it it will look like "The current Lake Level is 170.64 ft"
now i realize that there are 2 good things that can do it and thats the DOM and SimpleXML, but i cant seem to get anything to work.
I am very very new to PHP and any other programming, so please forgive me. I am studying the best i can.
thanks all

Re: Retrieving XML values from node of complex document with

Posted: Sun Apr 18, 2010 5:04 pm
by requinix

Code: Select all

<timeSeriesResponse xsi:schemaLocation="http://www.cuahsi.org/waterML/1.0/ http://waterservices.usgs.gov/WOF/WaterML-1.0.xsd">
	<queryInfo>
		<creationTime>2001-01-01T22:22:22</creationTime>
		<queryURL>location=08025350&variable=00062&startDate=2010-04-18T14:21:42.169&endDate=2010-04-18T15:12:42.169&period=PT51M&mode=2&token=unknown</queryURL>
		<criteria>
			<locationParam>08025350</locationParam>
			<variableParam>00062</variableParam>
			<timeParam>
				<beginDateTime>2010-04-18T14:21:42.169</beginDateTime>
				<endDateTime>2010-04-18T15:12:42.169</endDateTime>
			</timeParam>
		</criteria>
		<note title="RequestId">be848300-4b26-11df-8c98-00144f6f9e5a</note>
		<note title="DataSource">webnaddeb01</note>
	</queryInfo>
	<timeSeries name="NWIS Time Series Instantaneous Values">
		<sourceInfo xsi:type="SiteInfoType">
			<siteName>Toledo Bd Res nr Burkeville, TX</siteName>
			<siteCode agencyCode="USGS" siteID="241" network="NWIS">08025350</siteCode>
			<geoLocation>
				<geogLocation xsi:type="LatLonPointType" srs="EPSG:4326">
					<latitude>31.1962984</latitude>
					<longitude>-93.5721192</longitude>
				</geogLocation>
			</geoLocation>
		</sourceInfo>
		<variable>
			<variableCode vocabulary="NWISUV">00062</variableCode>
			<variableName>Reservoir elevation</variableName>
			<variableDescription>Elevation of reservoir water surface above datum, feet</variableDescription>
			<dataType>Instantaneous</dataType>
			<units unitsType="Flow" unitsAbbreviation="ft"/>
			<NoDataValue>-999999</NoDataValue>
		</variable>
		<values count="1">
			<value dateTime="2010-04-18T14:30:00.000-05:00" qualifiers="P">170.64</value>
			<qualifier vocabulary="uv_rmk_cd" network="USGS" qualifierCode="P">Provisional data subject to revision.</qualifier>
			<method methodID="0">
				<MethodDescription>/Reservoir elevation</MethodDescription>
			</method>
		</values>
	</timeSeries>
</timeSeriesResponse>
SimpleXML is really easy to use.

Code: Select all

$xml = simplexml_load_file("http://waterservices.usgs.gov/WOF/InstantaneousValues?location=08025350&variable=00062&period=PT51M");
echo (string)$xml->timeSeries->values->value[0];
Technically the [0] there on the end is unnecessary - for that input. But the XML suggests that there could be multiple <value>s so using a [0] ensures you'll always get the first value.