在Go中遍历xml

给定一个类似于以下内容的xml块:


<data>

  <entry>

    ... several nested elements

  </entry>

  <entry>

    ... more nested elements

  </entry>

</data>

我如何遍历<entry>文档中的每个元素并将其放入结构中以对其执行一些操作,然后再移至下一个条目?


我已经能够如上所述将XML块中的数据解析并存储到结构中,而XML块中仅<entry>存在一个元素。那就是说我能够成功地将这样的东西存储到一个结构中:


<entry>

  ... several nested elements

</entry>


MYYA
浏览 239回答 3
3回答

慕哥9229398

只要确保您正确处理EOFfor {&nbsp; &nbsp; // Read tokens from the XML document in a stream.&nbsp; &nbsp; t, err := decoder.Token()&nbsp; &nbsp; if t == nil {&nbsp; &nbsp; &nbsp; &nbsp; if err == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if err == io.EOF {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; //...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go