1

I’m fairly new to using RegEx to obtain information from XML files. I am trying to get the main information from each section to display in an array but cannot figure it out. I would appreciate any help possible for this.

I think I have split each section that I would like to return information from but I am not sure on how to put this all together in order to display the resulting array in a table.

The XML I am using can be found here: National Roads Weather Station Data http://data.tii.ie/Datasets/Its/DatexII/WeatherData/Content.xml

The RegEx that I have created for the parts I want to use are:

Road ID

(NRA\d+)

Date & Time

(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2})

Air Temperature

<airTemperature>(\d{1,2}.\d{1})<\/airTemperature>

Wind Direction

<windDirectionCompass>(\w+)<\/windDirectionCompass>

Wind Speed

<windSpeed>(\d{1,3}.\d{1})<\/windSpeed>

Road Temperature

<roadSurfaceTemperature>(\d{1,3}.\d{1})<\/roadSurfaceTemperature>

If anyone could show me how this would work I would really appreciate it as I am trying to develop my coding skills into new areas. Thanks!

Ryan
  • 23
  • 5
  • 8
    Why are you not using a XML parser? https://www.w3schools.com/Php/php_xml_simplexml_read.asp – Chin Leung Feb 17 '17 at 16:16
  • @ChinLeung As I am fairly new to this I have plenty to learn and discover! I will have a look at this, thank you! – Ryan Feb 17 '17 at 16:21
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?rq=1 – AbraCadaver Feb 17 '17 at 16:22

1 Answers1

0

Yes as @Chin Leung pointed out an XML Parser would be the best way to handle this.

Here is a way you can handle this:

$xml = file_get_contents('http://data.tii.ie/Datasets/Its/DatexII/WeatherData/Content.xml');
        $xml_object = simplexml_load_string($xml);
        $publication = $xml_object->payloadPublication;
        $siteMeasurements = $publication->siteMeasurements;
        foreach($siteMeasurements as $siteMeasurement)
        {
            $road_id = str_replace('NRA', '', $siteMeasurement->measurementSiteReference);
            etc..
        }

using http://us2.php.net/manual/en/function.simplexml-load-string.php

and http://us2.php.net/manual/en/function.file-get-contents.php

Billy Stalnaker
  • 146
  • 1
  • 1
  • 10