Solved

get xpath attributes from xml

Posted on 2010-08-18
10
533 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
Independent Software Vendors: 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!

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 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