Hi,
Would appreciate any assistance in explaining why I get an error running this simple XML GETter and etree parser:
package main
import (
"strings"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/beevik/etree"
)
func main() {
// Set up the URL
url := "http://w1.weather.gov/xml/current_obs/KRIC.xml"
// Execute GET then close the body
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
//Convert the body to array of uint8
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// Convert the body to a string
responseString:= string(responseData)
responseString = strings.TrimSpace(responseString)
// Print the string
fmt.Println("\nPrinting the xml response as a plain string: ")
fmt.Println(responseString)
doc := etree.NewDocument()
doc.ReadFromString(responseString)
tt := doc.FindElement(".//latitude")
fmt.Println("Title:", tt.Tag, tt.Text() )
}
I get a
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x11f4fab]
goroutine 1 [running]:
main.main()
/Users/user/go/src/xml_demo.go:42 +0x48b
I should add that when I attempt the same procedure in etree using a local string (exactly that returned from the online source), it works just fine.
I must be missing something in the preparation of the XML response before passing to etree for parsing?
Many thanks!