go-xmlpath / xmlpath

Strict subset of the XPath specification for the Go language.
http://gopkg.in/xmlpath.v2
Other
115 stars 37 forks source link

root.String() return "empty" string #21

Open irwinnoteam2009 opened 8 years ago

irwinnoteam2009 commented 8 years ago
import (
    "bytes"
    "encoding/xml"
    "fmt"
    "strings"
    "gopkg.in/xmlpath.v2"
    "golang.org/x/net/html/charset"
)

func main() {
    r := bytes.NewBuffer([]byte(d))
    decoder := xml.NewDecoder(r)
    decoder.CharsetReader = charset.NewReaderLabel
    root, _ := xmlpath.ParseDecoder(decoder)

        fmt.Printf("% x\n", root.Bytes()) 

    p, _ := xmlpath.Compile("list_boxes/boxes/*")
    iter := p.Iter(root)
    for iter.Next() {
        elem := iter.Node()
        s := strings.TrimSpace(elem.String())
        fmt.Printf("node: [%s]\n", s)
    }
}

const d = `
<?xml version="1.0" encoding="windows-1251" ?>
<list_boxes>
  <boxes>
    <box Name="1" Caption="Àäìèíèñòðàòîð" >
      <Flags/>
    </box>
    <box Name="2" Caption="Îïåðàòîð">
      <Flags/>
    </box>
    </boxes>
</list_boxes>`