Solved

get xpath attributes from xml

Posted on 2010-08-18
10
531 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

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.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.

823 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