func (hfy *httphfy) Paser(reader io.Reader) *Response { rsp := new(Response) root, err := xmlpath.Parse(reader) if err != nil { rsp.Status = FAILED rsp.Msg = "短信平台服务错误" return rsp } pathstatus := xmlpath.MustCompile("//returnsms/returnstatus") pathmessage := xmlpath.MustCompile("//returnsms/message") if status, ok := pathstatus.String(root); ok { if status == "Success" { rsp.Status = SUCCESS } else { rsp.Status = FAILED } } else { rsp.Status = FAILED rsp.Msg = "短信平台服务错误" return rsp } if message, ok := pathmessage.String(root); ok { rsp.Msg = "短信平台:" + message } return rsp }
// lookupItem from Amazon store using itemId. Uses ItemLookup method from Product Advertising API. func lookupItem(cred AWSCredentials, itemId string) *xmlpath.Node { // create request q := newAWSQuery(cred.host, cred.accessKey) q.params["Operation"] = "ItemLookup" q.params["ItemId"] = itemId q.params["ResponseGroup"] = "ItemAttributes" // create signature query, signature := signRequest(q, cred) // make request url request := "http://" + cred.host + "/onca/xml" + "?" + query + "&Signature=" + signature // HTTP GET resp, _ := http.Get(request) // parse response xml root, err := xmlpath.Parse(resp.Body) if err != nil { panic(err) } return root }
func main() { f, err := os.Open("test3.xml") if err != nil { fmt.Println(err) return } n, err := xmlpath.Parse(f) f.Close() if err != nil { fmt.Println(err) return } q1 := xmlpath.MustCompile("//item") if _, ok := q1.String(n); !ok { fmt.Println("no item") } q2 := xmlpath.MustCompile("//price") for it := q2.Iter(n); it.Next(); { fmt.Println(it.Node()) } q3 := xmlpath.MustCompile("//name") names := []*xmlpath.Node{} for it := q3.Iter(n); it.Next(); { names = append(names, it.Node()) } if len(names) == 0 { fmt.Println("no names") } }
func (vim *VimClient) FindByInventoryPath(inventoryPath string) (vmId string, err error) { data := struct { InventoryPath string }{ inventoryPath, } t := template.Must(template.New("FindByInventoryPath").Parse(FindByInventoryPathRequestTemplate)) log.Printf("Looking for '%s'", inventoryPath) request, _ := vim.prepareRequest(t, data) response, err := vim.Do(request) // defer response.Body.Close() if err != nil { err = fmt.Errorf("Error calling FindByInventoryPath: '%s'", err.Error()) return } body, _ := ioutil.ReadAll(response.Body) // log.Printf("RESPONSE BODY BELOW:\n============\n%s\n===========\nEND RESPONSE BODY\n", string(body)) root, _ := xmlpath.Parse(bytes.NewBuffer(body)) path := xmlpath.MustCompile("//*/FindByInventoryPathResponse/returnval") if vmId, ok := path.String(root); ok { return vmId, err } else { err := fmt.Errorf("Found nothing.") return vmId, err } }
func (r *XMLResource) XPath(xpath string) (string, bool) { path := xmlpath.MustCompile(xpath) b := bytes.NewBufferString(r.Body()) root, _ := xmlpath.Parse(b) return path.String(root) }
func (r *XMLResource) XPathIter(xpath string) *XMLIter { path := xmlpath.MustCompile(xpath) b := bytes.NewBufferString(string(r.Body())) root, _ := xmlpath.Parse(b) return &XMLIter{iter: path.Iter(root)} }
func (s *BasicSuite) TestRootText(c *C) { node, err := xmlpath.Parse(bytes.NewBuffer(trivialXml)) c.Assert(err, IsNil) path := xmlpath.MustCompile("/") result, ok := path.String(node) c.Assert(ok, Equals, true) c.Assert(result, Equals, "abcdefg") }
func parseVmPowerStateProperty(body *bytes.Buffer) (value string) { root, _ := xmlpath.Parse(bytes.NewBuffer(body.Bytes())) path := xmlpath.MustCompile("//*/RetrievePropertiesResponse/returnval/propSet[name='runtime']/val/powerState") if value, ok := path.String(root); ok { return value } else { return "" } }
func (s *BasicSuite) TestLibraryTable(c *C) { node, err := xmlpath.Parse(bytes.NewBuffer(libraryXml)) c.Assert(err, IsNil) for _, test := range libraryTable { cmt := Commentf("xml path: %s", test.path) path, err := xmlpath.Compile(test.path) if want, ok := test.result.(cerror); ok { c.Assert(err, ErrorMatches, string(want), cmt) c.Assert(path, IsNil, cmt) continue } c.Assert(err, IsNil) switch want := test.result.(type) { case string: got, ok := path.String(node) c.Assert(ok, Equals, true, cmt) c.Assert(got, Equals, want, cmt) c.Assert(path.Exists(node), Equals, true, cmt) iter := path.Iter(node) iter.Next() node := iter.Node() c.Assert(node.String(), Equals, want, cmt) c.Assert(string(node.Bytes()), Equals, want, cmt) case []string: var alls []string var allb []string iter := path.Iter(node) for iter.Next() { alls = append(alls, iter.Node().String()) allb = append(allb, string(iter.Node().Bytes())) } c.Assert(alls, DeepEquals, want, cmt) c.Assert(allb, DeepEquals, want, cmt) s, sok := path.String(node) b, bok := path.Bytes(node) if len(want) == 0 { c.Assert(sok, Equals, false, cmt) c.Assert(bok, Equals, false, cmt) c.Assert(s, Equals, "") c.Assert(b, IsNil) } else { c.Assert(sok, Equals, true, cmt) c.Assert(bok, Equals, true, cmt) c.Assert(s, Equals, alls[0], cmt) c.Assert(string(b), Equals, alls[0], cmt) c.Assert(path.Exists(node), Equals, true, cmt) } case exists: wantb := bool(want) ok := path.Exists(node) c.Assert(ok, Equals, wantb, cmt) _, ok = path.String(node) c.Assert(ok, Equals, wantb, cmt) } } }
func ExtractToken(s string) string { root, err := xmlpath.Parse(strings.NewReader(s)) if err != nil { log.Fatal(err) } if value, ok := path.String(root); ok { return value } return "" }
func parseVmPropertyValue(prop string, body *bytes.Buffer) (value string) { root, _ := xmlpath.Parse(body) pathString := strings.Join([]string{"//*/RetrievePropertiesResponse/returnval/propSet[name='", prop, "']/val"}, "") path := xmlpath.MustCompile(pathString) if value, ok := path.String(root); ok { return value } else { return "" } }
func getXPathValue(xml string, eomfile EomFile, lookupPath string) (string, bool) { path := xmlpath.MustCompile(lookupPath) root, err := xmlpath.Parse(strings.NewReader(xml)) if err != nil { warnLogger.Printf("Cannot parse XML of eomfile using xpath [%v], error: [%v]", err.Error(), lookupPath) return "", false } xpathValue, ok := path.String(root) return xpathValue, ok }
func parseTaskIdFromResponse(response *http.Response) (value string) { body, _ := ioutil.ReadAll(response.Body) root, _ := xmlpath.Parse(bytes.NewBuffer(body)) path := xmlpath.MustCompile("//*/CloneVM_TaskResponse/returnval") if value, ok := path.String(root); ok { return value } else { return "" } }
func (s *BasicSuite) BenchmarkSimplePathString(c *C) { node, err := xmlpath.Parse(bytes.NewBuffer(instancesXml)) c.Assert(err, IsNil) path := xmlpath.MustCompile("/DescribeInstancesResponse/reservationSet/item/instancesSet/item/instanceType") var str string c.ResetTimer() for i := 0; i < c.N; i++ { str, _ = path.String(node) } c.StopTimer() c.Assert(str, Equals, "m1.small") }
func (s *BasicSuite) BenchmarkSimplePathExists(c *C) { node, err := xmlpath.Parse(bytes.NewBuffer(instancesXml)) c.Assert(err, IsNil) path := xmlpath.MustCompile("/DescribeInstancesResponse/reservationSet/item/instancesSet/item/instanceType") var exists bool c.ResetTimer() for i := 0; i < c.N; i++ { exists = path.Exists(node) } c.StopTimer() c.Assert(exists, Equals, true) }
func parseTaskPropertyValues(body []byte, props ...string) map[string]string { values := make(map[string]string) for _, prop := range props { root, _ := xmlpath.Parse(bytes.NewBuffer(body)) // pathString := fmt.Sprintf("//*/RetrievePropertiesResponse/returnval/propSet/val/%s", prop) pathString := strings.Join([]string{"//*/RetrievePropertiesResponse/returnval/propSet/val/", prop}, "") path := xmlpath.MustCompile(pathString) if value, ok := path.String(root); ok { values[prop] = value } else { values[prop] = "" } } return values }
// LatestVersion find latest version of the artifact func (r *MavenRepository) LatestVersion(artifact *Artifact) (version string, err error) { metadataURL := fmt.Sprintf("%s/maven-metadata.xml", r.artifactURLBase(artifact)) utils.Log("info", fmt.Sprintf("Search latest version to %s: %+v", metadataURL, artifact)) content, err := ValidateDownload(metadataURL) if err != nil { return "", err } xmlPath := xmlpath.MustCompile(`/metadata/versioning/latest`) root, err := xmlpath.Parse(bytes.NewReader(content)) if err != nil { return "", err } if latestVersion, ok := xmlPath.String(root); ok { utils.Log("info", fmt.Sprintf("Found latest version: %s", latestVersion)) return latestVersion, nil } return "", fmt.Errorf("Could not find latest version in %s", metadataURL) }
func parsePathProperty(body *bytes.Buffer) (value string) { root, _ := xmlpath.Parse(bytes.NewBuffer(body.Bytes())) path := xmlpath.MustCompile("//RetrievePropertiesResponse//val") iter := path.Iter(root) values := make([]string, 0) // iter.Next() // skip top element "Datacenters" for { // iter.Next() // skip id element ok := iter.Next() if ok == false { break } else { newVal := []string{iter.Node().String()} // add new value to the beginning of the slice // TODO: get rid of ids // current end value: Datacenters/group-d1/Tukwila/datacenter-2/vm/group-v3/1-templates/group-v53287/Lower/group-v54541/my_new_template_that_packer_built values = append(newVal, values...) } } value = strings.Join(values, "/") return value }
func main() { flag.Parse() pageXPath = xmlpath.MustCompile("/mediawiki/page") pageIdXPath = xmlpath.MustCompile("id") textXPath = xmlpath.MustCompile(("revision/text")) titleXPath = xmlpath.MustCompile("title") fi, err := os.Open("enwikiquote-20140817-pages-articles-multistream.xml") //fi, err := os.Open("sample6.xml") //fi, err := os.Open("sample.xml") //fi, err := os.Open("sample2.xml") if err != nil { panic(err) } // close fi on exit and check for its returned error defer func() { if err := fi.Close(); err != nil { panic(err) } }() t := time.Now() prefix := t.Format(layout) fname := fmt.Sprintf("quotes-en-%s.csv", prefix) // open output file glog.V(1).Infof("Creating file %s", fname) quoteCSV, err := os.Create(fname) if err != nil { panic(err) } // close fo on exit and check for its returned error defer func() { if err := quoteCSV.Close(); err != nil { panic(err) } }() quoteWriter = csv.NewWriter(quoteCSV) // make a read buffer r := bufio.NewReader(fi) root, err := xmlpath.Parse(r) if err != nil { glog.Fatal(err) } iter := pageXPath.Iter(root) go Multiplex() go Multiplex() go Multiplex() go Multiplex() for iter.Next() { page := iter.Node() pageChannel <- page } }
func (s *BasicSuite) BenchmarkParse(c *C) { for i := 0; i < c.N; i++ { _, err := xmlpath.Parse(bytes.NewBuffer(instancesXml)) c.Assert(err, IsNil) } }
func main() { sourceFilePath := flag.String("source-file-path", "", "source file path") targetDirPath := flag.String("target-dir-path", "", "target dir path") verbose := flag.Bool("verbose", false, "verbose") flag.Parse() if *sourceFilePath == "" || *targetDirPath == "" { flag.PrintDefaults() os.Exit(2) } var err error report, err = os.Create("report.txt") if err != nil { panic(err) } defer report.Close() f, err := os.Open(*sourceFilePath) if err != nil { panic(err) } defer f.Close() root, err = xmlpath.Parse(f) if err != nil { panic(err) } filepath.Walk(*targetDirPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } if filepath.Base(*sourceFilePath) == filepath.Base(path) { fmt.Printf("CHECKING... %s\n", path) fmt.Fprintf(report, "\n\nSOURCE : %s\n", *sourceFilePath) fmt.Fprintf(report, "TARGET : %s\n", path) f, err := os.Open(path) if err != nil { fmt.Println(err) fmt.Fprintln(report, err) return nil } defer f.Close() m, err := mxj.NewMapXmlReader(f) if err != nil { fmt.Println(err) fmt.Fprintln(report, err) return nil } matched = matched[:0] skipped = skipped[:0] unmatched = unmatched[:0] lookupMap(m) if len(unmatched) != 0 { fmt.Fprintf(report, "\n...UNMATCHED...\n") for _, s := range unmatched { fmt.Fprintf(report, "%s\n", s) } } if len(skipped) != 0 { fmt.Fprintf(report, "\n...skipped...\n") for _, s := range skipped { fmt.Fprintf(report, "%s\n", s) } } if *verbose && len(matched) != 0 { fmt.Fprintf(report, "\n...matched...\n") for _, s := range matched { fmt.Fprintf(report, "%s\n", s) } } } return nil }) fmt.Println("DONE... report.txt") }