xml - XML 编码:混合属性和元素
问题描述
我有一个关于编组 Go XML 的问题:我明白了:
<root abc="">
<element></element>
</root>
但我想要这样:
<root>
<element abc=""></element>
</root>
(属性abc
在子元素中)。
这(很容易)可能吗?
我的代码:
package main
import (
"encoding/xml"
"fmt"
"os"
)
type foo struct {
XMLName xml.Name `xml:"root"`
Abc string `xml:"abc,attr"`
Element string `xml:"element"`
}
func main() {
f := foo{}
a, err := xml.MarshalIndent(f, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(0)
}
fmt.Println(string(a))
}
解决方案
您可以定义您的结构,如:
type foo struct {
XMLName xml.Name `xml:"root"`
Element struct{
xml.Name `xml:"element"`
Abc string `xml:"abc,attr"`
}
}
推荐阅读
- php - 我的 .php 文件没有在 WAMP 服务器上运行,它正在尝试保存文件
- laravel - 密码中是否存在 defaults() 方法?
- html - 请,我如何使这个 HTML 代码响应
- c++ - 使用前哨 C++ 添加数字列表
- c++ - 不同的 c++ 库方法之间的区别?
- reactjs - Swr 的缓存更新,但 UI 不是无缘无故的 - swr hook Next.js ( 带有 typescript )
- flutter - (Flutter/Dart) 如何将 FutureBuilder 列表转换为 ExpandableListView?
- installation - 如何在安装时更改生成文件中的 CMAKE_INSTALL_PREFIX
- c++ - 在 PVC 中使用 libxml 文件解析期间如何解决 Error extra 'xzread()'?
- django - 有没有比 ModelMultipleChoiceField 更好的解决方案?