0

Given the following

<data type="new">
     <time>1</time>
     <time>2</time>
</data>

 <data type="old">
      <time>3</time>
      <time>4</time>
 </data

The goal being to type something along the following

   <?php 
    $test = simplexml_load_file('http://....')
    echo $test->data['type="old"']->time[0] //I want this to return 3
   ?>

and get the value of 3! I hate tried using attributes but I'm still on the search for the solution.

Thanks, JTC

The actually doc I'm trying to parse from

<data type="current observations">
    <time-layout time-coordinate="local">
        <start-valid-time period-name="current">2013-05-27T13:53:00-04:00</start-valid-time>
    </time-layout>
</data>

I attempted the following but got an Invalid expression warning.

  $weather = simplexml_load_file('http://...');
  $time=$weather->xpath('//data[@type="current observations"]/"time-layout"/"start-valid-   time"');
  echo $time[0];
4
  • possible duplicate of Help parse XML to PHP by attribute value Commented May 27, 2013 at 18:45
  • I would agree they are similar but I'm interested in the array value. Commented May 27, 2013 at 19:10
  • the questions are identical, whether you know it or not. Commented May 27, 2013 at 19:57
  • They looked similar to my lack of presenting the question better. I have sence updated it! Commented May 27, 2013 at 20:03

2 Answers 2

1

Here's a code snippet using SimpleXMLElement and XPath. I've added a root datas node so the XML could be parsed, I'm guessing yours is well formed.

<?php
$xml = <<<XML
<datas>
    <data type="new">
        <time>1</time>
        <time>2</time>
    </data>
    <data type="old">
        <time>3</time>
        <time>4</time>
    </data>
</datas>
XML;

$sxe  = new SimpleXMLElement($xml);
$time = $sxe->xpath('//data[@type="old"]/time');
echo $time[0];

Output

3
Sign up to request clarification or add additional context in comments.

3 Comments

the XML file is located external to the file that's why i was using simplexml_load_file() im not sure of how to use the impleXMLElement()
Read the official documentation of SimpleXMLElement _construct, you'll notice that it accepts external URLs and local paths. Just do something like $sxe = new SimpleXMLElement('http://....', 0, true);.
I updated the bottom of my question to better reflect my problem! I tried your suggestion with no luck but this may due to my highly lacking knowledge of parsing.
0

OK, taking your actual XML document which is well-formed, you could do this...

$data = $weather->xpath('/data[@type="current observations"]');
$time_layout = $data[0]->xpath('time-layout[@time-coordinate="local"]');
$start_valid_time = $time_layout[0]->xpath('start-valid-time[@period-name="current"]');
echo (string)$start_valid_time[0];

See it in action here.

4 Comments

being that the file is not actually in the document ie I'm pulling the data in. Can i use the simplexml_load_string() just like the simplexml_load_file()
You don't need to change that line. We are using different functions for demo purposes only. The line you want is the next one.
when trying to use your code the [0]->time[0] gave me an error! I also updated the above question to better reflect the problem I'm having.
Sorry @tman, my original syntax was only working in latest versions of PHP. I've updated with better syntax supported in 5.0.0 upwards. You can see here.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.