解组 XML 中的数组元素:检索所有元素,而不仅仅是第一个
在 Golang 中使用 xml.Unmarshal 解组 XML 数组时( []byte(p.Val.Inner), &t),您可能会遇到仅检索第一个元素的情况。要解决此问题,请利用 xml.Decoder 并重复调用其 Decode 方法。
解组所有 XML 数组元素的步骤:
- 创建一个新的 xml。使用 xml.NewDecoder(bytes.NewBufferString(VV)) 进行解码,其中 VV 是包含数组元素的 XML 字符串。
- 输入一个循环来处理每个 XML 元素:
- 声明一个变量 t目标切片类型(例如 HostSystemIdentificationInfo)。
- 调用 d.Decode(&t) 将下一个 XML 元素解组到 t 变量中。
- 重复步骤 2-4,直到 d.Decode(&t) 解组到 t 变量。 Decode(&t) 调用返回 io.EOF。
修改后的 Golang 代码:
<code class="go">package main import ( "bytes" "encoding/xml" "fmt" "io" "log" ) type HostSystemIdentificationInfo []struct { IdentiferValue string `xml:"identifierValue"` IdentiferType struct { Label string `xml:"label"` Summary string `xml:"summary"` Key string `xml:"key"` } `xml:"identifierType"` } func main() { d := xml.NewDecoder(bytes.NewBufferString(VV)) for { var t HostSystemIdentificationInfo err := d.Decode(&t) if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Println(t) } } const VV = `<hostsystemidentificationinfo xsi:type="HostSystemIdentificationInfo"> <identifiervalue> unknown</identifiervalue> <identifiertype> <label>Asset Tag</label> <summary>Asset tag of the system</summary> <key>AssetTag</key> </identifiertype> </hostsystemidentificationinfo> <hostsystemidentificationinfo xsi:type="HostSystemIdentificationInfo"> <identifiervalue>Dell System</identifiervalue> <identifiertype> <label>OEM specific string</label> <summary>OEM specific string</summary> <key>OemSpecificString</key> </identifiertype> </hostsystemidentificationinfo> <hostsystemidentificationinfo xsi:type="HostSystemIdentificationInfo"> <identifiervalue>5[0000]</identifiervalue> <identifiertype> <label>OEM specific string</label> <summary>OEM specific string</summary> <key>OemSpecificString</key> </identifiertype> </hostsystemidentificationinfo> <hostsystemidentificationinfo xsi:type="HostSystemIdentificationInfo"> <identifiervalue>REDACTED</identifiervalue> <identifiertype> <label>Service tag</label> <summary>Service tag of the system</summary> <key>ServiceTag</key> </identifiertype> </hostsystemidentificationinfo>`</code>
示例输出:
[{ unknown {Asset Tag Asset tag of the system AssetTag}}] [{Dell System {OEM specific string OEM specific string OemSpecificString}}] [{5[0000] {OEM specific string OEM specific string OemSpecificString}}] [{REDACTED {Service tag Service tag of the system ServiceTag}}]
通过使用 xml.Decoder 并重复调用 Decode,可以成功检索出 XML 数组中的所有元素,解决了只能获取第一个元素的问题。
以上是如何在 Golang 中检索 XML 数组中的所有元素而不仅限于第一个元素?的详细内容。更多信息请关注PHP中文网其他相关文章!

Golang在实际应用中表现出色,以简洁、高效和并发性着称。 1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能