解析嵌套的OPML文档

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

Unmarshal a nested OPML Document

问题

我正在尝试解析一个简单的嵌套opml文档。问题出在当我有嵌套的opml时,我无法确定结构。我将代码放在下面。请告诉我如何解析Outline结构的多层嵌套。

package main

import (
	"encoding/xml"
	"fmt"
)

var response = `<opml version='1.0'>
	<head>
		<title>More Cases</title>
		<expansionState>1,6,26</expansionState>
	</head>
	<body>
		<outline text='Testing' _note='indeterminate'>
			<outline text='Weekly' _status='indeterminate'>
			</outline>
		</outline>
	</body>
</opml>`

type Opml struct {
	XMLName xml.Name
	Version string `xml:"version,attr"`
	Head    Head   `xml:"head"`
	Body    Body   `xml:"body"`
}

type Head struct {
	Title          string `xml:"title"`
	ExpansionState string `xml:"expansionState"`
}

type Body struct {
	Outline Outline `xml:"outline"`
}
type Outline struct {
	Text string `xml:"text,attr"`
	Note string `xml:"_note,attr"`
}

func main() {

	fmt.Println("UnMrashal XML")
	opml := &Opml{}
	xml.Unmarshal([]byte(response), opml)
	fmt.Println(opml)
}
英文:

I am trying to unmarshal a simple nested opml document. The problem comes when I have nested opml and I am unable to decide on structure. I am putting the code below. Please let me know how can I parse the multi level of nesting of the Outline struct.

http://play.golang.org/p/FobiL1JDdb

	package main

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

	var response = `&lt;opml version=&#39;1.0&#39;&gt;
	 &lt;head&gt;
	  &lt;title&gt;More Cases&lt;/title&gt;
	  &lt;expansionState&gt;1,6,26&lt;/expansionState&gt;
	 &lt;/head&gt;
	 &lt;body&gt;
	  &lt;outline text=&#39;Testing&#39; _note=&#39;indeterminate&#39;&gt;
	   &lt;outline text=&#39;Weekly&#39; _status=&#39;indeterminate&#39;&gt;
	   &lt;/outline&gt;
	   &lt;/outline&gt;
	 &lt;/body&gt;
	&lt;/opml&gt;`

	type Opml struct {
		XMLName xml.Name
		Version string `xml:&quot;version,attr&quot;`
		Head    Head   `xml:&quot;head&quot;`
		Body    Body   `xml:&quot;body&quot;`
	}

	type Head struct {
		Title          string `xml:&quot;title&quot;`
		ExpansionState string `xml:&quot;expansionState&quot;`
	}

	type Body struct {
		Outline Outline `xml:&quot;outline&quot;`
	}
	type Outline struct {
		Text string `xml:&quot;text,attr&quot;`
		Note string `xml:&quot;_note,attr&quot;`
	}

	func main() {

		fmt.Println(&quot;UnMrashal XML&quot;)
		opml := &amp;Opml{}
		xml.Unmarshal([]byte(response), opml)
		fmt.Println(opml)
	}

答案1

得分: 1

你需要使用指针

type Outline struct {
    Text string `xml:"text,attr"`
    Note string `xml:"_note,attr"`
    Outline *Outline `xml:"outline"`
}

http://play.golang.org/p/f1UqEkJq4S

英文:

you need use pointer

type Outline struct {
	Text string `xml:&quot;text,attr&quot;`
	Note string `xml:&quot;_note,attr&quot;`
	Outline *Outline `xml:&quot;outline&quot;`
}

http://play.golang.org/p/f1UqEkJq4S

huangapple
  • 本文由 发表于 2015年8月27日 18:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/32247493.html
匿名

发表评论

匿名网友

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

确定