2019-05-22 02:56
采纳率: 0%
浏览 111


I'm trying to get an RSS feed, change some text, and then serve it again as an RSS feed. However, the code I've written doesn't validate properly. I get these errors:

line 3, column 0: Missing rss attribute: version

line 14, column 6: Undefined item element: content (10 occurrences)

Here is my code:

header("Content-type: text/xml");

echo "<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl'?>
<?xml-stylesheet type='text/xsl' media='screen'                 
<rss xmlns:content=''>

<description>Blog RSS</description>

$html = "";
$url = "";
$xml = simplexml_load_file($url);

for ($i = 0; $i < 10; $i++){
$title = $xml->channel->item[$i]->title;
$description = $xml->channel->item[$i]->description;
$content = $xml->channel->item[$i]->children("content", true);
$content = preg_replace("/The post.*/","", $content);

echo "<item>

echo "</channel></rss>";

图片转代码服务由CSDN问答提供 功能建议

我正在尝试获取RSS提要,更改一些文本,然后再次将其作为RSS提要提供。 但是,我写的代码没有正确验证。 我收到这些错误:


第14行,第6列:未定义 item元素:content(10次出现)


header  (“Content-type:text / xml”); 
echo“&lt;?xml version ='1.0'coding ='UTF-8'?&gt; 
&lt;?xml-stylesheet type ='text / xsl'  ?&gt; 
&lt;?xml-stylesheet type ='text / xsl'media ='screen'
href ='/ ~d / styles / rss2full.xsl'?&gt; 
&lt; rss xmlns:content ='http:  //'>

<description>Blog RSS&lt; / description&gt; 
&lt; language&gt;  en-us&lt; / language&gt; 
 $ html =”“; 
 $ url =”";
$xml = simplexml_load_file  ($ url); 
for($ i = 0; $ i&lt; 10; $ i ++){
 $ title = $ xml-&gt; channel-&gt; item [$ i]  - &gt; title; \  n $ description = $ xml-&gt; channel-&gt; item [$ i]  - &gt; description; 
 $ content = $ xml-&gt; channel-&gt; item [$ i]  - &gt;  children(“content”,true); 
 $ content = preg_replace(“/ The post。* /”,“”,$ content); 
echo“&lt; item&gt; 
&lt; title&gt; $ title&lt; /  title&gt; 
&lt; description&gt; $ description&lt; / description&gt; 
&lt; content&gt; $ content&lt; / content&gt; 
&lt; / item&gt;“; 
echo”&lt; / channel&gt;&lt; /  rss&gt;“; 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhong1891 2019-05-22 03:53

    Just as you don't treat XML as a string when parsing it, you don't treat it as as string when you create it. Use the proper tools to create your XML; in this case, the DomDocument class.

    You had a number of problems with your XML; biggest is that you were creating a <content> element, but the original RSS had a <content:encoded> element. That means the element name is encoded but it's in the content namespace. Big difference between that and an element named content. I've added comments to explain the other steps.

    // create the XML document with version and encoding
    $xml = new DomDocument("1.0", "UTF-8");
    $xml->formatOutput = true;
    // add the stylesheet PI
            'type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"'
    // create the root element
    $root = $xml->appendChild($xml->createElement('rss'));
    // add the version attribute
    $v = $root->appendChild($xml->createAttribute('version'));
    // add the namespace
    // create some child elements
    $ch = $root->appendChild($xml->createElement('channel'));
    // specify the text directly as second argument to
    // createElement because it doesn't need escaping
    $ch->appendChild($xml->createElement('title', 'Blaakdeer'));
    $ch->appendChild($xml->createElement('description', 'Blog RSS'));
    $ch->appendChild($xml->createElement('language', 'en-us'));
    $url = "";
    $rss = simplexml_load_file($url);
    for ($i = 0; $i < 10; $i++) {
        if (empty($rss->channel->item[$i])) {
        $title = $rss->channel->item[$i]->title;
        $description = $rss->channel->item[$i]->description;
        $content = $rss->channel->item[$i]->children("content", true);
        $content = preg_replace("/The post.*/","", $content);
        $item_el = $ch->appendChild($xml->createElement('item'));
        $title_el = $item_el->appendChild($xml->createElement('title'));
        // this stuff is unknown so it has to be escaped
        // so have to create a separate text node
        $desc_el = $item_el->appendChild($xml->createElement('description'));
        // the other alternative is to create a cdata section
        // the content:encoded element is not the same as a content element
        // the element must be created with the proper namespace prefix
        $cont_el = $item_el->appendChild(
    header("Content-type: text/xml");
    echo $xml->saveXML();
    点赞 打赏 评论
  • douxiangbiao1899 2019-05-22 03:13

    The first error is just a missing attribute, easy enough:

    <rss version="2.0" ...>

    For the <p> and other HTML elements, you need to escape them. The file should look like this:


    There are other ways, but this is the easiest way. In PHP you can just call a function to encode entities.

    $output .= htmlspecialchars(" <p>Paragraph</p> ");

    As for the <content> tag problem, it should be <description> instead. The <content> tag currently generates two errors. Changing it to <description> in both places should fix both errors.

    Otherwise it looks like you understand the basics. You <open> and </close> tags and those have to match. You can also use what is called empty tags: <empty/> which exist on their own but to not include content and no closing tag.

    点赞 打赏 评论

相关推荐 更多相似问题