dongping4273 2017-08-21 12:31
浏览 137

使用xmlpath.v2 golang获取节点的值

I am parsing a xml document using gopkg.in/xmlpath.v2, and I am finding a trouble... I have no problem to get info from a single node, or get a iterator and loop over its items getting their info. But, the case where I am blocked is when I try to get the info from the same node on which I am iterating. I think that an example will be illuminating.
This is the XML:

<Warnings>
    <Warning Type="309" ShortText="Unfulfilled Paid Service 1">Unable to book seat 1</Warning>
    <Warning Type="309" ShortText="Unfulfilled Paid Service 2">Unable to book seat 2</Warning>
    <Warning Type="309" ShortText="Unfulfilled Paid Service 3">Unable to book seat 3</Warning>
</Warnings>

These are the xpath that I am usin:

xpath := xPathWarning{
    WarningsBase:       "Warnings/Warning",
    Warning:            "",
    WarningAttr:        "@ShortText",
}

And this is the way that I am trying to get the value and attribute:

func getWarnings(root *xmlpath.Node, xpath xpath_OC) []Warning {
    warnings := []Warning{}
    v, _ := xmlpath.Compile(xpath.WarningsBase)
    WarningsBaseIter := v.Iter(root)
    for 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 am able to get the ShortText, but not the value. I had been checking the error, showing on terminal the result of Path, err := xmlpath.Compile(pathString), and the err shown is compiling xml path "":0: empty path.

Any solution for this??
Thanks.

  • 写回答

1条回答 默认 最新

  • doushengyou2617 2017-08-24 01:08
    关注

    xmlquery package supports extract data from XML by XPath expression.

    package main
    
    import (
        "fmt"
        "strings"
    
        "github.com/antchfx/xmlquery"
    )
    
    func main() {
        xmlStr := `<?xml version="1.0"?><Warnings><Warning Type="309" ShortText="Unfulfilled Paid Service 1">Unable to book seat 1</Warning></Warnings>`
        doc, _ := xmlquery.Parse(strings.NewReader(xmlStr))
        nodes := xmlquery.Find(doc, "//Warning")
        for _, node := range nodes {
            fmt.Println(node.SelectAttr("ShortText"))
            fmt.Println(node.InnerText)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器