Solved

Quick php xpath question

Posted on 2009-07-01
7
232 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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now