Solved

get xpath attributes from xml

Posted on 2010-08-18
10
534 Views
Last Modified: 2013-11-11
Im trying to echo the attribute from popularity xpath. Im getting errors.

The results should read: 4759525

Not sure what im doing wrong, here is my code:
feed2......

<SERVER VER="0.9" URL="computersciencelaboratories.com/" HOME="0" AID="=">
<SD TITLE="A" FLAGS="" HOST="computersciencelaboratories.com">
<LINKSIN NUM="3"/>
</SD>
<SD>
<POPULARITY URL="computersciencelaboratories.com/" TEXT="4759525"/>
<REACH RANK="4682738"/>
<RANK DELTA="-704558"/>
</SD>
</SERVER>


$xml2 = simplexml_load_file("$feed2");
$result2 = $xml2->xpath(server/sd[2]/popularity/@text);
echo $result2[0]->asXml();

Open in new window

0
Comment
Question by:zefex
[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
  • 5
  • 5
10 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33472221
XML and XPath are case sensitive
The correct XPath would be

xpath(/SERVER/SD[2]/POPULARITY/@TEXT)

I am not sure about the PHP, I think you need quotes around the XPath

xpath("/SERVER/SD[2]/POPULARITY/@TEXT")
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33472556
By the way, I am pretty uncertain about this
simplexml_load_file("$feed2")
I think it should be
simplexml_load_file($feed2)
otherwise the variable would be regarded as a string
0
 

Author Comment

by:zefex
ID: 33472609
I got this working!!!  But the XML is not complete.....
$string = <<<XML
<SD>
<POPULARITY URL="computersciencelaboratories.com/" TEXT="4759525"/>
<REACH RANK="4682738"/>
<RANK DELTA="-704558"/>
</SD>
XML;

$xml = simplexml_load_string($string);
foreach($xml->POPULARITY[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

Open in new window

0
Stressed Out?

Watch some penguins on the livecam!

 

Author Comment

by:zefex
ID: 33472615
Im trying to get this to work, it has the complete XML. But i am getting errors. I am so close i can taste the error.
$string = <<<XML
<SERVER VER="0.9" URL="computersciencelaboratories.com/" HOME="0" AID="=">
<SD TITLE="A" FLAGS="" HOST="computersciencelaboratories.com">
<LINKSIN NUM="3"/>
</SD>
<SD>
<POPULARITY URL="computersciencelaboratories.com/" TEXT="4759525"/>
<REACH RANK="4682738"/>
<RANK DELTA="-704558"/>
</SD>
</SERVER>
XML;

$xml = simplexml_load_string($string);
foreach($xml->POPULARITY[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33472624
what errors do you get?
0
 

Author Comment

by:zefex
ID: 33472646
Fatal error: Call to a member function attributes() on a non-object in script.php on line 26
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 33472829
why did you leave the XPath path?
I don't know enough about php, but the line with POPULARITY[0] smells rotten
0
 

Author Comment

by:zefex
ID: 33472841
i left the xpath because i couldnt get it working and i was trying other examples.

I finally got it to work though :)

$xml->SD[1]->POPULARITY[0]->attributes()

Thanks so much for your help, i didnt know it was case sensitive.
0
 

Author Closing Comment

by:zefex
ID: 33472859
He is a Genius.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33472904
welcome
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

689 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