1

hey guys I'm trying to parse a xml(feeds) and save it with php?(I'm halfway),.i already achieve to load the xml and display just the data i need to display,.now how can i save the xml?

this is my code :

 <?php

$html = "";
$url = "http://www.conciencia.net/rss.aspx";
$xml = simplexml_load_file($url);

$channel_title = $xml->channel->title;
$channel_link = $xml->channel->link;
$managingEditor = $xml->channel->managingEditor;
$channel_description = $xml->channel->description;
$lbd = $xml->channel->lastBuildDate;

$html .= "<br/>$channel_title";
$html .= "<br/>$channel_link";
$html .= "<br/>$managingEditor";
$html .= "<br/>$lbd"; 
$html .= "<br/>$channel_description<br/>";

for($i = 0; $i < 7; $i++){

    $pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
    $guid = $xml->channel->item[$i]->guid;
    $author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;
    $url0 = $xml->channel->item[$i]->enclosure->attributes()->url;

    for($ii = 0; $ii < 2; $ii++){
    $url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
   }

    $html .= "<br/>$pubDate";     
    $html .= "<a href='$link'><h3>$title</h3></a>";
    $html .= "<br/>$guid";
    $html .= "<br/>$author";
    $html .= "<br/>$description";
    $html .= "<br/>$url0";
    $html .= "<br/>$url";
    $html .= "<br/>$link<br/>"; 
}

echo $html;

?>

that code is working good, loading the tags from the xml i just want to display, what i want to do after that is create a xml(as is below structured) and save the data. how can i achieve that ?

myxml.xml

<channel>
                <title>$channel_title</title>
                <link>$channel_link</link>
                <managingEditor>$managingEditor</managingEditor>
                <channel_description>$channel_description</channel_description>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
                <item>
                       <title>$title</title>
                       <guid>$guid</guid>
                       <author>$author</author>
                       <description>$description</description>
                       <enclosure0>$url0</enclosure0>
                       <enclosure1>$url</enclosure>

                </item>
</channel>
Mr. Mol
  • 29
  • 6
  • You should escape your variables in HTML using `htmlspecialchars()`. – Ja͢ck Jan 11 '15 at 07:45
  • Possible duplicate of http://stackoverflow.com/questions/3418376/how-to-save-changed-simplexml-object-back-to-file – Aram Tchekrekjian Jan 11 '15 at 07:46
  • why do you need to write xml? you haven't actually modified anything. you've just taken some xml properties and output them with your html. – r3wt Jan 11 '15 at 07:53
  • you can create xml as a string, then write it to an xml file as a string using filesystem functions. no special method is required. – r3wt Jan 11 '15 at 07:56

2 Answers2

1

Read the docs http://php.net/manual/en/simplexmlelement.asxml.php

$xml->asXml('rss.xml');

Edit

and if you want to create a new xml there is an answer https://stackoverflow.com/a/143192/1742977

Community
  • 1
  • 1
Gregsparrow
  • 1,332
  • 1
  • 11
  • 13
  • 1
    Reference from docs: "filename If specified, the function writes the data to the file rather than returning it." Reference from user post: "now how can i save the xml?" Where I am wrong ? – Gregsparrow Jan 11 '15 at 07:51
  • hey guys I've updated the question, adding more code, trying to be more clear on my question, and what I'm trying to do. – Mr. Mol Jan 11 '15 at 07:55
1

Well you can simly use normal string concatenation to write the xml file(alternatively you could spend time doing it the hard way with an xml object, which some might consider more proper).

Next you open a file with fopen() in write mode. if the file doesn't exist, it will be created. make sure you have permission to write to that directory, or the file will not be written(and if error reporting is strict, a fatal error will be thrown, else just a warning). second, you use fwrite() to write to the file.for more information, read the manual on filesystem functions

$file = '/path/to/mynewxmlfile.xml';
$data = 
'
<?xml version="1.0"?>
<channel>
<title>'.$channel_title.'</title>
<link>'.$channel_link.'</link>
<managingEditor>'.'$managingEditor.</managingEditor>
<channel_description>'.$channel_description.'</channel_description>
';

for($i = 0; $i < 7; $i++){

    $pubDate = $xml->channel->item[$i]->pubDate;
    $title = $xml->channel->item[$i]->title;
    $link = $xml->channel->item[$i]->link;
    $guid = $xml->channel->item[$i]->guid;
    $author = $xml->channel->item[$i]->author;
    $description = $xml->channel->item[$i]->description;
    $url0 = $xml->channel->item[$i]->enclosure->attributes()->url;

    for($ii = 0; $ii < 2; $ii++){
        $url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
    }
    $data .=
    '
    <item>
    <title>'.$title.'</title>
    <guid>'.$guid.'</guid>
    <author>'.$author.'</author>
    <description>'.$description.'</description>
    <enclosure0>'.$url0.'</enclosure0>
    <enclosure1>'.$url.'</enclosure1>
    </item>
    ';

}
$data .= '</channel>';

fwrite(fopen($file,'w'),$data);

Alternatively, you could use simplexml_load_string() to load the string as xml, then use asXml() to write it to disk, like so:

$data = simplexml_load_string($data);
$data->asXml($file);
r3wt
  • 4,642
  • 2
  • 33
  • 55
  • 1
    thanks Bro you're the man !,that works!,.. i just still have to figure out how to decode the : " é " (html entities i believe). any idea ? – Mr. Mol Jan 13 '15 at 00:50