Re: [PHP] Append Dom Document
On Thu, May 13, 2010 at 7:46 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, I am trying to create a news feed page that loads a number of different feeds depending on what options the user selects. For some reason, I could not figure out how to get the dom document to append the different xml documents that get created. Below is the code, and obviously now every time when I try to have a new item selected, then it displays that element's ?php $q=$_GET[q]; $q2 = explode( ,$q); $count = count($q2); for($i=0;$i$count;$i++) { //find out which feed was selected switch ($q2[$i]) { case Weather: $xml=(http://rss.weather.com/rss/national/rss_nwf_rss.xml?cm_ven=NWFcm_cat=rsspar=NWF_rss;); break; case NFL: $xml = (http://www.nfl.com/rss/rsslanding?searchString=home;); break; default: exit; break; } } $xmlDoc = new DOMDocument(); $xmlDoc-load($xml); //get and output item elements $x=$xmlDoc-getElementsByTagName('item'); for ($i=0; $i=4; $i++) { $item_title=$x-item($i)-getElementsByTagName('title') -item(0)-childNodes-item(0)-nodeValue; $item_link=$x-item($i)-getElementsByTagName('link') -item(0)-childNodes-item(0)-nodeValue; $item_desc=$x-item($i)-getElementsByTagName('description') -item(0)-childNodes-item(0)-nodeValue; echo (pa href=' . $item_link . ' . $item_title . /a); echo (br /); echo ($item_desc . /p); } ? Is there such a function where I could allow my dom document here append and not have to create a new one every time when a new selection is made? Thanks for your help. Alice First, I don't see where you need to append anything. Your code simply opens XML documents that you fetch from remote sources and iterates through the nodes in each document to echo links to the articles. If that's all you need, what you have looks like it will work just fine. If you are concerned about the overhead of creating a new DOMDocument each time through the loop, you could move that step outside the loop. Every time you run $xmlDoc-load($xml) it will replace the existing contents with the new contents. However, I suspect that instantiating a new object is fairly cheap compared to the rest of the actions in your loop, so I doubt you'll save much. As for your original question, if you really need to append multiple XML documents into a single document using DOM, you would need to create a DOMDocument outside the loop that contains your aggregator's information in the header and that will contain the aggregated items. Then inside your loop you would still load each feed into a separate DOMDocument instance just as you are now and import the nodes from the rss feed into your aggregated document. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Append Dom Document
On Fri, May 14, 2010 at 12:04 AM, Nathan Nobbe quickshif...@gmail.com wrote: [snip] having said that if you wanted to append a new DOMNode to an existing one, you would use the appendChild() method. Usually, yes. In this case, since she would be importing nodes from one document into another document, she would need to use importNode() instead of appendChild(). Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Append Dom Document
On Fri, May 14, 2010 at 7:20 AM, Andrew Ballard aball...@gmail.com wrote: On Fri, May 14, 2010 at 12:04 AM, Nathan Nobbe quickshif...@gmail.com wrote: [snip] having said that if you wanted to append a new DOMNode to an existing one, you would use the appendChild() method. Usually, yes. In this case, since she would be importing nodes from one document into another document, she would need to use importNode() instead of appendChild(). right, if there was actually more than one document, which in this case there isnt... whether or not there should be multiple DOMDocuments here is about as unclear as OPs original question. -nathan
RE: [PHP] Append Dom Document
Hi, You are right about the fact I am not having multiple documents, and yet what I am trying to do here is to have one xmldoc, which I have declared in my original email, and have my other rss feeds that I am trying to call from the PHP to append as I check more checkboxes from the list. Right now, when I check one box, it does present it the way I want it, which is open a new xmldoc, and print out the rss feed. Yet, when I try to check the second box, it gives me the display of the rss feed from the second and not the first, because the second one opens a new xmldoc. However, I would like to see both docs in one screen, which is why I want to know if there is such a function available. Is what I am trying to do here possibly by any chance? Thanks for your help. Alice Date: Fri, 14 May 2010 08:26:48 -0600 Subject: Re: [PHP] Append Dom Document From: quickshif...@gmail.com To: aball...@gmail.com CC: aj...@alumni.iu.edu; php-general@lists.php.net On Fri, May 14, 2010 at 7:20 AM, Andrew Ballard aball...@gmail.com wrote: On Fri, May 14, 2010 at 12:04 AM, Nathan Nobbe quickshif...@gmail.com wrote: [snip] having said that if you wanted to append a new DOMNode to an existing one, you would use the appendChild() method. Usually, yes. In this case, since she would be importing nodes from one document into another document, she would need to use importNode() instead of appendChild(). right, if there was actually more than one document, which in this case there isnt... whether or not there should be multiple DOMDocuments here is about as unclear as OPs original question. -nathan _ The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. http://www.windowslive.com/campaign/thenewbusy?tile=multicalendarocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5
Re: [PHP] Append Dom Document
On Fri, May 14, 2010 at 1:14 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, You are right about the fact I am not having multiple documents, and yet what I am trying to do here is to have one xmldoc, which I have declared in my original email, and have my other rss feeds that I am trying to call from the PHP to append as I check more checkboxes from the list. Right now, when I check one box, it does present it the way I want it, which is open a new xmldoc, and print out the rss feed. Yet, when I try to check the second box, it gives me the display of the rss feed from the second and not the first, because the second one opens a new xmldoc. However, I would like to see both docs in one screen, which is why I want to know if there is such a function available. Is what I am trying to do here possibly by any chance? Thanks for your help. Alice Just move all of your DOMDocument code block inside the loop after the switch. ?php $q=$_GET[q]; $q2 = explode( ,$q); $count = count($q2); for($i=0;$i$count;$i++) { //find out which feed was selected switch ($q2[$i]) { case Weather: $xml=(http://rss.weather.com/rss/national/rss_nwf_rss.xml?cm_ven=NWFcm_cat=rsspar=NWF_rss;); break; case NFL: $xml = (http://www.nfl.com/rss/rsslanding?searchString=home;); break; default: exit; break; } $xmlDoc = new DOMDocument(); $xmlDoc-load($xml); //get and output item elements $x=$xmlDoc-getElementsByTagName('item'); for ($i=0; $i=4; $i++) { $item_title=$x-item($i)-getElementsByTagName('title') -item(0)-childNodes-item(0)-nodeValue; $item_link=$x-item($i)-getElementsByTagName('link') -item(0)-childNodes-item(0)-nodeValue; $item_desc=$x-item($i)-getElementsByTagName('description') -item(0)-childNodes-item(0)-nodeValue; echo (pa href=' . $item_link . ' . $item_title . /a); echo (br /); echo ($item_desc . /p); } } ? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Append Dom Document
On Thu, May 13, 2010 at 5:46 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, I am trying to create a news feed page that loads a number of different feeds depending on what options the user selects. For some reason, I could not figure out how to get the dom document to append the different xml documents that get created. Below is the code, and obviously now every time when I try to have a new item selected, then it displays that element's ?php $q=$_GET[q]; $q2 = explode( ,$q); $count = count($q2); for($i=0;$i$count;$i++) { //find out which feed was selected switch ($q2[$i]) { case Weather: $xml=( http://rss.weather.com/rss/national/rss_nwf_rss.xml?cm_ven=NWFcm_cat=rsspar=NWF_rss ); break; case NFL: $xml = (http://www.nfl.com/rss/rsslanding?searchString=home;); break; default: exit; break; } } $xmlDoc = new DOMDocument(); $xmlDoc-load($xml); //get and output item elements $x=$xmlDoc-getElementsByTagName('item'); for ($i=0; $i=4; $i++) { $item_title=$x-item($i)-getElementsByTagName('title') -item(0)-childNodes-item(0)-nodeValue; $item_link=$x-item($i)-getElementsByTagName('link') -item(0)-childNodes-item(0)-nodeValue; $item_desc=$x-item($i)-getElementsByTagName('description') -item(0)-childNodes-item(0)-nodeValue; echo (pa href=' . $item_link . ' . $item_title . /a); echo (br /); echo ($item_desc . /p); } you might have an easier time w/ SimpleXML for this task; have a look at the examples: http://us.php.net/manual/en/simplexml.examples-basic.php the other thing i find strange about this code is the xml parsing assumes a similar schema when the source urls are from seemingly totally disparate domains, and topic .. id guess it sheer coincidence they have overlapping schemas at all. -nathan
RE: [PHP] Append Dom Document
Date: Thu, 13 May 2010 18:49:35 -0600 Subject: Re: [PHP] Append Dom Document From: quickshif...@gmail.com To: aj...@alumni.iu.edu CC: php-general@lists.php.net On Thu, May 13, 2010 at 5:46 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, I am trying to create a news feed page that loads a number of different feeds depending on what options the user selects. For some reason, I could not figure out how to get the dom document to append the different xml documents that get created. Below is the code, and obviously now every time when I try to have a new item selected, then it displays that element's ?php $q=$_GET[q]; $q2 = explode( ,$q); $count = count($q2); for($i=0;$i$count;$i++) { //find out which feed was selected switch ($q2[$i]) { case Weather: $xml=(http://rss.weather.com/rss/national/rss_nwf_rss.xml?cm_ven=NWFcm_cat=rsspar=NWF_rss;); break; case NFL: $xml = (http://www.nfl.com/rss/rsslanding?searchString=home;); break; default: exit; break; } } $xmlDoc = new DOMDocument(); $xmlDoc-load($xml); //get and output item elements $x=$xmlDoc-getElementsByTagName('item'); for ($i=0; $i=4; $i++) { $item_title=$x-item($i)-getElementsByTagName('title') -item(0)-childNodes-item(0)-nodeValue; $item_link=$x-item($i)-getElementsByTagName('link') -item(0)-childNodes-item(0)-nodeValue; $item_desc=$x-item($i)-getElementsByTagName('description') -item(0)-childNodes-item(0)-nodeValue; echo (pa href=' . $item_link . ' . $item_title . /a); echo (br /); echo ($item_desc . /p); } you might have an easier time w/ SimpleXML for this task; have a look at the examples: http://us.php.net/manual/en/simplexml.examples-basic.php the other thing i find strange about this code is the xml parsing assumes a similar schema when the source urls are from seemingly totally disparate domains, and topic .. id guess it sheer coincidence they have overlapping schemas at all. I am not sure what you mean in your second point, but I can explain the first one. I am using PHP to parse RSS feeds, so that is why they all look the same. Or, what do you mean here? I checked out your example, but my problem here is that I cannot get the XML Dom document to append when I make a new selection. I am hoping that it would create a new sub document of some sort beneath the new one, instead of overwriting the entire document. Am I making sense here? Alice _ The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail. http://www.windowslive.com/campaign/thenewbusy?tile=multiaccountocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
Re: [PHP] Append Dom Document
I am not sure what you mean in your second point, but I can explain the first one. I am using PHP to parse RSS feeds, so that is why they all look the same. wow thats hilarious, you can see how little ive worked w/ rss feeds, read *none* Or, what do you mean here? I checked out your example, but my problem here is that I cannot get the XML Dom document to append when I make a new selection. I am hoping that it would create a new sub document of some sort beneath the new one, instead of overwriting the entire document. umm, i dont see you making any modifications to the original document, $xmlDoc in your code. thats why i suggested simple xml, its the go-to imo if all you need to do is traverse an existing document. DOM is much better if you need to edit the document. having said that if you wanted to append a new DOMNode to an existing one, you would use the appendChild() method. Am I making sense here? no because youre not editing the DOMDocument in your code, youre just reading a document; what is getting overwritten here? im not entirely sure what youre trying to accomplish. -nathan