在Go语言中解析任意XML

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

Parsing arbitrary XML in Go

问题

我想使用Go语言解析任意的XML并提取特定的标签。例如,如果我们有以下XML:

<foo>
Hello, world.
<bar attr="true" />
</foo>

我想提取<bar attr="true" />,并且还想以字符串的形式访问“其他”数据,而不必担心它是什么。我不想定义文档可能包含的每个可能的XML标签,只想提取我想要的标签。
有人可以帮忙指导如何实现吗?提前感谢。

英文:

I want to use Go to parse arbitrary XML and pick out specific tags. For example if we have:

&lt;foo&gt;
Hello, world.
&lt;bar attr=&quot;true&quot; /&gt;
&lt;/foo&gt;

I want to pick out &lt;bar attr=&quot;true&quot; /&gt;, and also be able to access the "other" data as a string without having to worry about what it is. I don't want to have to define every possible XML tag the document could contain - only the ones I want to pick out.
Could someone help advise how this can be done? Thanks in advance.

答案1

得分: 2

我认为你想要的是一个XPath库,对吗?试试这个:http://godoc.org/launchpad.net/xmlpath

英文:

I think what you want is an xpath library, right? try this one: http://godoc.org/launchpad.net/xmlpath

huangapple
  • 本文由 发表于 2014年2月18日 17:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/21849052.html
匿名

发表评论

匿名网友

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

确定