Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

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

0
warrenrapson
Asked:
warrenrapson
  • 4
  • 3
1 Solution
 
mallcoreCommented:
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

0
 
warrenrapsonAuthor Commented:
Great!

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

Eg.

echo 'Street Name: ' . ?????????????
0
 
mallcoreCommented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mallcoreCommented:
Ops made a mistake, sorry.


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

Open in new window

0
 
warrenrapsonAuthor Commented:
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.
0
 
mallcoreCommented:
Try $result->Street;
0
 
warrenrapsonAuthor Commented:
Brilliant!!

Thanks for your help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now