douxiangbiao1899 2013-02-18 22:35
浏览 58
已采纳

解析XML命名空间节点

I am relatively new to Go but struggling with a basic task. I am parsing a GPX file using the "encoding/xml" package and have the following example xml data;

<gpx creator="StravaGPX" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
 <metadata>
  <time>2013-02-16T10:11:25Z</time>
 </metadata>
 <trk>
  <name>Demo Data</name>
  <trkseg>
   <trkpt lat="51.6395658" lon="-3.3623858">
    <ele>111.6</ele>
    <time>2013-02-16T10:11:25Z</time>
    <extensions>
     <gpxtpx:TrackPointExtension>
      <gpxtpx:atemp>8</gpxtpx:atemp>
      <gpxtpx:hr>136</gpxtpx:hr>
      <gpxtpx:cad>0</gpxtpx:cad>
     </gpxtpx:TrackPointExtension>
    </extensions>
   </trkpt>

So far I am able to get at the most basic info with the following types;

type Gpx struct {
    Creator string `xml:"creator,attr"`
    Time string `xml:"metadata>time"`
    Title string `xml:"trk>name"`
    TrackPoints []TrackPoint `xml:"trk>trkseg>trkpt"`
}

type TrackPoint struct {
    Lat float64 `xml:"lat,attr"`
    Lon float64 `xml:"lon,attr"`
    Elevation float32 `xml:"ele"`
    Time string `xml:"time"`
    Temperature int `xml:"extensions>gpxtpx>atemp"`
}

Where I am failing to parse is in TrackPoint.Temperature. I cannot get the tag right to match the namespaced nodes underneath extensions.

Any suggestions as to where I am going wrong?

  • 写回答

1条回答 默认 最新

  • douchuopiao9002 2013-02-18 22:57
    关注

    You're targeting a namespace instead of a variable name in your TrackPoint.Temperature xml instructions.

    Change

    Temperature int `xml:"extensions>gpxtpx>atemp"`
    

    To

    Temperature int `xml:"extensions>TrackPointExtension>atemp"`
    

    Here's a working copy in the playground.

    package main
    
    import (
        "fmt"
        "encoding/xml"
        )
    
    type Gpx struct {
        Creator string `xml:"creator,attr"`
        Time string `xml:"metadata>time"`
        Title string `xml:"trk>name"`
        TrackPoints []TrackPoint `xml:"trk>trkseg>trkpt"`
    }
    
    type TrackPoint struct {
        Lat float64 `xml:"lat,attr"`
        Lon float64 `xml:"lon,attr"`
        Elevation float32 `xml:"ele"`
        Time string `xml:"time"`
        Temperature int `xml:"extensions>TrackPointExtension>atemp"`
    }
    
    func main() {
        data := `<gpx creator="StravaGPX" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" 
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
     <metadata>
      <time>2013-02-16T10:11:25Z</time>
     </metadata>
     <trk>
      <name>Demo Data</name>
      <trkseg>
       <trkpt lat="51.6395658" lon="-3.3623858">
        <ele>111.6</ele>
        <time>2013-02-16T10:11:25Z</time>
        <extensions>
         <gpxtpx:TrackPointExtension>
          <gpxtpx:atemp>8</gpxtpx:atemp>
          <gpxtpx:hr>136</gpxtpx:hr>
          <gpxtpx:cad>0</gpxtpx:cad>
         </gpxtpx:TrackPointExtension>
        </extensions>
       </trkpt>
      </trkseg>
     </trk>
    `
        g := &Gpx{}
        _ = xml.Unmarshal([]byte(data), g)
        fmt.Printf("len: %d
    ", len(g.TrackPoints))
        fmt.Printf("temp: %v
    ", g.TrackPoints[0].Temperature)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里