duanbei1709 2018-02-14 17:11
浏览 14
已采纳

解组包含数据和子级的xml节点

Im not sure this is valid xml, unfortunately its what I'm having to deal with - looking for suggestions on how to accomplish.

my xml looks like this:

<report>
    <sort>
        <field>type<order>ascending</order></field>
    </sort>
</report>

I am attempting to unmarshall into the following structs:

type Sort struct {
    Field string `xml:"field"`
    Order string `xml:"field>order"`
}

type Report struct {
    Sort Sort       `xml:"sort"`
}

unfortunately this is throwing the error:

Unmarshalling error: v7.Sort field "Field" with tag "field" conflicts with field "Order" with tag "field>order"

Is there a built in way of achieving this or am I looking at some custom unmarshalling

UPDATE: At least according to this answer it appears this should be valid if slightly ugly xml: Can a XML element contain text and child elements at the same time?

  • 写回答

1条回答 默认 最新

  • doutan6286 2018-02-14 17:36
    关注

    A single XML element can only be mapped to a single struct field, and your model tries to map <field> to 2 struct fields, so it's not allowed.

    The character data of <field> and the child element <order> are at the same "level", they are siblings, so they must be in the same struct. But they are different types of nodes: to get the character data, use the xml:",chardata" struct tag, and to get the value of the <order> child element, use the xml:"order" struct tag.

    So use the following Go model:

    type Field struct {
        Value string `xml:",chardata"`
        Order string `xml:"order"`
    }
    
    type Sort struct {
        Field Field `xml:"field"`
    }
    
    type Report struct {
        Sort Sort `xml:"sort"`
    }
    

    Parsing your input XML into this model:

    func main() {
        var r Report
        err := xml.Unmarshal([]byte(src), &r)
        fmt.Printf("%+v %v", r, err)
    }
    
    const src = `<report>
        <sort>
            <field>type<order>ascending</order></field>
        </sort>
    </report>`
    

    Output is (try it on the Go Playground):

    {Sort:{Field:{Value:type Order:ascending}}} <nil>
    

    Since Sort only contains a single Field field, we can simplify the model to this:

    type Field struct {
        Value string `xml:",chardata"`
        Order string `xml:"order"`
    }
    
    type Report struct {
        SortField Field `xml:"sort>field"`
    }
    

    And it works the same way, gives a similar output (try this one on the Go Playground):

    {SortField:{Value:type Order:ascending}} <nil>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图