Link to home
Start Free TrialLog in
Avatar of warrenrapson
warrenrapson

asked on

Quick php xpath question

Hi all,

One hour has pass and I can't work out how to get the result of an xpath query!!!

With the below, how can I return one node's value? I mean, how would I say

for each result
echo show_me_the_streetname_value
next

???

Thanks,

Warren


<?php
$xmlDocument = new DOMDocument();
if ($xmlDocument->load('vw_WebsitePropertyData.xml')) {
	$xpath = new DOMXPath($xmlDocument);
	$nodeList = $xpath->query('/dataroot/vw_WebsitePropertyData[RentPerWeek>22]', $xmlDocument);
}
?>

Open in new window

Avatar of mallcore
mallcore
Flag of Slovenia image

I would do the following:
<?php
$xml = simplexml_load_file("vw_WebsitePropertyData.xml");
 
$results = $xml->xpath("/dataroot/vw_WebsitePropertyData[RentPerWeek>22]'");
 
foreach ($results as $result)
{
   print_r($result);
}
 
?>

Open in new window

Avatar of warrenrapson
warrenrapson

ASKER

Great!

So how would I get a named value from that array object?

Eg.

echo 'Street Name: ' . ?????????????
echo 'Street Name:' . $result[0]

If you have multiple values put it into a for loop like this:
for($i = 0; $i >= count($result) ; $i++)
{
  echo 'Street Name: ' . $result[$i];
}
 
Should work.

Open in new window

Ops made a mistake, sorry.


for($i = 0; $i <= count($result) ; $i++)
{
  echo 'Street Name: ' . $result[$i];
}

Open in new window

I tried that first, and I could see how that would work, but the array contains the SimpleXMLElement object. Below is part of the print_r output

SimpleXMLElement Object ( [ID] => 2 [Contact] => Paul Bradford [Street] => 10 Jeffson Street..............

So I am still lost as to how to pluck the 'street' value from this result set.
ASKER CERTIFIED SOLUTION
Avatar of mallcore
mallcore
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant!!

Thanks for your help.