duanaidang6197 2017-10-28 20:41
浏览 21
已采纳

XML解析/解组不返回任何内容

I need some values from xml:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:consultaPontoResponse 
xmlns:ns2="http://ws.consultaponto.senior.com/">
         <return>
            <clock>
               <date>
                  <day>28</day>
                  <month>10</month>
                  <year>2017</year>
               </date>
               <time>
                  <hour>9</hour>
                  <minute>14</minute>
               </time>
            </clock>
            <clock>
               <date>
                  <day>28</day>
                  <month>10</month>
                  <year>2017</year>
               </date>
               <time>
                  <hour>11</hour>
                  <minute>51</minute>
               </time>
            </clock>
            <clock>
               <date>
                  <day>28</day>
                  <month>10</month>
                  <year>2017</year>
               </date>
               <time>
                  <hour>12</hour>
                  <minute>4</minute>
               </time>
            </clock>
            <clock>
               <date>
                  <day>28</day>
                  <month>10</month>
                  <year>2017</year>
               </date>
               <time>
                  <hour>12</hour>
                  <minute>27</minute>
               </time>
            </clock>
            <workedTime>
               <hour>3</hour>
               <minute>0</minute>
            </workedTime>
            <currentDateTime>
               <date>
                  <day>28</day>
                  <month>10</month>
                  <year>2017</year>
               </date>
               <time>
                  <hour>13</hour>
                  <minute>16</minute>
               </time>
            </currentDateTime>
         </return>
      </ns2:consultaPontoResponse>
   </S:Body>
</S:Envelope>

And I create some structs to unmarshall :

type Envelope struct {
    Body Body
}

type Body struct {
    Consulta Consulta `xml: "consultaPontoResponse"`
}

type Consulta struct {
   Return Return
}

type Clock struct {
   Time Time
}

type Return struct {
    Clock []Clock
}

type Time struct {
   Hour string
   Minute string
}

So I dont receive nothing when use :

xmlEnvelope := &Envelope{}
xml.Unmarshal(sonataXml, xmlEnvelope)

The return of xml.Unmarshal(sonataXml, xmlEnvelope) is a empty object. This xml is a response of SOAP request and I don't need all the tag values. Can I get only specific tag values? Or do I need to create all structs to get values?

  • 写回答

1条回答 默认 最新

  • doudouwen2763 2017-10-28 21:34
    关注
    package main
    
    import (
        "encoding/xml"
        "fmt"
    )
    
    var data string = `<?xml version="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
       <S:Body>
          <ns2:consultaPontoResponse 
    xmlns:ns2="http://ws.consultaponto.senior.com/">
             <return>
                <clock>
                   <date>
                      <day>28</day>
                      <month>10</month>
                      <year>2017</year>
                   </date>
                   <time>
                      <hour>9</hour>
                      <minute>14</minute>
                   </time>
                </clock>
                <clock>
                   <date>
                      <day>28</day>
                      <month>10</month>
                      <year>2017</year>
                   </date>
                   <time>
                      <hour>11</hour>
                      <minute>51</minute>
                   </time>
                </clock>
                <clock>
                   <date>
                      <day>28</day>
                      <month>10</month>
                      <year>2017</year>
                   </date>
                   <time>
                      <hour>12</hour>
                      <minute>4</minute>
                   </time>
                </clock>
                <clock>
                   <date>
                      <day>28</day>
                      <month>10</month>
                      <year>2017</year>
                   </date>
                   <time>
                      <hour>12</hour>
                      <minute>27</minute>
                   </time>
                </clock>
                <workedTime>
                   <hour>3</hour>
                   <minute>0</minute>
                </workedTime>
                <currentDateTime>
                   <date>
                      <day>28</day>
                      <month>10</month>
                      <year>2017</year>
                   </date>
                   <time>
                      <hour>13</hour>
                      <minute>16</minute>
                   </time>
                </currentDateTime>
             </return>
          </ns2:consultaPontoResponse>
       </S:Body>
    </S:Envelope>`
    
    type Time struct {
        Hour   int `xml:"hour"`
        Minute int `xml:"minute"`
    }
    
    func (t Time) String() string {
        return fmt.Sprintf("%02d:%02d", t.Hour, t.Minute)
    }
    
    type Return struct {
        Times []Time `xml:"Body>consultaPontoResponse>return>clock>time"`
    }
    
    func main() {
        var r Return
    
        err := xml.Unmarshal([]byte(data), &r)
        if err != nil {
            panic(err)
        }
    
        fmt.Println("Times:")
        for _, t := range r.Times {
            fmt.Println("*", t)
        }
    }
    

    https://play.golang.org/p/Y9u3Q3YrSi

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭