943,773 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7617
  • PHP RSS
Jun 6th, 2009
0

PHP XML DOM Get Childnodes and Attributes

Expand Post »
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
=====================
PHP Syntax (Toggle Plain Text)
  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
=====================
PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

Have you tried using a print_r( $webResults ); statement to make sure that this variable has the values you are hoping for?
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

I got the following running print_r($webResults);

"DOMNodeList Object ()" and that is all....
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

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.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

$prnt0 equals 'entry'

PHP Syntax (Toggle Plain Text)
  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,
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

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.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Jun 7th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

@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):
php Syntax (Toggle Plain Text)
  1. $elemlist = $someDom->getElementsByTagName('link');
  2. foreach($elemlist as $elem) {
  3. var_dump($elem);
  4. }
}
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 11th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

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.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Jun 11th, 2009
0

Re: PHP XML DOM Get Childnodes and Attributes

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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need Help : Failed to connect to mailserver
Next Thread in PHP Forum Timeline: update mysql table using javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC