PHP XPath xml file cdata error - php

I'm trying to get data from a XML file through xPath.
This is the XML:
<item>
<title>
51-årig stukket i maven: Klapper i over for politiet
</title>
<link>
http://newsbreak.dk/51-aarig-ringede-om-hjaelp-efter-knivstik-nu-tier-han/
</link>
<pubDate>Tue, 20 Jan 2015 09:19:46 +0000</pubDate>
<description>
<![CDATA[
<img width="600" height="400" src="http://newsbreak.dk/wp-content/uploads/2015/01/politi44.jpg" class="attachment-big" alt="Foto: Colourbox (genrefoto)" /><br />51-årig mand vil ikke tale med politiet, selvom han selv ringede til ordensmagten for at få hjælp til knivstik
]]>
</description>
<category>
<![CDATA[ A-historier ]]>
</category>
<category>
<![CDATA[ Krim ]]>
</category>
<guid>
http://newsbreak.dk/51-aarig-ringede-om-hjaelp-efter-knivstik-nu-tier-han/
</guid>
</item>
This is the PHP:
$titelQuery = $xpath->query("//item/title/text()");
$itemQuery = $xpath->query("//item");
$linkQuery = $xpath->query("//item/guid/text()");
$imageQuery = $xpath->query("//item/description/text()");
I get the title and link correctly , but when im trying to get the "description" part, im only getting the text after the image tag like so:
<br />51-årig mand vil ikke tale med politiet, selvom han selv ringede til ordensmagten for at få hjælp til knivstik
]]>

Just target that <description> node, then use ->nodeValue:
$imageQuery = $xpath->query("//item/description");
echo $imageQuery->item(0)->nodeValue;
Sample Output
Using ->evaluate() will work as well:
echo $xpath->evaluate('string(//item/description)');

Related

Extract some CDATA from a xml file with PHP

I've to extract some data from a xml file.
In this file there are some tags that contains CDATA content and I don't try to extract this contents.
The file is like this:
<treeplat>
<ad>
<id>965192-VR</id>
<region>VT</region>
<description>
<![CDATA[
Orte scalo. Vicinissimo alla stazione, in zona tranquilla e panoramica, proponiamo appartamento di recente costruzione così composto: ingresso, salone con ampio angolo cottura e balcone, disimpegno, camera matrimoniale con balcone, cantina e posto auto.
]]>
</description>
<pictures>
<picture>
<picture_url>
<![CDATA[
http://www.immobile.net/media/foto/1440/9f352078-885d-4281-a36d-1d22b3cbdcd9-x.jpg
]]>
</picture_url>
</picture>
<picture>
<picture_url>
<![CDATA[
http://www.immobile.net/media/foto/1440/992a5c0f-62dd-48f2-8c41-9b8ee9e06ca9-x.jpg
]]>
</picture_url>
</picture>
<picture>
<picture_url>
<![CDATA[
http://www.immobile.net/media/foto/1440/a61b4705-ed0a-494b-86fc-e92bb4c916e7-x.jpg
]]>
</picture_url>
</picture>
<picture>
<picture_url>
<![CDATA[
http://www.immobile.net/media/foto/1440/d1817d53-51fa-43dc-baf9-d3457963e694-x.jpg
]]>
</picture_url>
</picture>
<picture>
<picture_url>
<![CDATA[
http://www.immobile.net/media/foto/1440/8299cd3e-f253-4c83-9629-fb77131a2efb-x.jpg
]]>
</picture_url>
</picture>
</pictures>
</ad>
</treeplat>
I parse the xml file in this way:
$xml = simplexml_load_file(storage_path('app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$percorso_file.'test.xml'));
foreach ($xml->ad as $immobile) {
...
}
My problem is tag. I try to extract the first only using this code:
$picture_url_1 = (string)$immobile->pictures->picture->picture_url
but I don't try to extract every picture_url.
How can I parse all the pictures tag?
Many thank's!
you are looping over the wrong variable. what you need is:
$pictures = $xml->ad->pictures->picture;
foreach ($pictures as $picture) {
echo $picture->picture_url;
}
(you can replace echo with whatever command you need)
Gilad almost had it!
$pictures = $xmlData->ad->pictures;
foreach($pictures as $picture)
{
foreach($picture as $pic)
{
echo (string) $pic->picture_url;
}
}
https://3v4l.org/990NR

How to access the attribute of a element when using rss in php

I am using a RSS-feed to display some news. But the thing is that i use bootstrap and the RSS-feed send a image that i cant figure out how to access, so i can putt class="img-responsive" on it. Now the images dosent fits to the screen..
php code when i collect the elments from the RSS-feed:
<?php
$q=$_GET["q"];
$xml=("https://www.gamereactor.se/rss/rss.php?texttype=4");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
echo("<br>");
echo($channel_desc."</p>");
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $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 ("<p><a href='".$item_link."'>".$item_title."</a>");
echo ("<br>");
echo ($item_desc);
echo ("<img class=img-responsive>".$item_desc."<img></p>");} //where im trying to access the image..
?>
This how the xml from the RSS-feed looks:
<title>Nya Need for Speed kommer att kräva internetuppkoppling</title>
<description><img src="http://www.gamereactor.dk/media/55/_1455534.jpg"><p>Svenska Ghost Games pillar för fullt med kommande Need for Speed som ska släppas till PC, Playstation 4 och Xbox One den 3:e november. Förutom att vi tidigare rapporterat...</p></description>
The issue is with the data you get back. When you view the XML, you will see:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media= "http://search.yahoo.com/mrss/">
<channel>
<atom:link href="http://www.gamereactor.se/rss/rss.php?texttype=4" rel="self" type="application/rss+xml" />
<title>Gamereactor Sverige Nyheter</title>
<link>http://www.gamereactor.se</link>
<description>Dagsfärska nyheter, tunga artiklar, intervjuer, personliga bloggar, intressanta forumdiskussioner och rättvisa recensioner. Gamereactor är Sveriges största speltidning- och spelcommunity</description>
<webMaster>kenneth.gant#gamereactor.dk (Kenneth Gant)</webMaster>
<item>
<link>http://www.gamereactor.se/nyheter/244164/Alla+f%F6rhandsbokningsbonusar+till+Arkham+Knight/?rs=rss</link>
<guid>http://www.gamereactor.se/nyheter/244164/?rs=rss</guid>
<title>Alla förhandsbokningsbonusar till Arkham Knight</title>
<description><img src="http://www.gamereactor.dk/media/28/framtidensbatman_1102834.jpg"><p>En flitig Reddit-besökare har tagit på sig att lista var och till vilket format du bör reservera Batman: Arkham Knight för att få den förhandsbokningsbonus just du vill...</p></description>
<author>jonas#gamereactor.se (Jonas Mäki)</author>
<pubDate>Fri, 29 May 2015 19:44:54 +0200</pubDate>
</item>
So the IMG tag is within the description and encoded. So the following line will have this detail:
$item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
To make use of this as HTML in your page, we want to do:
$item_desc=html_entity_decode($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue);
$item_desc will now contain: <img src="http://www.gamereactor.dk/media/28/framtidensbatman_1102834.jpg"><p>En flitig Reddit-besökare har tagit på sig att lista var och till vilket format du bör reservera Batman: Arkham Knight för att få den förhandsbokningsbonus just du vill...</p>
<?php
$q=$_GET["q"];
$xml=("https://www.gamereactor.se/rss/rss.php?texttype=4");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
echo("<br>");
echo($channel_desc."</p>");
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $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=html_entity_decode($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue);
echo ("<p><a href='".$item_link."'>".$item_title."</a>");
echo ("<br>");
echo ($item_desc);
}
?>

Filtering more categories out of XML item

I would like to only get the news that has the child <category>Lifestyle</category> in it. But my code currently only gets the first category and I would like to have the code to search for all <category> in one <item> I have written this code in PHP so far:
<?php
$html = "";
$url = "http://www.nu.nl/rss";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$category = $xml->channel->item[$i]->category;
if(strpos($category, 'Binnenland')!==false) {
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "$description";
$html .= "<br />$pubDate & $category<hr />";
}
}
echo $html;
?>
Some <item> have more categories but it should be in my filtered news as well if it contains <category >Lifestyle</category> (but my code currently only finds the first one)
Part of the XML:
<item>
<title>Elsevier lijft Newsflo in</title>
<link>http://www.nu.nl/beurs/3970862/elsevier-lijft-newsflo-in.html</link>
<description>Elsevier neemt het Britse Newsflo over. Dat maakt het bedrijf maandag bekend.</description>
<pubDate>Mon, 12 Jan 2015 11:43:41 +0100</pubDate>
<guid isPermaLink="false">3970862</guid>
<enclosure url="http://media.nu.nl/m/m1mxod5a5wxc_sqr256.jpg/elsevier-lijft-newsflo-in.jpg" length="0" type="image/jpeg" />
<category>Beurs</category>
<dc:creator>ANP</dc:creator>
<dc:rights>copyright photo: ANP</dc:rights>
<atom:link href="http://nu.nl/beurs/3800685/elsevier-koopt-medische-informatieleverancier.html" type="text/html" rel="related" title="Elsevier koopt medische informatieleverancier" />
</item>
<item>
<title>'Frida Giannini krijgt geen afscheidsshow bij Gucci'</title>
<link>http://www.nu.nl/lifestyle/3970858/frida-giannini-krijgt-geen-afscheidsshow-bij-gucci.html</link>
<description>Frida Giannini, voormalig hoofdontwerpster van Gucci, heeft geen grote afscheidsshow gekregen zoals verwacht. De ontwerpster zou vrijdag stilletjes vertrokken zijn bij het label.</description>
<pubDate>Mon, 12 Jan 2015 11:41:52 +0100</pubDate>
<guid isPermaLink="false">3970858</guid>
<enclosure url="http://media.nu.nl/m/m1mxn43acqev_sqr256.jpg/frida-giannini-krijgt-geen-afscheidsshow-bij-gucci.jpg" length="0" type="image/jpeg" />
<category>Lifestyle</category>
<dc:creator>NU.nl</dc:creator>
<dc:rights>copyright photo: Wenn</dc:rights>
<atom:link href="http://nu.nl/lifestyle/3970265/alessandro-michele-nieuwe-hoofdontwerper-gucci.html" type="text/html" rel="related" title="'Alessandro Michele nieuwe hoofdontwerper Gucci'" />
<atom:link href="http://nu.nl/lifestyle/3951514/hoofdontwerpster-en-directeur-vertrekken-bij-gucci.html" type="text/html" rel="related" title="Hoofdontwerpster en directeur vertrekken bij Gucci" />
<atom:link href="http://nu.nl/lifestyle/3783917/hoofdontwerpster-gucci-spreekt-geruchten-vertrek.html" type="text/html" rel="related" title="Hoofdontwerpster Gucci spreekt geruchten vertrek tegen" />
</item>
<item>
<title>Militairen met buitenlandse partner naar rechter</title>
<link>http://www.nu.nl/binnenland/3970856/militairen-met-buitenlandse-partner-rechter.html</link>
<description>Een aantal militairen met een buitenlandse partner stapt naar de rechter omdat zij al maanden geschorst thuis zitten. De schorsing is het gevolg van het feit dat hun partner niet gescreend kan worden.</description>
<pubDate>Mon, 12 Jan 2015 11:41:24 +0100</pubDate>
<guid isPermaLink="false">3970856</guid>
<enclosure url="http://media.nu.nl/m/m1oxcv7anu4p_sqr256.jpg/militairen-met-buitenlandse-partner-rechter.jpg" length="0" type="image/jpeg" />
<category>Binnenland</category>
<category>Lifestyle</category>
<dc:creator>ANP</dc:creator>
<dc:rights>copyright photo: ANP</dc:rights>
</item>
I think I made a mistake in the PHP but I do not know how to work around it...
Select only desired <item>s with xpath():
$xml = simplexml_load_string($x); // assume XML in $x
$items = $xml->xpath("//item[category='Lifestyle']");
//item will select all item nodes, regardless of their position in the tree, the condition is in [], $item is an array containing SimpleXML Elements.
Output:
foreach ($items as $item) echo $item->title . PHP_EOL;
see it working: https://eval.in/241693

PHP - Parse XML contained within XML element and Output into XML doc

I'm looking to parse out only the content of the element below into its own XML document, but am unsure of the proper PHP method to use. XML data in boxb.php is unable to be modified.
EX:
Parsing code:
<?php
include 'boxb.php';
$boxb = new SimpleXMLElement($xmlstr);
$boxb->ad[0]->content;
echo $boxb->ad[0]->content;
?>
boxb.php contains the following:
<?php
$xmlstr = <<<XML
<boxb>
<ad type="agnostic_template">
<url><![CDATA[http://ads.cookie.com/8/redir/1db04901-225e-11e4-86f3-bc305bf4914b/0/632361]]></url>
<track />
<content>
<VAST version="2.0">
<Ad id="228">
<InLine>
<AdSystem version="4.11.0-10">LiveRail</AdSystem>
<AdTitle><![CDATA[TV Overlay PNG]]></AdTitle>
<Description />
<Error><![CDATA[http://t4.liverail.com/?metric=error&erc=[ERRORCODE]&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=&redirect=]]></Error>
<Impression id="LR"><![CDATA[http://t4.liverail.com/?metric=impression&cofl=0&flid=0&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=29&y=29&xy=9dae&z2=0.00000]]></Impression>
<Impression id="QC"><![CDATA[http://pixel.quantserve.com/pixel/p-d05JkuPGiy-jY.gif?r=6662]]></Impression>
<Impression id="CS"><![CDATA[http://b.scorecardresearch.com/p?c1=1&c2=9864668&c3=1331&c4=&c5=09]]></Impression>
<Impression><![CDATA[http://load.exelator.com/load/?p=104&g=440&j=0]]></Impression>
<Impression><![CDATA[http://navdmp.com/usr?vast=http%3A%2F%2Ft4.liverail.com%2F%3Fmetric%3Dmsync%26p%3D78]]></Impression>
<Impression><![CDATA[http://pixel.tapad.com/idsync/ex/receive?partner_id=LIVERAIL&partner_device_id=97838239447]]></Impression>
<Impression><![CDATA[http://t4.liverail.com/?metric=rsync&p=3016&redirect=http%3A%2F%2Fliverail2waycm-atl.netmng.com%2Fcm%2F%3Fredirect%3Dhttp%253A%252F%252Ft4.liverail.com%252F%253Fmetric%253Dcsync%2526p%253D3016%2526s%253D(NM-UserID)]]></Impression>
<Impression><![CDATA[http://t4.liverail.com/?metric=rsync&p=3017&redirect=http%3A%2F%2Fm.xp1.ru4.com%2Factivity%3F_o%3D62795%26_t%3Dcm_rail]]></Impression>
<Impression><![CDATA[http://n.us1.dyntrk.com/adx/lr/sync_lr.php?lrid=97838239447]]></Impression>
<Creatives>
<Creative sequence="1" id="8455">
<NonLinearAds>
<NonLinear width="300" height="60">
<StaticResource creativeType="image/png"><![CDATA[http://cdn.liverail.com/adasset/228/8455/overlay.png]]></StaticResource>
<NonLinearClickThrough><![CDATA[http://t4.liverail.com/?metric=clickthru&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=&redirect=http%3A%2F%2Fwww.liverail.com]]></NonLinearClickThrough>
</NonLinear>
<TrackingEvents>
<Tracking event="acceptInvitation"><![CDATA[http://t4.liverail.com/?metric=accept&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=]]></Tracking>
<Tracking event="collapse"><![CDATA[http://t4.liverail.com/?metric=minimize&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=]]></Tracking>
</TrackingEvents>
</NonLinearAds>
</Creative>
<Creative sequence="1" id="8455">
<CompanionAds>
<Companion width="300" height="60">
<StaticResource creativeType="image/jpeg"><![CDATA[http://cdn.liverail.com/adasset/228/8455/300x60.jpg]]></StaticResource>
<TrackingEvents>
<Tracking event="creativeView"><![CDATA[http://t4.liverail.com/?metric=companion&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=]]></Tracking>
</TrackingEvents>
<CompanionClickThrough><![CDATA[http://t4.liverail.com/?metric=cclickthru&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=&redirect=http%3A%2F%2Fwww.liverail.com]]></CompanionClickThrough>
</Companion>
<Companion width="300" height="250">
<StaticResource creativeType="image/jpeg"><![CDATA[http://cdn.liverail.com/adasset/228/8455/300x250.jpg]]></StaticResource>
<TrackingEvents>
<Tracking event="creativeView"><![CDATA[http://t4.liverail.com/?metric=companion&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=]]></Tracking>
</TrackingEvents>
<CompanionClickThrough><![CDATA[http://t4.liverail.com/?metric=cclickthru&pos=1&coid=135&pid=1331&nid=1331&oid=228&olid=2281331&cid=8455&tpcid=&vid=&amid=&cc=default&pp=&vi=0&vv=&sg=&tsg=&pmu=0&pau=0&psz=0&ctx=&tctx=&coty=7&adt=0&did=&buid=&scen=&mca=&mma=&mct=0&url=http%3A%2F%2Fwww.iab.net%2Fguidelines%2F508676%2Fdigitalvideo%2Fvast%2Fvast_xml_samples&trid=53ea9957dc20a5.06241648&bidf=0.00000&bids=0.00000&bidt=1&bidh=0&bidlaf=0&cb=6662.66.104.228.162.0&ver=1&w=&wy=&x=&y=&xy=&redirect=http%3A%2F%2Fwww.liverail.com]]></CompanionClickThrough>
</Companion>
</CompanionAds>
</Creative>
</Creatives>
<Extensions />
</InLine>
</Ad>
</VAST>
<!-- 321 US_NEWJERSEY_NEWYORK_METUCHEN 08840 -->
</content>
</ad>
</boxb>
XML;
?>
If you want to get the XML-data, use asXML(),
echo $boxb->ad[0]->content->asXML();
And if you want to create your own XML document, you could use for example,
$myXML = new SimpleXMLElement($boxb->ad[0]->content->asXML());
echo $myXML->asXML();
Which would echo,
<!--?xml version="1.0"?-->
<content>
...
</content>
<?php
include 'boxb.php';
// Load string and parse as XML
$boxb = simplexml_load_string($xmlstr);
// Extract "content" element from loaded XML
$content = $boxb->ad->content;
// Convert extracted info into XML
$new_xml = $content->asXML();
// Send a header tag to the browser, stating that this info is XML
header('Content-type: application/XML');
// Print the actual XML
echo $new_xml;
?>

Wordpress posts to xml

I have a project where i have to link values given the wordpress backend to xmls, since i am new to Worpress i don't know where to start.
So the xml looks like this
<?xml version="1.0"?>
<articles>
<article>
<title><![CDATA[Teddiedrum - The Audience]]>
</title>
<image><![CDATA[http://niteversions.com/uploads/Teddiedrum-The-Audience-490x490.jpg]]>
</image>
<content>
<![CDATA[Oei.]]>
</content>
<date>
<![CDATA[16/04/2012]]>
</date>
</article>
<article>
<title><![CDATA[Soap&Skin - Boat Turns Toward The Port ]]>
</title>
<image><![CDATA[http://neuundgut.zib21.com/wp-content/uploads/2011/12/soap-skin-boat-turns-towards-the-boat-video.jpg]]>
</image>
<content>
<![CDATA[Elke aanleiding moéten we gebruiken om deze magistrale Soap&Skin-song te posten. We geraken immers nog steeds niet uit de wurggreep van ‘Boat Turns Toward The Port’. De 22-jarige Anja Plaschg slaat ons, op amper 2:30, compleet murw met dit hartverscheurend, schuifelend treurlied. Alle haren overeind, woensdagavond in AB, Brussel.]]>
</content>
<date>
<![CDATA[17/04/2012]]>
</date>
</article>
<article>
<title><![CDATA[Mr. Scruff Vs Kirsty Almeida - Pickled Spider]]>
</title>
<image><![CDATA[http://ecx.images-amazon.com/images/I/41HRpRNhG-L.jpg]]>
</image>
<content>
<![CDATA[De dolgedraaide Ninja Tuner Mr Scruff palmt vrijdag - van 20u30 tot 2u (!) - het podium van de Brusselse VK* in. Mail snel naar filip.rifraf#skynet.be met als subject Mr. Scruff voor één van de 3 gratis duotickets. De winnaars maken we hier donderdag bekend. “Maar als je niet tegen vette bassen kan, ben blijf je beter thuis”, waarschuwt hij. Doe de bass-test met deze ‘Pickled Spider’.]]>
</content>
<date>
<![CDATA[18/04/2012]]>
</date>
</article>
<article>
<title><![CDATA[DJ Muggs feat Dizzee Rascal & Bambu - Snap Ya Neck Back]]>
</title>
<image><![CDATA[http://www.danceeuphoria.com/wp-content/uploads/2012/03/djmuggssnapyaneckback.jpg]]>
</image>
<content>
<![CDATA[Een goed jaar geleden op de RifRaf-cover, nu bruutweg een banaan molesterend. Tsssss.]]>
</content>
<date>
<![CDATA[18/04/2012]]>
</date>
</article>
<article>
<title><![CDATA[Steak Number Eight - Dickhead]]>
</title>
<image><![CDATA[http://userserve-ak.last.fm/serve/500/6144813/Steak+Number+Eight++010.jpg]]>
</image>
<content>
<![CDATA[Een goed jaar geleden op de RifRaf-cover, nu bruutweg een banaan molesterend. Tsssss.]]>
</content>
<date>
<![CDATA[19/04/2012]]>
</date>
</article>
<article>
<title><![CDATA[The Heart Is A Drum - Drums Are For Parades feat. Tim Vanhamel]]>
</title>
<image><![CDATA[http://www.liegecity.be/wp-content/uploads/2012/03/DrumsAreForParade.jpg]]>
</image>
<content>
<![CDATA[De 'IMPERIVM’-picture disc van Drums Are For Parades is één van de vele, unieke releases nav Record Store Day, nu zaterdag. Drums Are For Parades sluiten trouwens die hoogdag van de onafhankelijke platenwinkels ’s avonds live af in Handelsbeurs, Gent. Uit ‘IMPERIVM’, deze ‘The Heart Is A Drum’ feat. Tim Vanhamel.]]>
</content>
<date>
<![CDATA[20/04/2012]]>
</date>
</article>
</articles>
What i want is to make fields in the backend like title, image, content, date and everytime this field is saved it adds a new node to the xml.
Any information on how to start doing this / the steps i have to undertake would be welcome.
Kind regards
Toon
There's already 2 tools available in wordpress import and export xml in backend just install that and you'll get what you want there's no need to do any custom thing on every post but if you still want it then read about 'publish_post' hook
As mentioned, your best bet would be to search existing plugins. I would start with this one and maybe see about modifying it to your needs:
http://wordpress.org/extend/plugins/backwpup/

Categories