我怎么能加载一个xml,并用PHP保存?

时间:2015-01-11 07:38:34

标签: php xml

嘿伙计我试图解析xml(feed)并用php保存它?(我中途),我已经实现加载xml并显示我需要显示的数据,。现在如何保存xml?

这是我的代码:

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

?>

代码运行良好,从我想要显示的xml加载标签, 之后我想做的是创建一个xml(如下面的结构化)并保存数据。 我怎么能实现这个目标?

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>

2 个答案:

答案 0 :(得分:1)

阅读文档http://php.net/manual/en/simplexmlelement.asxml.php

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

修改

如果您想创建一个新的xml,则会有一个答案https://stackoverflow.com/a/143192/1742977

答案 1 :(得分:0)

嗯,你可以使用正常的字符串连接来编写xml文件(或者你可以花时间用xml对象来做这件事,有些人可能会考虑更合适)。

接下来,在写入模式下打开fopen()的文件。如果该文件不存在,则将创建该文件。确保您有权写入该目录,否则将无法写入文件(如果错误报告严格,则会抛出致命错误,否则只会发出警告)。第二,您使用fwrite()写入文件。有关详细信息,请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);

或者,您可以使用simplexml_load_string()将字符串加载为xml,然后使用asXml()将其写入磁盘,如下所示:

$data = simplexml_load_string($data);
$data->asXml($file);