[objective-c] SMXMLDocument 사용법

GPX Tracker에는 GDataXML 이라는 라이브러리가 사용되는데, 좀 어렵다.

왜 선택했는지 기억도 안난다.

좋은거 있는지 찾다가 쉬워보이는 걸 찾았다.

 

source : https://github.com/nfarina/xmldocument

blog post : http://nfarina.com/post/2843708636/a-lightweight-xml-parser-for-ios

 

<gpx>
  <trk>
    <name>화악산</name>
    <trkseg>
      <trkpt lon="127.51106221228838" lat="37.81410447321832">
        <ele>36.0</ele>
        <time>2013-09-07T00:28:30.000Z</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:atemp>23.0</gpxtpx:atemp>
            <gpxtpx:hr>102</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
      <trkpt lon="127.51106388866901" lat="37.81410682015121">
        <ele>36.0</ele>
        <time>2013-09-07T00:28:31.000Z</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:atemp>23.0</gpxtpx:atemp>
            <gpxtpx:hr>102</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
    </trkseg>
  </trk>
</gpx>

 

NSError *error;

NSString *filePath = [[NSBundle mainBundle] bundlePath];
NSString *path = [[NSString alloc] initWithString:[filePath stringByAppendingPathComponent:@"activity_371809874.gpx"]];
NSData *data = [NSData dataWithContentsOfFile:path];
SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
SMXMLElement *trk = [document.root childNamed:@"trk"];
NSString* name = [trk valueWithPath:@"name"];

SMXMLElement *trkseg = [trk childNamed:@"trkseg"];
NSArray* trkpts = [trkseg childrenNamed:@"trkpt"];
SMXMLElement* trkpt = [trkpts objectAtIndex:0];

NSLog(@"name : %@, %@, %@",name, [trkpt attributeNamed:@"lon"], [trkpt attributeNamed:@"lat"]);
// name : 화악산, 127.51106221228838, 37.81410447321832