I have a XML file like:
* * *
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tests>
<!--
Comment
-->
<tests version="1.0" xmlns="
http://www.w3.org/2001/XMLSchema-instance" SchemaLocation="tests.xsd">
<test>
<address>foo</address>
<valid>true</valid>
<id>1</id>
</test>
<test>
<address>bar</address>
<valid>true</valid>
<id>2</id>
</test>
...
...
<glossary id="length">
<title>123</title>
<desc>asd</desc>
</glossary>
...
...
</tests>
* * *
* * *
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"path/filepath"
)
type Result struct {
address string
valid bool
id int
}
func main() {
testFile := filepath.Join("testdata", "foo.xml")
data, err := ioutil.ReadFile(testFile)
if err != nil {
log.Fatalf("fail: %s", err)
}
parser := xml.NewDecoder(bytes.NewReader(data))
//v := &Result{}
for i := 0; i < 4; i++ {
tok, err := parser.Token()
if err != nil {
log.Fatalf("fail: %s", err)
}
fmt.Println(tok)
}
}
* * *
The output that I get is:
***
{xml [118 101 114 115 105 111 110 61 34 49 46 48 34 32 101 110 99 111 100 105 110 103 61 34 117 116 102 45 56 34]}
[10]
[68 79 67 84 89 80 69 32 116 101 115 116 115]
[10]
***
But it does not help me a lot of; the XML elements should have a method String().
How to handle it correctly to get data in elements <test> ?