2017-10-28 14 views
1

何も返さないアンマーシャリング:XML解析/私は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リクエストのレスポンスであり、すべてのタグ値は必要ありません。特定のタグ値のみを取得できますか?または、値を取得するためにすべての構造体を作成する必要がありますか?

+0

あなたはどのような値をしたいですか? – md2perpe

+0

すべて「

+0

答えて

2
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

関連する問題