简体   繁体   中英

How to print element values when iterating through XML document in PHP

I am iterating through the results of a service call to yahoo news thus:

//Send service request
    if (!$yahooResults = file_get_contents($yahooRequest)) {
      echo 'Error processing service request';
    }

    //Read result into xml document
    $yahooResultXml = new DOMDocument('1.0', 'UTF-8');
    $yahooResultXml->loadXML($yahooResults);

    //Build page
    include_once('components/pageHeader.php');
    echo 'Search Results';
    //echo $yahooResultXml->saveHTML();
    //Iterate over each Result node
    $stories = $yahooResultXml->getElementsByTagName('Result');
    foreach ($stories as $story) {
      //Title
      //Summary
      //Url
      //Source
      //Language
      //Publish Date
      //Modification Date
    }

    include_once('components/pageFooter.php');

Each Title is in a Title node within a Result Node. I cannot figure out how to simply echo the content of the Title node!

Check the first user comment at PHP: DOMDocument::getElementsByTagName

$items = $doc->getElementsByTagName('item'); $headlines = array();

 foreach($items as $item) { $headline = array(); if($item->childNodes->length) { foreach($item->childNodes as $i) { $headline[$i->nodeName] = 

$i->nodeValue; } }

  $headlines[] = $headline; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM