<;零>;在golang中解析xml字符串时
我想使用<;零>;在golang中解析xml字符串时,xml,go,Xml,Go,我想使用golang解析xml。由于不熟悉使用go,我在网上阅读了一些文章,解释了如何解析XML,但我不确定在这种情况下为什么返回值是nil package main import ( "fmt" //"io/ioutil" "encoding/xml" ) func check(e error) { if e != nil { panic(e) } } type Books struct { XMLName xml.Name
golang
解析xml
。由于不熟悉使用go
,我在网上阅读了一些文章,解释了如何解析XML,但我不确定在这种情况下为什么返回值是nil
package main
import (
"fmt"
//"io/ioutil"
"encoding/xml"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type Books struct {
XMLName xml.Name `xml:"Books"`
BookList []Book `xml:"Books>Book"`
}
type Book struct {
title string `xml:"title,attr"`
author string
published string
}
func main() {
//f, err := ioutil.ReadFile("xml/Books.xml")
//check(err)
var data = []byte(`
<Books>
<Book title="A Brief History of Time" author="Stephen Hawking" published="1988">
<title>title here</title>
A Brief History of Time: From the Big Bang to Black Holes is a 1988 popular-science book by British physicist Stephen Hawking. It became a bestseller and sold more than 10 million copies in 20 years.
</Book>
<Book title="Steve Jobs" author="Walter Isaacson" published="2011">
Steve Jobs is the authorized self-titled biography book of Steve Jobs. The book was written at the request of Jobs by Walter Isaacson, a former executive at CNN.
</Book>
</Books>
`)
b := Books{}
o := xml.Unmarshal([]byte(data), &b)
fmt.Println(o)
}
主程序包
进口(
“fmt”
//“io/ioutil”
“编码/xml”
)
功能检查(e错误){
如果e!=nil{
恐慌(e)
}
}
类型图书结构{
XMLName xml.Name`xml:“书籍”`
BookList[]Book`xml:“图书>图书”`
}
类型书结构{
标题字符串`xml:“标题,属性”`
作者字符串
已发布字符串
}
func main(){
//f、 err:=ioutil.ReadFile(“xml/Books.xml”)
//检查(错误)
变量数据=[]字节(`
标题在这里
《时间简史:从大爆炸到黑洞》是英国物理学家斯蒂芬·霍金1988年出版的一本科普书。该书成为畅销书,20年内销量超过1000万册。
史蒂夫·乔布斯是史蒂夫·乔布斯的授权自传。这本书是应乔布斯的要求,由美国有线电视新闻网(CNN)前高管沃尔特·艾萨克森(Walter Isaacson)撰写的。
`)
b:=书籍{}
o:=xml.Unmarshal([]字节(数据),&b)
fmt.Println(o)
}
我将调试反馈放在一条评论中,但我刚刚修改了您的示例,使其能够工作,并且可以在这里进行测试
主程序包
进口(
“fmt”
//“io/ioutil”
“编码/xml”
)
功能检查(e错误){
如果e!=nil{
恐慌(e)
}
}
类型图书结构{
XMLName xml.Name`xml:“书籍”`
BookList[]Book`xml:“Book”`
}
类型书结构{
标题字符串`xml:“标题,属性”`
作者字符串`xml:“作者,属性”`
已发布字符串`xml:“已发布,属性”`
}
func main(){
//f、 err:=ioutil.ReadFile(“xml/Books.xml”)
//检查(错误)
变量数据=[]字节(`
标题在这里
《时间简史:从大爆炸到黑洞》是英国物理学家斯蒂芬·霍金1988年出版的一本科普书。该书成为畅销书,20年内销量超过1000万册。
史蒂夫·乔布斯是史蒂夫·乔布斯的授权自传。这本书是应乔布斯的要求,由美国有线电视新闻网(CNN)前高管沃尔特·艾萨克森(Walter Isaacson)撰写的。
`)
b:=书籍{}
o:=xml.Unmarshal([]字节(数据),&b)
fmt.Println(o)
fmt.Println(b)
}
下面是我所做的四个改变的概要
1) 打印Books
对象,而不是从Unmarshal
2) 大写Book
上字段的第一个字母,使它们“导出”,以便其他包(本例中的解组器)可以获取/设置它们
3) 添加xml属性。在导出字段时,它会使字段不存在隐式字符串匹配,因此必须显式指定将哪个xml值读入每个字段
4) 更新
BookList
的XML路径。为此,您说过它将是Books>Book,但这意味着另一个层次的嵌套在您的XML中不存在。这个对象是Book
,您想要在该列表中的元素的相对xpath为Book
,所以这就是您放在那里的内容。您正在打印错误,因为它有效,所以为零xml.Unmarshal
不返回传入的值-second arg&b
。此外,在键入Book
时,字段未报告(第一个字母需要改为大写),因此解编程序将无法设置其值。
package main
import (
"fmt"
//"io/ioutil"
"encoding/xml"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type Books struct {
XMLName xml.Name `xml:"Books"`
BookList []Book `xml:"Book"`
}
type Book struct {
Title string `xml:"title,attr"`
Author string `xml:"author,attr"`
Published string `xml:"published,attr"`
}
func main() {
//f, err := ioutil.ReadFile("xml/Books.xml")
//check(err)
var data = []byte(`
<Books>
<Book title="A Brief History of Time" author="Stephen Hawking" published="1988">
<title>title here</title>
A Brief History of Time: From the Big Bang to Black Holes is a 1988 popular-science book by British physicist Stephen Hawking. It became a bestseller and sold more than 10 million copies in 20 years.
</Book>
<Book title="Steve Jobs" author="Walter Isaacson" published="2011">
Steve Jobs is the authorized self-titled biography book of Steve Jobs. The book was written at the request of Jobs by Walter Isaacson, a former executive at CNN.
</Book>
</Books>
`)
b := Books{}
o := xml.Unmarshal([]byte(data), &b)
fmt.Println(o)
fmt.Println(b)
}