【问题标题】:Displaying specific data from an XML file based on an attribute in PHP基于 PHP 中的属性显示 XML 文件中的特定数据
【发布时间】:2012-01-27 11:05:56
【问题描述】:

我对 PHP 和 XML 还很陌生,并且遇到了一个与 xml 提要有关的特定问题。在 XML 数据中,每个新闻报道都有一个字段“article_content”,该字段具有唯一的属性 (id)。 我需要能够根据从索引页面创建的 URL 在页面上显示这个故事,该页面显示所有故事(url 的示例是 path/to/file/newsstory.php?storyid=19837775),其中 storyid 匹配 id文章内容字段中的属性。

任何人都可以帮忙,因为我在这里把我的头从墙上敲下来!


更新:

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

<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());
}
?>

【问题讨论】:

  • 请添加 XML 和您拥有的代码 ....SimpleXML 可能是前进的方向 ....
  • XML 格式如下(每个故事的新文章内容) 文章 1 的标题20120127 这里是故事1的主体 这里是介绍文字0103 我得到的php代码是: channel->article_content[id] as $item) {echo "

    ".$item->title."

    "; echo nl2br ($item->body->asXML()); } ?>

标签: php xml xml-parsing


【解决方案1】:

没有你的 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;

输出类型属性:

$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->rating[0]['type']

Example here

【讨论】:

    【解决方案2】:

    首先是一个示例可能如何完成(使用XPathphp's DOM extension

    <?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>
    

    ....但这毕竟可能不是一个好的解决方案。这取决于您实际想要实现的目标(以及解决方案的可扩展性)。
    请详细说明。


    update:使用实际数据结构的示例。

    <?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
    

    您可能还对https://en.wikipedia.org/wiki/XSLTXSL 感兴趣,它也使用 XPath 来选择节点,但让我们转换输出(例如,从您的 xml 表示形式到用户友好的 html 格式)。

    【讨论】:

    • 抱歉没有说得太清楚(我对此很陌生!)“article_content”节点下有子节点,所以我要做的是只选择文章内容节点(和子节点),其中 id 属性来自 url,以便我可以在其下显示各种节点的内容(这有意义吗?)
    • 是的,这个例子就是这样做的……或多或少。变量$story“是”您正在寻找的故事元素(它是该DOMElement 的DOM 表示的句柄/引用)。 IE。您可以从那里访问子节点。但是有一些问题,尤其是。关于可扩展性。加载 xml 数据需要时间。并且将其放入 DOM 对象中也需要时间。如果你的应用程序必须是可扩展的,你应该考虑一个非常好的缓存机制。或者选择另一种存储格式。或者使用基于 xml/文档的数据库系统。或以上所有内容的混合。
    • 谢谢,我认为将 XML 输入数据库会是更好的选择!
    • 我现在迷路了,我已经尝试过了,但出现以下错误: Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_END_HEREDOC or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN in C:\xampp\ htdocs\testfeed\newsflow\newsstory.php 在第 66 行
    • 奇怪 ...因为演示脚本甚至没有 66 行 ;-) 如果您使用 getData() 函数,请确保之前没有字符(甚至没有空格) eox; 之后没有字符,除了换行符/回车符。见docs.php.net/manual/de/…
    猜你喜欢
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多