XML Parsing with etree


Would appreciate any assistance in explaining why I get an error running this simple XML GETter and etree parser:

package main

import (

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 {
	defer response.Body.Close()

	//Convert the body to array of uint8
	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {

	// 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: ")

	doc := etree.NewDocument()
	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]:
        /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!

I’d start by checking the error return from doc.ReadFromString.


Thanks! What a silly oversight that was. I went rushing in to try out etree without reading the docs well enough… anyway…

Yes, it was returning a:
Got error: xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil

So two options appeared to me:

  1. Simply remove the iSO declaration line which was derailing Go from using a standard UTF-8 charset for the decode. This works perfectly well for the trivial use case I’m testing.
  2. Change the decoder. This post helps with that:

Many thanks.

1 Like

The advantage of getting used to a language without exceptions is that it feels wrong when a call looks like it might fail and is not error checked. The read call sticks out because it parses XML and should likely be able to fail. :slight_smile:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.