Solved

Quick php xpath question

Posted on 2009-07-01
7
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML Data Missing in PHP SimpleXML 8 97
read an xml file in perl 2 86
.net and XML report from SCCM 5 80
XML and version of MS XML Dom Document versions 5 46
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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