如何加载 XML,并使用 PHP 保存它


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

嘿伙计们,我正在尝试解析 xml(feeds) 并使用 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>

好吧,你可以简单地使用普通的字符串连接来编写xml文件(或者,你可以花时间用xml对象来做这件事,有些人可能会认为这更合适)。

接下来,在写入模式下打开一个包含fopen()的文件。 如果该文件不存在,则将创建该文件。 确保您有权写入该目录,否则不会写入该文件(如果错误报告很严格,则会抛出致命错误,否则只是警告)。 其次,使用 fwrite() 写入文件。有关详细信息, 阅读文件系统功能手册

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

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

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

编辑

如果你想创建一个新的XML,有一个答案 https://stackoverflow.com/a/143192/1742977