ranell
ranell

Reputation: 703

how to filter XML

I'm looking for filtering this xml enter link description here The best way to filter an xml it is : -to run all the xml and to affect value in variable -after that we rewrite this xml with this variable

is there any other method? For this method i've used the dom like this:

    $flux= new DOMDocument();
if ($flux->load('http://xml.weather.com/weather/local/FRXX0076?unit=m&hbhf=6&ut=C'))  
{$loc=$flux->getElementsByTagName('t');
  foreach($loc as $lo)
    echo $lo->firstChild->nodeValue . "<br />";
}

in this code i've tried to display <t> but there are 2 balise <t> in <hour> , therefore i've two value of <t> instead the first child of <hour>

Upvotes: 0

Views: 206

Answers (1)

Tomasz Struczyński
Tomasz Struczyński

Reputation: 3303

The brief answer:

$flux= new DOMDocument();
if ($flux->load('http://xml.weather.com/weather/local/FRXX0076?unit=m&hbhf=6&ut=C'))  
{
    $xpath = new DomXPath($flux);

    $elements = $xpath->query("*/hour/t[1]");

    if (!is_null($elements)) {
      foreach ($elements as $element)
      {
        echo "<br/>[". $element->nodeName. "]";

        $nodes = $element->childNodes;
        foreach ($nodes as $node)
        {
          echo $node->nodeValue. "\n";
        }
      }
    }
}

I think you should know where to go from this point :)

More advanced version will be to issue XPath query on all hour elements ("*/hour") and then in foreach for each hour element issue another xpath query in this element context ($xpath->query("*/t[1]", $hourElement);). This way you'll also have access to hour object and can for example display this hour.

UPDATE

Simpler version of foreach:

if (!is_null($elements)) {
    foreach ($elements as $element)
    {
        echo "<br/>".$element->nodeValue;
    }
}

Upvotes: 1

Related Questions