golang xml 解析数组

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <flag>success</flag>
    <code>SUCCESS</code>
    <message>查询成功!</message>
    <items>
        <item>
            <warehouseCode>B01</warehouseCode>
            <itemCode>123</itemCode>
            <itemId>123</itemId>
            <inventoryType>CC</inventoryType>
            <quantity>3</quantity>
            <lockQuantity>0</lockQuantity>
        </item>
        <item>
            <warehouseCode>B01</warehouseCode>
            <itemCode>333</itemCode>
            <itemId>555</itemId>
            <inventoryType>ZP</inventoryType>
            <quantity>5239</quantity>
            <lockQuantity>0</lockQuantity>
        </item>
    </items>
</response>

对应的 struct

Response struct {
    XMLName xml.Name `xml:"response"`
    Flag    string   `xml:"flag"`
    Code    string   `xml:"code"`
    Message string   `xml:"message"`
    Items   []Item   `xml:"items"`
}

Item struct {
    WarehouseCode string `xml:"warehouseCode"`
    ItemCode      string `xml:"itemCode"`
    ItemID        string `xml:"itemId"`
    InventoryType string `xml:"inventoryType"`
    Quantity      string `xml:"quantity"`
    LockQuantity  string `xml:"lockQuantity"`
}


结果打印出来 xml Response Items : [{ }]

Items 竟然是 空 请求大佬 这样的 xml 不是这么定义的吗?

找个大佬 指导一下咯 ?

阅读 9k
1 个回答

type Response struct {
    XMLName xml.Name `xml:"response"`
    Flag    string   `xml:"flag"`
    Code    string   `xml:"code"`
    Message string   `xml:"message"`
    Items   []Item   `xml:"items>item"`
}

type Item struct {
    XMLName       xml.Name `xml:"item"`
    WarehouseCode string   `xml:"warehouseCode"`
    ItemCode      string   `xml:"itemCode"`
    ItemID        string   `xml:"itemId"`
    InventoryType string   `xml:"inventoryType"`
    Quantity      string   `xml:"quantity"`
    LockQuantity  string   `xml:"lockQuantity"`
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题