Advertisement
Guest User

XML parsing, Go, V1

a guest
Feb 14th, 2012
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.24 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.         "encoding/xml"
  5.         "fmt"
  6.         "io"
  7.         "log"
  8.         "os"
  9.         "time"
  10. )
  11.  
  12. func main() {
  13.         file, e := os.Open("SearchRequest.xml")
  14.         if e != nil { log.Fatal(e) }
  15.         decoder := xml.NewDecoder(file)
  16.         var nodeName string
  17.         for t, e := decoder.Token(); e != io.EOF; t, e = decoder.Token() {
  18.                 switch t.(type) {
  19.                 case xml.StartElement:
  20.                         nodeName = t.(xml.StartElement).Name.Local
  21.                 case xml.EndElement:
  22.                         nodeName = ""
  23.                 case xml.CharData:
  24.                         date := string(t.(xml.CharData))
  25.                         if nodeName == "created" {
  26.                                 // Thu, 1 Dec 2011 07:26:59 -0500
  27.                                 t, e := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", date)
  28.                                 if e != nil {
  29.                                         fmt.Fprintf(os.Stderr, "Unable to parse %s\n", date)
  30.                                 } else {
  31.                                         fmt.Printf(t.Format("2006/01/02 15:04:05\n"))
  32.                                 }
  33.                         }
  34.                 }
  35.         }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement