php - Accessing a single XML DOM Document node -
i new dom documents, trying do, load rss feed in , select 1 node, , save xml file.
here xml loading web feed:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>markets</title> <description/> <link>http://www.website.com</link> <language>en-us</language> <copyright>xml output copyright</copyright> <ttl>15</ttl> <pubdate>tue, 16 nov 2010 09:38:00 +0000</pubdate> <webmaster>admin@website.com</webmaster> <image> <title>title</title> <url>http://www.website.com/images/xmllogo.gif</url> <link>http://www.website.com</link> <width>144</width> <height>16</height> </image> <item> <title>title</title> <description>the description goes here </description> <enclosure url="http://www.website.com/images/image.png" type="image/png"/> </item> </channel> </rss>
here lame attempt @ getting <description>
node , saving feed.xml:
<?php $feed = new domdocument(); $feed->load('http://www.website.com/directory/directory/cz.c'); $nodevalue = $feed->getelementsbytagname('description')->item(0)->nodevalue; $feed->save("feed.xml"); ?>
so need description tag, , save xml file.
any appreciated, thanx in advance!
almost correct. "outerxml" of node, can pass node savexml()
$feed = new domdocument(); $feed->load('http://www.website.com/directory/directory/cz.c'); $xml = $feed->savexml($feed->getelementsbytagname('description')->item(0)); file_put_contents("feed.xml", $xml);
saving file_put_contents
not include xml prolog. note in example, first description element empty, file contain <description/>
.
if want extract node standalone xml document, have instantiate new domdocument
, import domnode
, use save()
.
$dom = new domdocument($feed->xmlversion, $feed->xmlencoding); $dom->appendchild( $dom->importnode( $feed->getelementsbytagname('description')->item(0), true ) ); echo $dom->save('new.xml');
Comments
Post a Comment