解析XML并保留子元素

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

Unmarshal XML preserving child elements

问题

以下是您提供的代码的翻译:

package main

import (
	"encoding/xml"
	"log"
)

func main() {
	xmltext := []byte(`<Root><Foo>text text text <a href="url">foo bar</a> and more text.</Foo></Root>`)
	type mystruct struct {
		Foo string `xml:",innerxml"`
	}
	v := &mystruct{}

	err := xml.Unmarshal(xmltext, v)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(v.Foo)
}

您想要在解组时保留某个 XML 标签中的子元素。当前的结果是:

<Foo>text text text <a href="url">foo bar</a> and more text.</Foo>

但是您想要获取的结果是:

text text text <a href="url">foo bar</a> and more text.

即:您想要保留元素 Foo 的“字符串值”(不是 XPath 意义上的字符串值)。

您如何在不包含 <Foo></Foo> 的情况下获取元素 Foo 的内容?

英文:

Code at http://play.golang.org/p/PlOMw4wfT2

I'd like to preserve the child elements in some XML tag when unmarshalling. This is my code:

package main

import (
	&quot;encoding/xml&quot;
	&quot;log&quot;
)

func main() {
	xmltext := []byte(`&lt;Root&gt;&lt;Foo&gt;text text text &lt;a href=&quot;url&quot;&gt;foo bar&lt;/a&gt; and more text.&lt;/Foo&gt;&lt;/Root&gt;`)
	type mystruct struct {
		Foo string `xml:&quot;,innerxml&quot;`
	}
	v := &amp;mystruct{}

	err := xml.Unmarshal(xmltext, v)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(v.Foo)
}

and the result is:

	&lt;Foo&gt;text text text &lt;a href=&quot;url&quot;&gt;foo bar&lt;/a&gt; and more text.&lt;/Foo&gt;

but I'd like to get

	text text text &lt;a href=&quot;url&quot;&gt;foo bar&lt;/a&gt; and more text.

(without the surrounding Foo-tags)

That is: I want to preserve the "string value" (not the string value in the XPath sense) of the element Foo.

How can I get the contents from the element Foo without the &lt;Foo&gt; and &lt;/Foo&gt;?

答案1

得分: 1

,innerxml包含XML标签的内容(这就是它的名称所指的)。因此,我需要再深入一层:

package main

import (
	"encoding/xml"
	"log"
)

func main() {
	xmltext := []byte(`<Root><Foo>text text text <a href="url">foo bar</a> and more text.</Foo></Root>`)

	type text struct {
		Text string `xml:",innerxml"`
	}
	type mystruct struct {
		Foo text
	}
	v := &mystruct{}

	err := xml.Unmarshal(xmltext, v)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(v.Foo.Text)
	// text text text <a href="url">foo bar</a> and more text.
}
英文:

The ,innerxml contains the contents of the XML tag (that's what it's name is about). Therefore I have to go one level deeper:

<!-- language: lang-go -->
package main

import (
	&quot;encoding/xml&quot;
	&quot;log&quot;
)

func main() {
	xmltext := []byte(`&lt;Root&gt;&lt;Foo&gt;text text text &lt;a href=&quot;url&quot;&gt;foo bar&lt;/a&gt; and more text.&lt;/Foo&gt;&lt;/Root&gt;`)

	type text struct {
		Text string `xml:&quot;,innerxml&quot;`
	}
	type mystruct struct {
		Foo text
	}
	v := &amp;mystruct{}

	err := xml.Unmarshal(xmltext, v)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(v.Foo.Text)
	// text text text &lt;a href=&quot;url&quot;&gt;foo bar&lt;/a&gt; and more text.
}

huangapple
  • 本文由 发表于 2014年5月27日 17:57:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/23886378.html
匿名

发表评论

匿名网友

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

确定