当前位置: > > > > XML 编组无需 Root 即可生成 XML
来源:stackoverflow
2024-04-22 08:27:40
0浏览
收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《XML 编组无需 Root 即可生成 XML》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
问题内容
我正在 golang 中开发一个 rest api,并且能够将数据编组为 xml 和 json。我的结构有多个记录,但 xml 编组不会为这些记录添加根。因此 xml 无效。
我看到一些问题通过设置 xmlname 得到了解决,但我认为这只会将我的客户类型更改为其他类型。
我仍然可能尝试添加 xml 编组字节并连接。但我不确定我是否必须这样做。
编辑:显然 xml 已正确编组,因为我正在编组一个内部包含多个对象的结构。然后编组为每个对象创建单独的 xml 文档。那么问题是其余客户端可以使用什么来使用数据,或者在其余响应中发送多个 xml 文档是一种不好的做法吗?浏览器无法显示生成的 xml。
要返回单个 xml 文档,我必须将列表放入一个结构中,然后可以对其进行编组。我不确定如何以这种方式使用 sqlx 初始化结构。
添加以下代码不起作用:
type custs struct { list []customer `xml:"customer"` } custs customers.[]customer = []customer{}
代码:
package main import ( "database/sql" json "encoding/json" "encoding/xml" "fmt" "net/http" ) type customer struct { customerid int `db:"customerid"` firstname string `db:"firstname"` lastname string `db:"lastname"` company sql.nullstring `db:"company"` } func tstsqlite(w http.responsewriter, r *http.request) { contenttype := r.header.get("content-type") customers := []customer{} var err error fmt.print("executing query") err = chinookdb.select(&customers, "select customerid, firstname,lastname, company from customers") if err != nil { panic(err) } for _, g := range customers { fmt.println("firstname:", g.firstname, "lastname:", g.lastname) } var data []byte if contenttype == "application/xml" { data, err = xml.marshal(customers) } if contenttype == "application/json" || contenttype != "application/xml" { data, err = json.marshal(customers) } w.header().set("content-type", "application/json") w.write(data) }
我得到的是以下 xml:
<customer> <customerid>1</customerid> <firstname>luís</firstname> <lastname>gonçalves</lastname> <company> <string>embraer - empresa brasileira de aeronáutica s.a.</string> <valid>true</valid> </company> </customer> <customer> <customerid>2</customerid> <firstname>leonie</firstname> <lastname>köhler</lastname> <company> <string /> <valid>false</valid> </company> </customer> <customer> <customerid>3</customerid> <firstname>françois</firstname> <lastname>tremblay</lastname> <company> <string /> <valid>false</valid> </company> </customer>
但我认为应该是这样的:
<customers> <customer> <customerid>1</customerid> <firstname>luís</firstname> <lastname>gonçalves</lastname> <company> <string>embraer - empresa brasileira de aeronáutica s.a.</string> <valid>true</valid> </company> </customer> <customer> <customerid>2</customerid> <firstname>leonie</firstname> <lastname>köhler</lastname> <company> <string /> <valid>false</valid> </company> </customer> <customer> <customerid>3</customerid> <firstname>françois</firstname> <lastname>tremblay</lastname> <company> <string /> <valid>false</valid> </company> </customer> </customers>
编组的 json 看起来是正确的,因为客户位于括号中:
[ { "CustomerID":1, "FirstName":"Luís", "LastName":"Gonçalves", "Company":{ "String":"Embraer - Empresa Brasileira de Aeronáutica S.A.", "Valid":true } }, { "CustomerID":2, "FirstName":"Leonie", "LastName":"Köhler", "Company":{ "String":"", "Valid":false } } ]
解决方案
您必须将此客户集合包装在另一个结构中,如下所示
package main import ( "encoding/xml" "fmt" "os" ) func main() { type Customer struct { XMLName xml.Name `xml:"Customer"` CustomerID int `db:"CustomerId"` FirstName string `db:"FirstName"` LastName string `db:"LastName"` Company string `db:"Company"` } type Customers struct { List []Customer } csts := []Customer{ Customer{CustomerID: 1, FirstName: "John", LastName: "Doe", Company: "Demo Company"}, Customer{CustomerID: 2, FirstName: "John2", LastName: "Doe2", Company: "Demo Company2"}, } res := Customers { List: csts, } output, err := xml.MarshalIndent(res, " ", " ") if err != nil { fmt.Printf("error: %v\n", err) } os.Stdout.Write(output) }
默认它可以采用构造的名称。您还可以使用 xml.name 类型自定义名称。
今天关于《XML 编组无需 Root 即可生成 XML》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!