PHP XML DOM Get Childnodes and Attributes

Reply

Join Date: Mar 2008
Posts: 36
Reputation: lonestar23 is an unknown quantity at this point 
Solved Threads: 0
lonestar23 lonestar23 is offline Offline
Light Poster

PHP XML DOM Get Childnodes and Attributes

 
0
  #1
Jun 6th, 2009
I am trying to parse a XML document but can not seem to parse both Childnodes and Attributes in the same Foreach statement.

=====================
PHP CODE
=====================
  1. <?PHP
  2. $webResults = $response->getElementsByTagName($prnt0);
  3. if ($webResults->length<>0) {
  4. foreach($webResults as $value){
  5. $title = $value->childNodes->item($child1)->nodeValue;
  6. }
  7. }
  8. ?>

Need the title childnode and all links from tag name "links" attribute "href" Thanks in Advance!

=====================
XML is from twitter
=====================
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <feed xmlns:google="base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="a9.com/-/spec/opensearch/1.1/" xmlns="www.w3.org/2005/Atom" xmlns:twitter="api.twitter.com/">
  3. <entry>
  4. <id>tag:search.twitter.com,2005:1982519510</id>
  5. <published>2009-05-31T18:22:20Z</published>
  6. <link type="text/html" rel="alternate" href="twitter.com/buildasearch/statuses/1982519510"/>
  7. <title>New Search Background Uploader ! Upload your favorite background image to BuildaSearch. GIF - JPEG - PNG. Just do it!</title>
  8. <content type="html">New Search Background Uploader ! Upload your favorite background image to &lt;b&gt;BuildaSearch&lt;/b&gt;. GIF - JPEG - PNG. Just do it!</content>
  9. <updated>2009-05-31T18:22:20Z</updated>
  10. <link type="image/png" rel="image" href="amazonaws.com/twitter_production/profile_images/66983558/build_normal.png"/>
  11. <twitter:source>&lt;a href="twitter.com/"&gt;web&lt;/a&gt;</twitter:source>
  12. <twitter:lang>en</twitter:lang>
  13. <author>
  14. <name>buildasearch (buildasearch)</name>
  15. <uri>twitter.com/buildasearch</uri>
  16. </author>
  17. </entry>
  18. </feed>
Last edited by lonestar23; Jun 6th, 2009 at 7:59 pm. Reason: misspelling
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #2
Jun 7th, 2009
Have you tried using a print_r( $webResults ); statement to make sure that this variable has the values you are hoping for?
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 36
Reputation: lonestar23 is an unknown quantity at this point 
Solved Threads: 0
lonestar23 lonestar23 is offline Offline
Light Poster

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #3
Jun 7th, 2009
I got the following running print_r($webResults);

"DOMNodeList Object ()" and that is all....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #4
Jun 7th, 2009
So the issue is above the decleration of $webResults, whats $prnt0 ?

It seems like the method you are using to fetch the data is not working.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 36
Reputation: lonestar23 is an unknown quantity at this point 
Solved Threads: 0
lonestar23 lonestar23 is offline Offline
Light Poster

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #5
Jun 7th, 2009
$prnt0 equals 'entry'

  1. <?PHP
  2. $gurl = 'search.twitter.com/search.atom?lang=en&q=devo'
  3.  
  4. $getUrl = $gurl;
  5. $resUrl = new DOMDocument();
  6. $resUrl->load($getUrl);
  7.  
  8. $prnt0 = 'entry';
  9. $webResults = $resUrl->getElementsByTagName($prnt0);
  10. if ($webResults->length<>0) {
  11. foreach($webResults as $value){
  12. $title = $value->childNodes->item($child1)->nodeValue;
  13. }
  14. }
  15. ?>

Let me know if the new code is detailed enough. I know how to get the attributes using SimpleXML and DOM, but can not frigure out how to get childnodes and attribute information at the same time using DOM.

Thanks,
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #6
Jun 7th, 2009
This seems to be your issue $webResults = $resUrl->getElementsByTagName($prnt0); .

Try print_r( $resUrl ); this should print out something.

You need to do this for pretty much every variable you have so as you know were things are dropping their values.

Maybe take a look at either the manual or the documentation about this specific method as I don't have much knowledge of it.

Could you not create a function that uses a series of explotions and loops to extract the values?

I could give you some help with this. I am not really much of a PHP 5 & 6 guy as I learnt PHP years ago. PHP 5 should suffice.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,433
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 233
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #7
Jun 7th, 2009
@Josh, PHP 6 doesn't exist yet and wont for a long while

For lonestar. getElementsByTagName returns a DOMNodeList which, when iterated over, each entry is a DOMNode which can act as a DOMElement which has the function getAttribute($attr_name); So you can do (in theory, see php.net/DOM):
  1. $elemlist = $someDom->getElementsByTagName('link');
  2. foreach($elemlist as $elem) {
  3. var_dump($elem);
  4. }
}
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #8
Jun 11th, 2009
Oh I am in my own little world, don't mind me .

I haven't taken much of a look at PHP5. Not sure if it is the version but the shit with all of the arrows, make my neck crawl. It seems pointless.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,433
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 233
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: PHP XML DOM Get Childnodes and Attributes

 
0
  #9
Jun 11th, 2009
Originally Posted by Josh Connerty View Post
Oh I am in my own little world, don't mind me .

I haven't taken much of a look at PHP5. Not sure if it is the version but the shit with all of the arrows, make my neck crawl. It seems pointless.
That "shit with all of the arrows" is OOP, you'll pretty much have to learn it to work on any PHP newer than version 4. Do a few google searches on imperative vs object oriented programming and you'll have your answer to whether or not it's pointless
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2081 | Replies: 8
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC