Solved

Quick php xpath question

Posted on 2009-07-01
7
236 Views
Last Modified: 2013-11-11
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
Comment
Question by:warrenrapson
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:mallcore
ID: 24760949
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
 

Author Comment

by:warrenrapson
ID: 24761001
Great!

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

Eg.

echo 'Street Name: ' . ?????????????
0
 
LVL 5

Expert Comment

by:mallcore
ID: 24761024
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Expert Comment

by:mallcore
ID: 24761029
Ops made a mistake, sorry.


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

Open in new window

0
 

Author Comment

by:warrenrapson
ID: 24761101
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
 
LVL 5

Accepted Solution

by:
mallcore earned 500 total points
ID: 24761129
Try $result->Street;
0
 

Author Comment

by:warrenrapson
ID: 24761232
Brilliant!!

Thanks for your help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question