0

I'm pretty new to PHP and XML and have got myself stuck on one particular problem with an xml feed. In the XML data there is one field "article_content" with a unique attribute (id) for every news story. I need to be able to just display this story on a page based on a URL created from the index page which shows all stories (example of url is path/to/file/newsstory.php?storyid=19837775) where storyid matches the id attribute in the article content field.

Can anyone help as I'm beating my head off the wall here!


update:

The XML is in this format below (New article_content for each story)

<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 code I've got is:

<?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());
}
?>
2
  • Please add the XML and the code you have .... SimpleXML is probably the way forward ....
    – Manse
    Jan 27, 2012 at 11:08
  • The XML is in this format below (New article_content for each story) <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 code I've got is: <?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()); } ?>
    – Pablo100
    Jan 27, 2012 at 11:19

2 Answers 2

0

Without your XML i can only show this example (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;

To output the type attribute :

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

Example here

0

First an example how it might be done (using XPath and php'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;
}

prints

<story article_content="19837775">content 19837775</story>

....but this might not be a good solution after all. It depends and what you're actually trying to achieve (and how scalable the solution must be).
Please elaborate.


update: an example using the actual data structure.

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

prints

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

You might also be interested in https://en.wikipedia.org/wiki/XSLTXSL which also uses XPath for selecting nodes but let's you transform the output (e.g. from your xml representation to a user-friendly html format).

6
  • Sorry for not making it too clear (I'm very new at this!) The "article_content" node has child nodes under it, so what I'm trying to do is to select only the article content node (and children) where the id attribute is from the url so that I can display the content of various nodes under that (does that make sense?)
    – Pablo100
    Jan 27, 2012 at 11:50
  • Yes, and the example does that ...more or less. The variable $story "is" the story element you're looking for (it's the handle/reference to the DOM representation of that DOMElement). I.e. you can access child node from there. But there are some issues esp. concerning scalability. Loading the xml data takes time. And putting it into the DOM object also takes time. If your application must be scalable you should consider a really good caching mecahnism. Or chose another storage format. Or use an xml/document based database system. Or a mix of all of the above.
    – VolkerK
    Jan 27, 2012 at 11:59
  • Thanks for that, I was thinking that feeding the XML into a database would be the better option!
    – Pablo100
    Jan 27, 2012 at 12:01
  • I'm now lost, I've tried that and I get the following error: 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 on line 66
    – Pablo100
    Jan 27, 2012 at 12:27
  • Strange ...since the demo script doesn't even have 66 lines ;-) In case you're using the getData() function make sure there are no characters (not even a whitespace) before the eox; and no characters except a linebreak/carriage-return after it. see docs.php.net/manual/de/…
    – VolkerK
    Jan 27, 2012 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.