英文:
Iterating through xml in go
问题
给定一个类似以下的xml块:
<data>
<entry>
... 几个嵌套元素
</entry>
<entry>
... 更多嵌套元素
</entry>
</data>
我该如何迭代遍历文档中的每个<entry>
元素,并将其放入一个结构体中,在移动到下一个条目之前对其执行一些操作?
我已经能够解析并从上述的XML块中将数据存储到结构体中,其中只存在一个<entry>
元素。也就是说,我已经成功地将类似以下的内容存储到结构体中:
<entry>
... 几个嵌套元素
</entry>
英文:
Given a block of xml similar to the following:
<data>
<entry>
... several nested elements
</entry>
<entry>
... more nested elements
</entry>
</data>
How can I iterate over each <entry>
element in the document and put it into a struct to perform some operations on it before moving to the next entry?
I'm already able to parse and store data into structs from a block of XML as above where only a single <entry>
element exists. That is to say that I am successfully able to store something like this into a struct:
<entry>
... several nested elements
</entry>
答案1
得分: 10
解析一个xmml文件直到达到entry元素是一种方法:
xmlFile, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer xmlFile.Close()
decoder := xml.NewDecoder(xmlFile)
total := 0
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
if startElement.Name.Local == "entry" {
// 在每个entry下执行需要的操作
}
}
}
英文:
Parsing an xmml file until you get to the entry elements is one way:
xmlFile, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer xmlFile.Close()
decoder := xml.NewDecoder(xmlFile)
total := 0
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
if startElement.Name.Local == "entry" {
// do what you need to do for each entry below
}
}
}
答案2
得分: 7
这是我会做的,根据文档和示例在http://golang.org/pkg/encoding/xml/
package main
import (
"encoding/xml"
"log"
)
// 代表一个<data>元素
type Data struct {
XMLName xml.Name `xml:"data"`
Entry []Entry `xml:"entry"`
}
// 代表一个<entry>元素
type Entry struct {
Name string `xml:"name"`
Age int `xml:"age"`
}
// 测试数据
var testXML string = `
<data>
<entry>
<name>John Doe</name>
<age>28</age>
</entry>
<entry>
<name>Jane Doe</name>
<age>29</age>
</entry>
<entry>
<name>Bob Doe</name>
<age>30</age>
</entry>
<entry>
<name>Beth Doe</name>
<age>31</age>
</entry>
</data>
`
func main() {
data := &Data{}
err := xml.Unmarshal([]byte(testXML), data)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(data.Entry); i++ {
log.Printf("%#v", data.Entry[i])
}
}
输出是:
main.Entry{Name:"John Doe", Age:28}
main.Entry{Name:"Jane Doe", Age:29}
main.Entry{Name:"Bob Doe", Age:30}
main.Entry{Name:"Beth Doe", Age:31}
英文:
Here's how I'd do it, per the documentation and examples in http://golang.org/pkg/encoding/xml/
<!-- language: lang-go -->
package main
import (
"encoding/xml"
"log"
)
// Represents a <data> element
type Data struct {
XMLName xml.Name `xml:"data"`
Entry []Entry `xml:"entry"`
}
// Represents an <entry> element
type Entry struct {
Name string `xml:"name"`
Age int `xml:"age"`
}
// Test data
var testXML string = `
<data>
<entry>
<name>John Doe</name>
<age>28</age>
</entry>
<entry>
<name>Jane Doe</name>
<age>29</age>
</entry>
<entry>
<name>Bob Doe</name>
<age>30</age>
</entry>
<entry>
<name>Beth Doe</name>
<age>31</age>
</entry>
</data>
`
func main() {
data := &Data{}
err := xml.Unmarshal([]byte(testXML), data)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(data.Entry); i++ {
log.Printf("%#v", data.Entry[i])
}
}
Output is:
main.Entry{Name:"John Doe", Age:28}
main.Entry{Name:"Jane Doe", Age:29}
main.Entry{Name:"Bob Doe", Age:30}
main.Entry{Name:"Beth Doe", Age:31}
答案3
得分: 0
只需确保正确处理EOF
for {
// 从XML文档中以流的形式读取标记。
t, err := decoder.Token()
if t == nil {
if err == nil {
continue
}
if err == io.EOF {
break
}
log.Fatal(err)
}
//...
}
英文:
Just make sure you handle EOF properly
for {
// Read tokens from the XML document in a stream.
t, err := decoder.Token()
if t == nil {
if err == nil {
continue
}
if err == io.EOF {
break
}
log.Fatal(err)
}
//...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论