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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?