简体   繁体   中英

how can i load a xml, and save it with php?

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>

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM