如何在Go中使用XPath从XML中获取值

huangapple go评论114阅读模式
英文:

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包。

示例用法:

  1. path := xmlpath.MustCompile("/library/book/isbn")
  2. root, err := xmlpath.Parse(file)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. if value, ok := path.String(root); ok {
  7. fmt.Println("找到:", value)
  8. }
英文:

There's also the xmlpath package.

Sample usage:

  1. path := xmlpath.MustCompile(&quot;/library/book/isbn&quot;)
  2. root, err := xmlpath.Parse(file)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. if value, ok := path.String(root); ok {
  7. fmt.Println(&quot;Found:&quot;, value)
  8. }

答案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的示例。

  1. package main
  2. import "fmt"
  3. import "encoding/xml"
  4. func main() {
  5. type People struct {
  6. Names []string `xml:"Person>FullName"`
  7. }
  8. data := `
  9. <People>
  10. <Person>
  11. <FullName>Jerome Anthony</FullName>
  12. </Person>
  13. <Person>
  14. <FullName>Christina</FullName>
  15. </Person>
  16. </People>
  17. `
  18. v := People{Names: []string{}}
  19. err := xml.Unmarshal([]byte(data), &v)
  20. if err != nil {
  21. fmt.Printf("error: %v", err)
  22. return
  23. }
  24. fmt.Printf("Names of people: %q", v)
  25. }
英文:

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.

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;encoding/xml&quot;
  4. func main() {
  5. type People struct {
  6. Names []string `xml:&quot;Person&gt;FullName&quot;`
  7. }
  8. data := `
  9. &lt;People&gt;
  10. &lt;Person&gt;
  11. &lt;FullName&gt;Jerome Anthony&lt;/FullName&gt;
  12. &lt;/Person&gt;
  13. &lt;Person&gt;
  14. &lt;FullName&gt;Christina&lt;/FullName&gt;
  15. &lt;/Person&gt;
  16. &lt;/People&gt;
  17. `
  18. v := People{Names: []string{}}
  19. err := xml.Unmarshal([]byte(data), &amp;v)
  20. if err != nil {
  21. fmt.Printf(&quot;error: %v&quot;, err)
  22. return
  23. }
  24. fmt.Printf(&quot;Names of people: %q&quot;, v)
  25. }

答案4

得分: 6

xmlquery 允许您使用XPath表达式从XML文档中提取数据。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/antchfx/xmlquery"
  6. )
  7. func main() {
  8. htmlstr := `<?xml version="1.0" ?>
  9. <html>
  10. <head>
  11. <title>this is a title</title>
  12. </head>
  13. <body>Hello,World</body>
  14. </html>`
  15. root, err := xmlquery.Parse(strings.NewReader(htmlstr))
  16. if err != nil {
  17. panic(err)
  18. }
  19. title := xmlquery.FindOne(root, "//title")
  20. fmt.Println(title.InnerText())
  21. }
英文:

xmlquery lets you extract data from XML documents using XPath expression.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. &quot;github.com/antchfx/xmlquery&quot;
  6. )
  7. func main() {
  8. htmlstr := `&lt;?xml version=&quot;1.0&quot; ?&gt;
  9. &lt;html&gt;
  10. &lt;head&gt;
  11. &lt;title&gt;this is a title&lt;/title&gt;
  12. &lt;/head&gt;
  13. &lt;body&gt;Hello,World&lt;/body&gt;
  14. &lt;/html&gt;`
  15. root, err := xmlquery.Parse(strings.NewReader(htmlstr))
  16. if err != nil {
  17. panic(err)
  18. }
  19. title := xmlquery.FindOne(root, &quot;//title&quot;)
  20. fmt.Println(title.InnerText())
  21. }

huangapple
  • 本文由 发表于 2012年11月16日 11:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/13409751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定