xml - Get value of node using xmlpath.v2 golang -
i parsing xml document using gopkg.in/xmlpath.v2, , finding trouble... have no problem info single node, or iterator , loop on items getting info. but, case blocked when try info same node on iterating. think example illuminating.
xml:
<warnings> <warning type="309" shorttext="unfulfilled paid service 1">unable book seat 1</warning> <warning type="309" shorttext="unfulfilled paid service 2">unable book seat 2</warning> <warning type="309" shorttext="unfulfilled paid service 3">unable book seat 3</warning> </warnings> these xpath usin:
xpath := xpathwarning{ warningsbase: "warnings/warning", warning: "", warningattr: "@shorttext", } and way trying value , attribute:
func getwarnings(root *xmlpath.node, xpath xpath_oc) []warning { warnings := []warning{} v, _ := xmlpath.compile(xpath.warningsbase) warningsbaseiter := v.iter(root) warningsbaseiter.next() { rawoffer := warningsbaseiter.node() warning := warning{ value: getstring("", xpath.warning, rawoffer), attr: getstring("", xpath.warningattr, rawoffer), } warnings = append(warnings, warning) } return warnings } func getstring(expressionprefix string, xpathexpression string, node *xmlpath.node) string { expr := []string{expressionprefix, xpathexpression} pathstring := strings.join(expr, "") if pathstring != "blank" { path, err := xmlpath.compile(pathstring) if err == nil { value, _ := path.string(node) return value } } return "" } i able shorttext, not value. had been checking error, showing on terminal result of path, err := xmlpath.compile(pathstring), , err shown compiling xml path "":0: empty path.
any solution this??
thanks.
try xquery package supports extract data xml xpath expression.
import ( "github.com/antchfx/xquery/xml" ) xmlstr:=`<?xml version="1.0"?><warnings>...</warnings>` doc, _ := xmlquery.parse(strings.newreader(xmlstr)) nodes:=xmlquery.find(doc,"//warning") _,node:=range nodes{ fmt.println(node.selectattr("shorttext")) fmt.println(node.innertext) }
Comments
Post a Comment