我正在使用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
}
}
]
答案 0 :(得分:0)
您必须将此客户集合包装在另一个结构中
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类型自定义名称。