简体   繁体   English

如何加载xml,并用php保存?

[英]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? 嗨,我正在尝试解析xml(feeds)并用php保存它?(我是中途),.我已经实现了加载xml并仅显示我需要显示的数据。现在如何保存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. 该代码运行良好,从我只想显示的xml加载标签,之后要做的是创建一个xml(如下结构)并保存数据。 how can i achieve that ? 我该如何实现?

myxml.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>

Read the docs http://php.net/manual/en/simplexmlelement.asxml.php 阅读文档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 如果要创建新的xml,则有一个答案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). 好了,您可以简单地使用普通的字符串连接来编写xml文件(或者,您可以花一些时间用xml对象的困难方式完成该工作,有些人可能认为这样做更合适)。

Next you open a file with fopen() in write mode. 接下来,在写模式下使用fopen()打开文件。 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 其次,使用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);

Alternatively, you could use simplexml_load_string() to load the string as xml, then use asXml() to write it to disk, like so: 另外,您可以使用simplexml_load_string()将字符串加载为xml,然后使用asXml()将其写入磁盘,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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