I'm trying to write a script that will update an RSS XML file. I want it to take the existing file and add a new item to the top of the items list. I've previous gotten it to add to the end of the file, but now it's currently not adding the new item at all. I've been checking around online, but I still can't get it to work. Here is what I have so far:
$rssDoc = new DOMDocument();
$rss_file = $_SERVER['DOCUMENT_ROOT'].'/test_site/feed.xml';
$rssDoc->load($rss_file);
$items = $rssDoc->getElementsByTagName('item');
$newItem = $rssDoc->createElement('item');
$rssTitle = $rssDoc->createElement('title');
$rssTitle->appendChild($rssDoc->createTextNode($title));
$newItem->appendChild($rssTitle);
$rssDesc = $rssDoc->createElement('description');
$rssDesc->appendChild($rssDoc->createTextNode($string));
$newItem->appendChild($rssDesc);
$rssLink = $rssDoc->createElement('link');
$rssLink->appendChild($rssDoc->createTextNode($link));
$newItem->appendChild($rssLink);
$rssDate = $rssDoc->createElement('pubDate');
$rssDate->appendChild($rssDoc->createTextNode($pubDate));
$newItem->appendChild($rssDate);
$firstItem = $items->item(0);
$firstItem->insertBefore($newItem,$firstItem->firstChild);
$rssDoc->formatOutput = true;
echo $rssDoc->saveXML();
What am I missing?
"What am I missing?"
The output: $rssDoc->save( 'filename.xml' ) http://php.net/domdocument.save.php
I got it working. I changed these lines:
$firstItem = $items->item(0);
$firstItem->insertBefore($newItem,$firstItem->firstChild);
To this line:
$items->item(0)->parentNode->insertBefore($newItem,$items->item(0));
Related
I have an XML file which is something like this:
(fileName : abc.xml)
<Envelope>
<Body>
<SResponse>
<Body>
<SResponseDetails>
<SItem>...</SItem>
<SItem>...</SItem>
<SItem>...</SItem>
<SItem>...</SItem>
</SResponseDetails>
</Body>
</SResponse>
</Body>
</Envelope>
I want to store all <SItem> in a db table (1 record for each SItem). I am using PHP for it and using XmlStringStreamer.
Here is my code for reading this file and processing it.
$streamer = \Prewk\XmlStringStreamer::createStringWalkerParser(__DIR__ . "/tempFile.xml");
$stream = new Stream\File(__DIR__ . "/abc.xml", 1024);
$parser = new Parser\StringWalker();
$streamer = new XmlStringStreamer($parser, $stream);
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
//-- code here for getting single node
}
I am using XmlStringStreamer and did not get any answer from any forum, I also tried but could not get what I want so, can anyone please help me.
Thanks Alot.
I have solved it, here is my answer for it.
For looping for specific item, we can directly use this in xmlStringSteamer:
$stream = new Stream\File(__DIR__ . "/abc.xml", 1024);
$options = array(
"uniqueNode" => "SItem"
);
$parser = new Parser\UniqueNode($options);
// Create the streamer
$streamer = new XmlStringStreamer($parser, $stream);
$countNodes = 0;
while ($node = $streamer->getNode())
{
print_r($node);
}
The only problem is that it converts xml tags to lowercase. like <SItem> becomes <sitem>. So, anyone have idea about this problem?
I didn't executed your code but i think you can use PHP logic. If anything doesn't work for you then last solution will be exploding $simpleXmlNode to get some array structure and then process it according to your need. By the way can you share what you got after $simpleXmlNode = simplexml_load_string($node)?
For others, i can't add comments so posting this as answer.
I am trying to find an element using #$xpath->query() function but it return a zero length for some of the url. Here's my code what I m trying to do :
$strLink ="http://www.pennenergy.com/3bl-energy-news.html"
$arrElements = array('//div[#id="threeblmediawidget"]', '//div[#id="threeblmediadetaillist"]');
$dom = new DOMDocument();
$fileContents = file_get_contents($strLink);
#$dom->loadHTML($fileContents);
$xpath = new DOMXPath($dom);
foreach ($arrElements as $strKey => $strVal) {
$arrParams[] = #$xpath->query($strVal);
}
That code working fine for most the url except some of them. Those url have one of the id element on the source but it returns zero. I am not able to find the issue.
I have tried to use xpath checker plugin in Mozilla and it shows the result for those url but not with the code I using. if someone have any suggestion, please help.
I am trying this but its not working
include_once('simple_html_dom.php');
$handle = file_get_html('file name.php');
if (!empty($handle)) {
$ret = $handle->find('div', 0);//this is able to find the element
echo $ret;
$ret->outertext = '';//this is **NOT** deleting the video
outertext=''; is not working i dont know why
i have tried $handle->find('div[id="'.$i.'"]', 0)->outertext = '';
but this also didn't work.
any other ways I haven't tried??
I don't see why this would make a difference, but try doing your modification inline with the find.
$handle->find('div', 0)->outertext = '';
I have a php file setup to pull through ONE XML data feed, What I would like to do is load up to 4 feeds into it and if possible make it select a random item too. Then parse that into an jQuery News Ticker.
My current PHP is as follows...
<?php
$feed = new DOMDocument();
$feed->load('/feed');
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
$guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['pubdate'] = $pubDate;
$json['item'][$i++]['guid'] = $guid;
echo '<li class="news-item">'.$title.'</li>';
}
//echo json_encode($json);
?>
How can I modify this to load more than one feed into the file?
Thanks in advance
The simplest approach to doing this is wrapping another loop around the code you have. It's not the cleanest way but will probably suffice for the purpose.
In general, IMO, it's always beneficial to learn the basics of the language first. E.g. PHP manual on foreach
This is roughly what the loop needs to look like:
$my_feeds = array("http://.....", "http://.....", "http://.....");
foreach ($my_feeds as $my_feed)
{
// This is where your code starts
$feed = new DOMDocument();
$feed->load($my_feed); <--------------- notice the variable
$json = array();
... and the rest of the code
}
this will walk through all the URLs in $my_feeds, open the RSS source, fetch all the items from it, and output them.
If I'm reading your question right, what you may want to do is turn your code into a function, which you would then run inside a foreach loop for each url (which you could store in an array or other data structure).
Edit: If you don't know much about functions, this tutorial section might help you. http://devzone.zend.com/9/php-101-part-6-functionally-yours/
I am trying to use LocationSearchParameter with TargetingIdeaService (v201109). I am struggling with an Invalid_Criterion_ID error. May I request some help with that? Here is how I am setting LocationSearchParameter in php
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->locations=$LocArray; // $LocArray is array of IDs 2840 for US
As of v201702. Here's a working example.
$loc = array();
$location = new location();
$location->setId(2840); // USA
$loc[]=$location;
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->setLocations($loc);
$searchParameters[] = $locationTargetParameter;
Also if you're copying the example file found here. Don't forget to include
use Google\AdsApi\AdWords\v201702\o\LocationSearchParameter;
Since this is needed for the LocationSearchParameter to work.
As for documentation and location IDs, check here.
Here is what worked for me in case there are others looking to do the same thing:
$loc = array();
$location = new location();
$location->id = '2840';
$loc[]=$location;
$locationTargetParameter = new LocationSearchParameter();
$locationTargetParameter->locations=$loc;