benhowdle89
benhowdle89

Reputation: 37504

change xml element with PHP

I have this xml output:

    <?xml version="1.0" encoding="UTF-8"?>
    <ysearchresponse xmlns="http://www.inktomi.com/" responsecode="200">
      <nextpage>/ysearch/news/v1/madonna?format=xml&amp;count=10&amp;appid=Zo5W4RbV34Fit1YXV.U6.wULh5WHjqh_XVB7Vutu0Z1gojo2sdv0hAlGoDVnUtSfZto-&amp;start=10</nextpage>
      <resultset_news count="10" start="0" totalhits="567" deephits="567">
        <result>
          <abstract>Is Madonna flexing her muscles at her former trainer, Tracy Anderson? The Material Girl recently announced a plan to open a series of Hard Candy...</abstract>
          <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTVjbXN2aXU4BF9TAzIwMjMxNTI3MDIEYXBwaWQDWm81VzRSYlYzNEZpdDFZWFYuVTYud1VMaDVXSGpxaF9YVkI3VnV0dTBaMWdvam8yc2R2MGhBbEdvRFZuVXRTZlp0by0EY2xpZW50A2Jvc3MEc2VydmljZQNCT1NTBHNsawN0aXRsZQRzcmNwdmlkA1N2Y2pHbUtJY3JxVDVqUnFZTEtpVnlURlJWcWlGRXpwSWRzQUFlN0w-/SIG=13nbl173k/**http%3A//www.nypost.com/p/pagesix/is_madonna_flexing_her_muscles_at_GrzN4FaNXrwcL5NiURPauO%3FCMP=OTC-rss%26FEEDNAME=</clickurl>
          <date>2010/11/15</date>
          <language>english</language>
          <source>New York Post</source>
          <sourceurl>http://www.nypost.com/</sourceurl>
          <time>06:47:42</time>
          <title>Madonna's gym revenge</title>
          <url>http://www.nypost.com/p/pagesix/is_madonna_flexing_her_muscles_at_GrzN4FaNXrwcL5NiURPauO?CMP=OTC-rss&amp;FEEDNAME=</url></result>
        <result>
</resultset_news></ysearchresponse>

which i got from my php code:

    header("Content-type: text/xml");
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
echo $results->asXML();

But what i need to do is, change the XML element "abstract" to "description". ie, instead of <abstract></abstract> i would have <description></description> How would i go about doing that?

Upvotes: 0

Views: 324

Answers (2)

lonesomeday
lonesomeday

Reputation: 238065

You can use the regex solution that ajreal gives, but you shouldn't use regex to match HTML. It would be nicer to use PHP's DOM API.

header("Content-type: text/xml");

//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';

//Create DOMDocument and load data
$dom = new DOMDocument;
$dom->load($url);

//Replace <abstract/> with <description/>
$abstract = $dom->getElementsByTagName('abstract')->item(0);
$description = $dom->createElement('description');
$description->nodeValue = $abstract->nodeValue;
$abstract->parentNode->replaceChild($description, $abstract);

// Output XML
echo $dom->saveXML();

This will be much more robust method. You could extend it to duplicate attributes as well if necessary.

Upvotes: 1

ajreal
ajreal

Reputation: 47331

Easiest way is to

$data = str_replace( array('<abstract>', '</abstract>'), array('<description>', '</description>'), $data);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
..

Or you can use regexp, or anything other string replacement methods

Upvotes: 2

Related Questions