Solved

get xpath attributes from xml

Posted on 2010-08-18
10
525 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now