gpt4 book ai didi

xml - Golang : get inner xml from xml with xml.解码

转载 作者:IT王子 更新时间:2023-10-29 01:54:51 30 4
gpt4 key购买 nike

我有这样简单的 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<nexgen_audio_export>
<audio ID="id_1667331726_30393658">
<type>Song</type>
<status>Playing</status>
<played_time>09:41:18</played_time>
<composer>Frederic Delius</composer>
<title>Violin Sonata No.1</title>
<artist>Tasmin Little, violin; Piers Lane, piano</artist>
<comments>
<p>Comment line1</p>
<p>Comment <b>line2</b></p>
<p>Comment line3</p>
</comments>
</audio>
</nexgen_audio_export>

如何从 xml:"nexgen_audio_export>audio>comments" 获取内部 XML所有标签( <p><b> 等)都使用 xml.decode?

谢谢,美联社

最佳答案

来自 https://golang.org/pkg/encoding/xml/#Unmarshal :

If the struct has a field of type []byte or string with tag ",innerxml", Unmarshal accumulates the raw XML nested inside the element in that field.

您可以只将结构标记 ",innerxml" 用于您元素内部的 string[]byte 类型的字段'试图从中提取 XML。您需要使用子结构。另请注意,XML 库的选择查询从第一个元素开始(这很奇怪)。因此,您不能以 nexgen_audio_export> 开始标记,而是直接转到 audio>

这是工作示例代码:

package main

import (
"encoding/xml"
"fmt"
)

// Encoding had to be changed to UTF-8
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<nexgen_audio_export>
<audio ID="id_1667331726_30393658">
<type>Song</type>
<status>Playing</status>
<played_time>09:41:18</played_time>
<composer>Frederic Delius</composer>
<title>Violin Sonata No.1</title>
<artist>Tasmin Little, violin; Piers Lane, piano</artist>
<comments>
<p>Comment line1</p>
<p>Comment <b>line2</b></p>
<p>Comment line3</p>
</comments>
</audio>
</nexgen_audio_export>`)

type audio struct {
Comments struct {
InnerXML string `xml:",innerxml"`
} `xml:"audio>comments"`
}

func main() {
var a audio
err := xml.Unmarshal(input, &a)
if err != nil {
panic(err)
}

fmt.Println(a.Comments.InnerXML)
}

Playground 链接:https://play.golang.org/p/LAL2V0zExc

关于xml - Golang : get inner xml from xml with xml.解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com