英文:
How to get a value from an XML using XPath in Go
问题
在查看go xml package时,我没有找到这样的可能性。
Go只允许定义结构树,将其映射到XML树并使用<code>xml.NewDecoder(myXmlString).Decode(myStruct)</code>进行反序列化。
即使我定义了所需的Go结构树,我仍然无法使用XPath查询该树。
C#有一个方便的函数SelectSingleNode,可以通过指定XPath从XML树中选择值,而无需在C#类中复制整个树结构。
Go中是否有类似的可能性?
如果没有,那么实现它的最简单方法是什么(可能可以重用xml包)?
英文:
Looking at go xml package I could not find such possibility.
Go only allows to define tree of structures, map them to XML tree and deserialize using <code>xml.NewDecoder(myXmlString).Decode(myStruct)</code>.
Even if I define needed tree of Go structures, I still can't query that tree using XPath.
C# has convenient function SelectSingleNode that allows to select value from XML tree by specifying XPath without duplicating whole tree structure in C# classes.
Is there similar possibility in Go ?
If not then what is simplest way to implement it (possibly reusing xml package) ?
答案1
得分: 15
还有一个xmlpath包。
示例用法:
path := xmlpath.MustCompile("/library/book/isbn")
root, err := xmlpath.Parse(file)
if err != nil {
log.Fatal(err)
}
if value, ok := path.String(root); ok {
fmt.Println("找到:", value)
}
英文:
There's also the xmlpath package.
Sample usage:
path := xmlpath.MustCompile("/library/book/isbn")
root, err := xmlpath.Parse(file)
if err != nil {
log.Fatal(err)
}
if value, ok := path.String(root); ok {
fmt.Println("Found:", value)
}
答案2
得分: 14
在Go的标准包中没有XPath解析功能,所以你需要使用第三方包。
我知道的一个包是Gokogiri。
该包基于libxml2并使用cgo。
你需要导入的子包是github.com/moovweb/gokogiri/xpath
。
英文:
There is no xpath parsing in the standard packages of Go, so you need to resort to using a 3rd party package.
Then one I know of is Gokogiri
The package is based on libxml2 using cgo
The subpackage you want to import is github.com/moovweb/gokogiri/xpath
答案3
得分: 9
即使不使用xpath,您也可以使用原生的go xml encoder包从XML中读取值。您可以使用xml.Unmarshal()函数。这里是一个go play的示例。
package main
import "fmt"
import "encoding/xml"
func main() {
type People struct {
Names []string `xml:"Person>FullName"`
}
data := `
<People>
<Person>
<FullName>Jerome Anthony</FullName>
</Person>
<Person>
<FullName>Christina</FullName>
</Person>
</People>
`
v := People{Names: []string{}}
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Names of people: %q", v)
}
英文:
Even though not xpath, you can read values out of XML with the native go xml encoder package. You would use the xml.Unmarshal() function. Here is a go play example.
package main
import "fmt"
import "encoding/xml"
func main() {
type People struct {
Names []string `xml:"Person>FullName"`
}
data := `
<People>
<Person>
<FullName>Jerome Anthony</FullName>
</Person>
<Person>
<FullName>Christina</FullName>
</Person>
</People>
`
v := People{Names: []string{}}
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Names of people: %q", v)
}
答案4
得分: 6
xmlquery 允许您使用XPath表达式从XML文档中提取数据。
package main
import (
"fmt"
"strings"
"github.com/antchfx/xmlquery"
)
func main() {
htmlstr := `<?xml version="1.0" ?>
<html>
<head>
<title>this is a title</title>
</head>
<body>Hello,World</body>
</html>`
root, err := xmlquery.Parse(strings.NewReader(htmlstr))
if err != nil {
panic(err)
}
title := xmlquery.FindOne(root, "//title")
fmt.Println(title.InnerText())
}
英文:
xmlquery lets you extract data from XML documents using XPath expression.
package main
import (
"fmt"
"strings"
"github.com/antchfx/xmlquery"
)
func main() {
htmlstr := `<?xml version="1.0" ?>
<html>
<head>
<title>this is a title</title>
</head>
<body>Hello,World</body>
</html>`
root, err := xmlquery.Parse(strings.NewReader(htmlstr))
if err != nil {
panic(err)
}
title := xmlquery.FindOne(root, "//title")
fmt.Println(title.InnerText())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论