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

标签 xml go xml-parsing unmarshalling

我需要一些来自 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>

然后我创建了一些结构来解码:

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
}

所以我在使用时没有收到任何东西:

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

xml.Unmarshal(sonataXml, xmlEnvelope) 的返回是一个空对象。此 xml 是 SOAP 请求的响应,我不需要所有标记值。我可以只获取特定的标签值吗?还是我需要创建所有结构来获取值?

最佳答案

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

关于XML 解析/解码不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46994280/

相关文章:

java - 如何在 EditText 密码输入字段旁边放置 ImageView

go - 如何在 Golang 中为字符串类型创建方法

php - 使用 php xPath 更改先前的同级值

mysql - 使用 sql.DB 的无效内存地址或 nil 指针取消引用

go - 较旧的服务传输较新版本的 Protocol Buffer 3 消息

oracle - PL/SQL XML 解析为关系表

java - java 如何判断没有捕获到异常

Perl,如何解析 XML 文件,xpath

java - CreateTextNode转义大文本字符串中的字符

xml - 检查XML DOM XMLDOC时出错