arrays - XML中的结构数组没有包装节点?
问题描述
我有一个客户拥有 XML,他们似乎想要一个库存数组,但是每个项目一个接一个地列出,没有包装节点。
这是我正在做的一个示例,但每个项目都有一个包装节点。有没有办法让它们在“root”下一个接一个地列出?
测试代码:
package main
import (
"encoding/xml"
"fmt"
"os"
"strconv"
)
func main() {
type InventoryItem struct {
XMLName xml.Name
ItemName string `xml:"Name"`
ItemDescription string `xml:"Description"`
}
type XMLEnvelop struct {
XMLName xml.Name `xml:"root"`
Inventory []InventoryItem `xml:"item"`
Records int `xml:"records"`
}
var items []InventoryItem
for i := 1; i < 6; i++ {
items = append(items, InventoryItem{XMLName: xml.Name{Local: "item" + strconv.Itoa(i)}, ItemName: "Test " + strconv.Itoa(i), ItemDescription: "Description " + strconv.Itoa(i)})
}
v := &XMLEnvelop{Records: 1, Inventory: items}
output, err := xml.MarshalIndent(v, "", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
// Write the output to check
os.Stdout.Write(output)
//Here is where I would make the request
}
测试输出:
<root>
<item1>
<Name>Test 1</Name>
<Description>Description 1</Description>
</item1>
<item2>
<Name>Test 2</Name>
<Description>Description 2</Description>
</item2>
<item3>
<Name>Test 3</Name>
<Description>Description 3</Description>
</item3>
<item4>
<Name>Test 4</Name>
<Description>Description 4</Description>
</item4>
<item5>
<Name>Test 5</Name>
<Description>Description 5</Description>
</item5>
<records>1</records>
</root>
去游乐场: https: //play.golang.org/p/3DRUABFEQvC
这是他们似乎正在寻找的输出......无论出于何种原因。
<root>
<Name>Test 1</Name>
<Description>Description 1</Description>
<Name>Test 2</Name>
<Description>Description 2</Description>
<Name>Test 3</Name>
<Description>Description 3</Description>
<Name>Test 4</Name>
<Description>Description 4</Description>
<Name>Test 5</Name>
<Description>Description 5</Description>
<records>1</records>
</root>
解决方案
您可以实现自定义封送拆收器。
type InventoryItem struct {
XMLName xml.Name
ItemName string `xml:"Name"`
ItemDescription string `xml:"Description"`
}
func (i InventoryItem) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// Ignore the passed in `start` argument, it represents the
// parent InventoryItem element of the Name and Description.
// Declare types to represent the elements you want to encode,
// initialize them to the item's field values and encode them.
//
// Use the ",chardata" option to tell the encoder to encode the
// field's value directly rather than as a child element.
type Name struct {
Value string `xml:",chardata"`
}
if err := e.Encode(Name{i.ItemName}); err != nil {
return err
}
type Description struct {
Value string `xml:",chardata"`
}
return e.Encode(Description{i.ItemDescription})
}
推荐阅读
- vue.js - Nuxt.JS 创建自定义 UI 材质组件
- android - 如何更改 PagingSource 或 RemoteMediator 中的 Paging3 问题
- c++ - VS Code:在 browse.path 中找不到包含文件,但 IDE 在我键入时识别出该名称
- c# - 是否可以在 Web App Bot 中翻译自适应卡片?
- python - 检测在一段时间内按下的键 Python
- google-analytics - Add Brand To Analytics.js Ecommerce's AddTransaction?
- ios - 在 YouTube 和 Instagram IOS 等应用程序中使用什么框架播放视频
- flutter - 如何编写一个函数(在flutter-dart中),以便在我们调用该函数时它接受某些参数?
- unix - 密钥库文件格式无效(z/OS Unix 系统服务)
- amazon-web-services - AWS ALB - 如何将现有会话路由到服务器 A,并将新会话路由到服务器 B?