Reading XML with golang

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

Reading XML with golang

问题

我正在尝试使用Go语言读取XML。我基于这个示例进行开发,该示例是有效的。https://gist.github.com/kwmt/6135123#file-parsetvdb-go

这是我的文件:

Castle0.xml

<?xml version="1.0" encoding="UTF-8"?>
<Channel>
<Title>test</Title>
<Description>this is a test</Description>
</Channel>

test.go

package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

type Query struct {
	Chan Channel `xml:"Channel"`
}

type Channel struct {
	title    string `xml:"Title"`
	desc string `xml:"Description"`
}


func (s Channel) String() string {
	return fmt.Sprintf("%s - %d", s.title, s.desc)
}

func main() {
	xmlFile, err := os.Open("Castle0.xml")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer xmlFile.Close()

	b, _ := ioutil.ReadAll(xmlFile)

	var q Query
	xml.Unmarshal(b, &q)

	fmt.Println(q.Chan)

}

输出:

  • %!d(string=)

有人知道我做错了什么吗?(我正在学习Go,所以请对我宽容一些 :P)

英文:

I'm trying to read som XML with golang. I'm basing it on this example which works. https://gist.github.com/kwmt/6135123#file-parsetvdb-go

This is my files:

Castle0.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;Channel&gt;
&lt;Title&gt;test&lt;/Title&gt;
&lt;Description&gt;this is a test&lt;/Description&gt;
&lt;/Channel&gt;

test.go

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;
)

type Query struct {
	Chan Channel `xml:&quot;Channel&quot;`
}

type Channel struct {
	title    string `xml:&quot;Title&quot;`
	desc string `xml:&quot;Description&quot;`
}


func (s Channel) String() string {
	return fmt.Sprintf(&quot;%s - %d&quot;, s.title, s.desc)
}

func main() {
	xmlFile, err := os.Open(&quot;Castle0.xml&quot;)
	if err != nil {
		fmt.Println(&quot;Error opening file:&quot;, err)
		return
	}
	defer xmlFile.Close()

	b, _ := ioutil.ReadAll(xmlFile)

	var q Query
	xml.Unmarshal(b, &amp;q)

	fmt.Println(q.Chan)

}

Output:

  • %!d(string=)

Any one know what I'm doing wrong? (I'm doing this to learn go, so go easy on me :P)

答案1

得分: 4

其他包,包括encoding/jsonencoding/xml,只能看到导出的数据。所以首先你的titledesc应该分别是TitleDesc

其次,在打印字符串时,你在Sprintf中使用了%d(整数)格式。这就是为什么你得到了%!d(string=),它的意思是“它不是一个整数,它是一个字符串!”。

第三,在你的XML中没有查询,所以直接解组到q.Chan中。

这是一个可工作的示例。http://play.golang.org/p/l0ImL2ID-j

英文:

Other packages, including encoding/json and encoding/xml can only see exported data. So firstly your title and desc should be Title and Desc correspondingly.

Secondly, you're using %d (integer) format in Sprintf when printing a string. That's why you're getting %!d(string=), which means "it's not an integer, it's a string!".

Thirdly, there is no query in your XML, so unmarshal directly into q.Chan.

This is the working example. http://play.golang.org/p/l0ImL2ID-j

huangapple
  • 本文由 发表于 2015年5月15日 04:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/30246565.html
匿名

发表评论

匿名网友

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

确定