func main() { data, err := ioutil.ReadFile("xpath.xml") util.CheckErr(err) doc, err := xml.Parse(data, nil, nil, 0, xml.DefaultEncodingBytes) util.CheckErr(err) defer doc.Free() n, err := doc.Root().Search(`/probe/header/@vendor`) fmt.Println("Vendor:") fmt.Println(n) util.CheckErr(err) }
func main() { data, err := ioutil.ReadFile("xpath.xml") if err != nil { log.Fatal(err) } doc, err := xml.Parse(data, nil, nil, 0, xml.DefaultEncodingBytes) if err != nil { log.Fatal(err) } defer doc.Free() xp := doc.DocXPathCtx() xp.RegisterNamespace("folia", "http://ilk.uvt.nl/folia") fmt.Println("\nAll sentences with all words:\n") xps := xpath.Compile("//folia:s") xpw := xpath.Compile("folia:w/folia:t") ss, err := doc.Root().Search(xps) if err != nil { log.Fatal(err) } for _, s := range ss { fmt.Println(s.Attr("id")) ww, err := s.Search(xpw) if err != nil { log.Fatal(err) } for _, w := range ww { fmt.Println("\t" + w.Parent().Attr("id") + " \t" + w.Content()) } } fmt.Println("\nSearch for specific sentence:\n") n, err := doc.Root().Search(`//folia:s[@xml:id="WR-P-E-E-0000000020.head.4.s.2"]`) if err != nil { log.Fatal(err) } fmt.Println(n) fmt.Println("\nSearch for sentence with specific word:\n") n, err = doc.Root().Search(`//folia:w[@xml:id="WR-P-E-E-0000000020.head.4.s.2.w.2"]`) if err != nil { log.Fatal(err) } fmt.Println(n[0].Parent()) }
// TODO: Make the Parser options configureable. func (d *Decoder) Decode(data []byte, v interface{}) error { var opts = gokoxml.DefaultParseOption | gokoxml.XML_PARSE_NOENT doc, err := gokoxml.Parse(data, gokoxml.DefaultEncodingBytes, nil, opts, gokoxml.DefaultEncodingBytes) d.doc = doc if err != nil { return err } defer doc.Free() val := reflect.ValueOf(v) if val.Kind() != reflect.Ptr { return errors.New("non-pointer passed to Decode") } return d.unmarshal(val.Elem(), nil) }
func (p *GokoParser) Decode(data []byte, result *ECSResponse) error { doc, err := gokoxml.Parse(data, gokoxml.DefaultEncodingBytes, nil, gokoxml.DefaultParseOption, gokoxml.DefaultEncodingBytes) p.doc = doc if err != nil { return err } defer p.doc.Free() doc.XPathCtx.RegisterNamespace("ecs", "http://webservices.amazon.com/AWSECommerceService/2010-11-01") root := doc.Root() res, err := root.Search("ecs:Items/ecs:Item") if err == nil && len(res) >= 1 { for _, v := range res { result.Items = append(result.Items, p.parseItem(v.FirstChild())) } } return err }
func parse(c *cli.Context, data []byte) { doc, err := xml.Parse(data, nil, nil, 0, xml.DefaultEncodingBytes) util.CheckErr(err) defer doc.Free() xp := doc.DocXPathCtx() for _, xmlns := range c.StringSlice("xmlns") { ns := strings.SplitN(xmlns, ":", 2) if c.Bool("verbose") { fmt.Println("NS " + ns[0] + "==" + ns[1]) } xp.RegisterNamespace(ns[0], ns[1]) } xps := xpath.Compile(c.String("xpath")) s, err := doc.Root().Search(xps) util.CheckErr(err) for _, s := range s { util.CheckErr(err) fmt.Println(s.Content()) } }
/* ParseXml parses an UTF-8 encoded byte array and returns an xml.XmlDocument. By default the parsing options ignore validation and suppress errors and warnings. This allows one to liberal in accepting badly-formed documents, but is not standards-compliant. If the content is not UTF-8 encoded or you want to customize the parsing options, you should call the Parse or ReadFile functions found in the github.com/moovweb/gokogiri/xml package. The xml.StrictParsingOption is conveniently provided for standards-compliant behaviour. */ func ParseXml(content []byte) (doc *xml.XmlDocument, err error) { return xml.Parse(content, xml.DefaultEncodingBytes, nil, xml.DefaultParseOption, xml.DefaultEncodingBytes) }