?
Solved

get xpath attributes from xml

Posted on 2010-08-18
10
Medium Priority
?
535 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

801 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