鍍金池/ 問答/GO/ golang xml 解析數(shù)組

golang xml 解析數(shù)組

<?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>

對應(yīng)的 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"`
}


結(jié)果打印出來 xml Response Items : [{ }]

Items 竟然是 空 請求大佬 這樣的 xml 不是這么定義的嗎?

找個(gè)大佬 指導(dǎo)一下咯 ?

回答
編輯回答
野橘

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"`
}
2018年9月3日 23:47