在PHP中基于属性显示XML文件中的特定数据

在PHP中基于属性显示XML文件中的特定数据,php,xml,xml-parsing,Php,Xml,Xml Parsing,我对PHP和XML还不太熟悉,我一直在解决XML提要的一个特殊问题。在XML数据中有一个字段“article_content”,每个新闻故事都有一个唯一的属性(id)。 我需要能够根据从索引页创建的URL在页面上显示这个故事,该URL显示所有故事(URL的示例是path/to/file/newstory.php?storyid=19837775),其中storyid与article content字段中的id属性匹配 有人能帮我吗?我在这里把头从墙上摔下来 更新: XML的格式如下(每个故事的

我对PHP和XML还不太熟悉,我一直在解决XML提要的一个特殊问题。在XML数据中有一个字段“article_content”,每个新闻故事都有一个唯一的属性(id)。 我需要能够根据从索引页创建的URL在页面上显示这个故事,该URL显示所有故事(URL的示例是path/to/file/newstory.php?storyid=19837775),其中storyid与article content字段中的id属性匹配

有人能帮我吗?我在这里把头从墙上摔下来


更新:


XML的格式如下(每个故事的新文章内容)


第1条的标题
20120127
10:18:00
故事的主体在这里
介绍文字在这里
0103
[...]
我得到的php代码是:

<?php
$xml = new SimpleXMLElement($rss);
$results = array ();
foreach ($xml->channel->article_content[id] as $item) {
    echo "<h3>".$item->title."</h3>";
    echo nl2br ($item->body->asXML());
}
?>

没有XML,我只能展示这个示例(:

首先举例说明如何完成(使用和


请添加XML和您拥有的代码……可能是前进的方向……XML的格式如下(每个故事的新文章内容)第1篇的标题20120127 10:18:00故事1的主体在这里介绍文本在这里0103 php代码我得到的是:抱歉没有说得太清楚(我在这方面很新!)“article_content”节点下有子节点,因此我尝试只选择id属性来自url的article content节点(和子节点),以便我可以在该节点下显示各个节点的内容(这有意义吗?)是的,示例或多或少会这样做。变量
$story
”是“您正在查找的story元素(它是该DomeElement的DOM表示形式的句柄/引用)也就是说,你可以从那里访问子节点,但也存在一些问题。特别是关于可扩展性。加载XML数据需要时间。并且将其放入DOM对象还需要时间。如果你的应用程序必须是可伸缩的,你应该考虑一个非常好的缓存McAcNeNISM。或者选择另一种存储格式。或者使用基于XML/文档的数据库系统。谢谢,我想把XML输入数据库是更好的选择!我现在迷路了,我试过了,我得到了以下错误:解析错误:语法错误,意外的$end,期望T_变量或T_end_HEREDOC或T_DOLLAR_OPEN_CURLY_括号或T_CURLY_OPEN in C:\xampp\htdocs\testfeed\ne第66行的wsflow\newsstory.php很奇怪……因为演示脚本甚至没有66行;-)如果您使用
getData()
函数,请确保
eox;
前面没有字符(甚至没有空格),后面除了换行符/回车符之外没有字符。请参阅
<?php
$xml = new SimpleXMLElement($rss);
$results = array ();
foreach ($xml->channel->article_content[id] as $item) {
    echo "<h3>".$item->title."</h3>";
    echo nl2br ($item->body->asXML());
}
?>
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->rating[0]['type']
<?php
$id = '19837775'; // this is the parameter you'd fetch from the url

$doc = new DOMDocument;
//$doc->load('feed.xml');
$doc->loadxml( getData() );

$xpath = new DOMXPath($doc);
foreach ( $xpath->query('story[@article_content="'.$id.'"]') as $story ) {
    echo $doc->savexml($story); 
}


function getData() {
    return <<< eox
<stories>
    <story article_content="1">content 1</story>
    <story article_content="2">content 2</story>
    <story article_content="19837775">content 19837775</story>
    <story article_content="22222222">content 22222222</story>
</stories>  
eox;
}
<story article_content="19837775">content 19837775</story>
<?php
$id = '19837775'; // the parameter fetched from the request

$doc = new DOMDocument;
//$doc->load('feed.xml');
$doc->loadxml( getData() );

$xpath = new DOMXPath($doc);

$article = firstNode( $xpath->query('article_content[@id="'.$id.'"]') );
if ( !$article ) {
    die('no such article');
}

$title = firstNode( $xpath->query('title', $article) );
$date = firstNode( $xpath->query('date', $article) );
$body = firstNode( $xpath->query('body', $article) );

// TODO: check $title, $data and $body

echo 'title: ', $title->nodeValue, "\n";
echo 'date: ', $title->nodeValue, "\n";
echo 'body: ', $doc->savexml($body), "\n";
echo 'body->nodeValue: ', $body->nodeValue;
die;

function firstNode($nodelist) {
    if ( $nodelist instanceof DOMNodeList && 0<$nodelist->length ) {
        return $nodelist->item(0);
    }
    return false;
}

function getData() {
    return <<< eox
<channel>
    <article_content id="1" status="X"></article_content>
    <article_content id="19837775" status="A">
        <title>title of article 1</title><date>20120127</date><time>10:18:00</time>
        <body><h1>main body of story 1</h1><p>Not just plain text</p></body>
        <introduction>intro text here</introduction><abstract></abstract><by_line></by_line><category_id>0103</category_id>
    </article_content>
    <article_content id="20000000" status="X"></article_content>
    <article_content id="20000001" status="X"></article_content>
</channel>
eox;
}
title: title of article 1
date: title of article 1
body: <body><h1>main body of story 1</h1><p>Not just plain text</p></body>
body->nodeValue: main body of story 1Not just plain text