我是PHP和XML的新手,并且已经陷入了xml feed的一个特定问题。在XML数据中,每个新闻故事都有一个字段“article_content”,其中包含唯一属性(id)。 我需要能够在一个页面上显示这个故事,该页面基于从索引页面创建的URL,该页面显示所有故事(url的例子是path / to / file / newsstory.php?storyid = 19837775)其中storyid与id匹配文章内容字段中的属性。
任何人都可以提供帮助,因为我在这里击败了我的头!
更新
下面采用以下格式的XML(每个故事的新article_content)
<channel>
<article_content id="19837775" status="A">
<title>title of article 1</title>
<date>20120127</date>
<time>10:18:00</time>
<body>main body of story 1 here</body>
<introduction>intro text here</introduction>
<abstract></abstract>
<by_line></by_line>
<category_id>0103</category_id>
</article_content>
[...]
我得到的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());
}
?>
答案 0 :(得分:0)
如果没有您的XML,我只能展示此示例(taken from here):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;
输出type属性:
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->rating[0]['type']
答案 1 :(得分:0)
首先举例说明如何
<?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
您可能也对XPath XSL感兴趣,它也使用XPath选择节点,但让您转换输出(例如从您的xml表示转换为用户友好的html格式)。