0

I have a big number of xml-files. It is files with my gps-tours. I need to select ONE pair of number from every file. The format is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<name>T2022-02-02-13-52</name>  <bounds minlat="55.63957603" minlon="12.45321142" maxlat="55.71344546" maxlon="12.50049124"/></metadata><trk>
<trkseg>
    <trkpt lat="55.6401" lon="12.49971159">
        <ele>13.5</ele><time>2022-02-02T08:39:56Z</time>
    </trkpt>
    <trkpt lat="55.64005171" lon="12.49973769">
         <ele>1.6</ele><time>2022-02-02T08:40:07Z</time>
    </trkpt>

-and 7000 lines in this file. It is the degree in the xml-file: lat an lon i need. In this file is 55.6401 and 12.49971159 in an array. Can you help with the code using php.

compac
  • 21
  • 1
  • 3
  • 1
    Does this answer your question? [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – user3783243 Aug 24 '22 at 15:48

1 Answers1

0

i wrote the PHP script to read from each huge file first 400 characters and to grab lat and lon..

for the test i used xampp windows : index.php

<?php

$dir='.';
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {

            if( ($file!='index.php')and($file!='.')and($file!='..') and (pathinfo($file)['extension']=='xml')){
                //echo $file.'<br>';
                $f=fopen($file,'r');

                $read400= fread($f,400);//  read partial file 'cause is huge
                //echo $read400.'<hr>';


                $lat_sta=strpos($read400,'trkpt lat="');//11 " " -2
                $lat_stp=strpos($read400,'" lon="',$lat_sta+11);
                //echo substr($read400,$lat_sta+11,$lat_stp-$lat_sta-11);echo '<br>';
                $lat=substr($read400,$lat_sta+11,$lat_stp-$lat_sta-11);
                $lon_sta=strpos($read400,'lon="',$lat_stp);
                $lon_stp=strpos($read400,'"',$lon_sta+5);
                //echo substr($read400,$lon_sta+5,$lon_stp-$lon_sta-5);echo '<br>';
                $lon=substr($read400,$lon_sta+5,$lon_stp-$lon_sta-5);
                $data[]=[$lat,$lon];
                fclose($f);
            }
        }
        closedir($dh);
    }
}
isset($data)&& var_dump($data);
?>

and file1.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<name>T2022-02-02-13-52</name>  <bounds minlat="55.63957603" minlon="12.45321142" maxlat="55.71344546" maxlon="12.50049124"/></metadata><trk>
<trkseg>
    <trkpt lat="55.6401" lon="12.49971159">
        <ele>13.5</ele><time>2022-02-02T08:39:56Z</time>
    </trkpt>
    <trkpt lat="55.64005171" lon="12.49973769">
         <ele>1.6</ele><time>2022-02-02T08:40:07Z</time>
    </trkpt>

and file2.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<name>T2022-02-02-13-52</name>  <bounds minlat="55.63957603" minlon="12.45321142" maxlat="55.71344546" maxlon="12.50049124"/></metadata><trk>
<trkseg>
    <trkpt lat="55.601" lon="12.4959">
        <ele>13.5</ele><time>2022-02-02T08:39:56Z</time>
    </trkpt>
    <trkpt lat="55.64005171" lon="12.49973769">
         <ele>1.6</ele><time>2022-02-02T08:40:07Z</time>
    </trkpt>

the result is:

array(2) { [0]=> array(2) { [0]=> string(7) "55.6401" [1]=> string(11) "12.49971159" } [1]=> array(2) { [0]=> string(6) "55.601" [1]=> string(7) "12.4959" } } 
Makyen
  • 31,849
  • 12
  • 86
  • 121