How can I regulate data from a text file with java?

127 Views Asked by At

To be more specific ; I have a xml file which has a lots of letters and gps data like this one ;

</Satellites>
</PVTInfo>
<PVTInfo ValidFlags="14327" Time="51973,3" LastTime="51973,1"
 Latitude="38,7525418333333" Longitude="35,5297423333333" 
 EllipsoidHeight="1120"
 GeoidHeight="1089,9" Speed="0,0166666666666667" Heading="338,9" 
 MagneticVariation="-5,19999999999999" Depth="0" PDOP="1,476482" HDOP="0,7"
 VDOP="0,7" TDOP="0" Status="GPSStatusTypeDOINGFIXES" Mode="GPSModeType3D">
<Satellites>
  <SatInfo PRN="69" Elevation="70" Azimuth="240" SNR="43" Used="True" />
  <SatInfo PRN="28" Elevation="61" Azimuth="188" SNR="44" Used="True" />
  <SatInfo PRN="5" Elevation="41" Azimuth="282" SNR="47" Used="True" />
  <SatInfo PRN="10" Elevation="35" Azimuth="202" SNR="47" Used="True" />
  <SatInfo PRN="7" Elevation="45" Azimuth="53" SNR="45" Used="True" />
  <SatInfo PRN="9" Elevation="31" Azimuth="123" SNR="39" Used="True" />
  <SatInfo PRN="74" Elevation="28" Azimuth="172" SNR="35" Used="True" />
  <SatInfo PRN="91" Elevation="13" Azimuth="62" SNR="31" Used="True" />
  <SatInfo PRN="30" Elevation="69" Azimuth="10" SNR="47" Used="True" />
  <SatInfo PRN="78" Elevation="33" Azimuth="324" SNR="39" Used="True" />
  <SatInfo PRN="72" Elevation="63" Azimuth="347" SNR="39" Used="True" />
  <SatInfo PRN="13" Elevation="25" Azimuth="310" SNR="37" Used="True" />
  <SatInfo PRN="39" Elevation="36" Azimuth="139" SNR="37" Used="False" />
  <SatInfo PRN="85" Elevation="34" Azimuth="264" SNR="38" Used="False" />
  <SatInfo PRN="19" Elevation="15" Azimuth="59" SNR="33" Used="False" />
  <SatInfo PRN="20" Elevation="12" Azimuth="181" SNR="38" Used="False" />
</Satellites> 

The file is like 35 pages. So I have lots of lines to procces. All I need is point number, Latitude and Longitude. I'm using Map Info to procces this data so I need these information in this format ;

"Number [TAB] Latitude [TAB] Longitude"

Otherwise Map Info cant proccess this data.

Thanks in advance...

3

There are 3 best solutions below

0
On BEST ANSWER

There are many ways to read and parse XML. Take a look at these Java XML tutorials. DOM parsing is simpler but you'll need enough memory to hold the entire document (XML file). If this is a recurring need then it's worth spending the time to learn JAXB, otherwise I'd just cobble together something by following an example.

See also this SO question - there's sample code in one of the answers.

0
On

You could use XQL (Xml Query Language) using Java XPath. This is the shortest way.

Alternatively you could convert (or look for) an XSD file which defines the structure of your XML file. And convert it to a bunch of java classes, using JAXB (Java Xml Binding). Which results in the most readable code.

Another option is to load your file using a DocumentBuilder (DOM). Less boiler plate code.

All 3 options have (dis)advantages. It's a matter of flexibility, dependencies and personal taste.

0
On

I've seen the answers and I've decided the best way to do this in Java is DOM.(in my case) But for learning different languages I've write my own Map Basic code. So it goes like this ;

Dim str As String, posLat As Integer, posLat1 As Integer, posLat2 As String, posLon As Integer, posLon1 As Integer, posLon2 As String, i As Integer
Open File "C:\Users\Capan\Desktop\Hüseyin\geotrack Log.xml" For Input As #1 
Open File "C:\Users\Capan\Desktop\Coordinates3.txt" For Output As #2

Do While Not EOF(1)
    Line Input #1, str

    If Not EOF(1) Then
        posLon = InStr(1,str,"Longitude")
        posLat = InStr(1,str,"Latitude")

     If posLon > 0 and posLat > 0 Then 
          posLon1 = posLon + 11
          posLon2 = Mid$(str,posLon1,8)
         posLat1 = posLat+10
         posLat2 = Mid$(str,posLat1,8)
     End If
        Print #2, " "+posLat2+" "+posLon2

    End If
Loop 
Close File #1
Close File #2