Encoding / xml handle non-displayable elements

From http://golang.org/pkg/encoding/xml/#Unmarshal

  • If the XML element contains a subitem that does not match any of the above rules, and the structure has a field with the tag ", any", unmarshal matches the subitem to this structure field.

I'm having trouble getting the rest of the XML envelope in my structure (to show that I have an incomplete mapping)

http://play.golang.org/p/mnFqAcguJQ

I know that you can use this particular method with bson.M from mgo packages using inline - but it looks like the map [string] {} interface is not the answer here.

EDIT: After some extra game, I found what, in my opinion, was an extra unexpected behavior.

Switching to string [], because the type begins to accept input, but not key / value pairs: http://play.golang.org/p/wCAJeeQa4m

I also planned to adapt encode / xml for html analysis. I do not see in the documentation that if an element exists more than once, it will save the last instance, and does not cross out the error: http://play.golang.org/p/0MY__R-Xi3

+4
source share
1 answer

Here: http://play.golang.org/p/iY8YlxYym0

c - , ",any", . c , []Tag xml:'",any"'... Tag, xml.Name, - ", innerxml".

, :

const xmlString = `<foo><a>1</a><b>2</b><c><c1>3</c1><c2>4</c2></c></foo>`
type Foo struct {
    A int   `xml:"a"`
    B int   `xml:"b"`
    C Extra `xml:"c"`
}

type Extra struct {
    Items []Tag `xml:",any"`
}

type Tag struct {
    XMLName xml.Name
    Content string `xml:",innerxml"`
}

:

type Foo struct {
    A int   `xml:"a"`
    B int   `xml:"b"`
    C struct {
        Items []struct {
            XMLName xml.Name
            Content string `xml:",innerxml"`
        } `xml:",any"`
    } `xml:"c"`
}

HTML go.net/html. XML- html .

+5

Source: https://habr.com/ru/post/1543281/


All Articles