// http://golang.org/pkg/io/ioutil/// http://golang.org/pkg/encoding/xml/packagemainimport("io/ioutil""encoding/xml""fmt")typedivstruct{XMLNamexml.Name`xml:"div"`// First letter must be capital. Cannot use `content`Contentstring`xml:",chardata"`}funcmain(){d:=div{}xmlContent,_:=ioutil.ReadFile("example-1.xml")err:=xml.Unmarshal(xmlContent,&d)iferr!=nil{panic(err)}fmt.Println("XMLName:",d.XMLName)fmt.Println("Content:",d.Content)}
Put the above two files on the same directory and run the code. The ouput will
be:
XMLName:{div}
Content:Example
Note
In the line:
XMLNamexml.Name`xml:"div"`
XMLName and xml.Name are fixed syntax for reading div
element, you can not change them at will.
Note
Also in the line:
Contentstring`xml:",chardata"`
The first letter of the variable Content must be capital. If you use
content, the Go parser will fail to read the content in div
element. You can use another name for the variable, as long as the first
letter is capital.
Note
If you replace the line:
Contentstring`xml:",chardata"`
with
Contentstring`xml:",innerxml"`
i.e., replace ,chardata with ,innerxml. The output result will be the same
because the raw XML nested inside the div element is the same as the
character data of the div element in this case.
# cannot use relative path in GOROOT, otherwise 6g not found. For example,# export GOROOT=../go (=> 6g not found)# it is also not allowed to use relative path in GOPATHexport GOROOT=$(realpath../../../../../go)export GOPATH=$(realpath.)export PATH:=$(GOROOT)/bin:$(PATH)all:parseFeedexample1: @# http://stackoverflow.com/questions/9967105/suppress-echo-of-command-invocation-in-makefile@gorunparse-1.go
example2:@gorunparse-2.go
example3:@gorunparse-3.go
example4:@gorunparse-4.go
example5:@gorunparse-5.go
example5_2:@gorunparse-5_2.go
example6:@gorunparse-6.go
example7:@gorunparse-7.go
atom2rss:@gorunatom2rss.go
parseFeed:@gorunparseFeed.go
help:gohelp