Go:XML解组嵌套的键值对

huangapple go评论118阅读模式
英文:

Go: XML Unmarshal nested key-value pairs

问题

我开始了一个处理XML输入的项目,但在unmarshal方法中遇到了问题。我试图将下面的XML解组为一个结构体。在XML中有多个'value'标签,这是可以的,但在'value'标签内部,可以有一个带有值的'value'标签,或者有多个带有'key'和'value'标签的'value'标签。我试图在Go结构体中捕获这个情况,但迄今为止没有成功。

<lines>
    <values>
        <key>Key_1</key>
        <value>Value_1</value>
    </values>
    <values>
        <key>Key_2</key>
        <value>
            <key>NestedKey_1</key>
            <value>NestedValue_1</value>
        </value>
        <value>
            <key>NestedKey_2</key>
            <value>NestedValue_2</value>
        </value>
    </values>
</lines>

Go结构体:

type Value struct {
    Key   string `xml:"key"`
    Value string `xml:"value"`
}

type Values struct {
    Key   string  `xml:"key"`
    Value []Value `xml:"value"`
}

type Lines struct {
    Values []Values `xml:"values"`
}

当我打印unmarshal的输出时,我可以看到Key_1和Key_2以及嵌套的键值对,但是我在输出中看不到Value_1。这是显而易见的,因为它是一个字符串而不是数组,有人有办法解决这个问题吗?

Playground: http://play.golang.org/p/I4U0lhPt5U

英文:

I started a project to process XML input, I ran into a problem with the unmarshal method. I'm trying to umarshal the XML below into a struct. The are multiple 'values' tag, which is fine, but within the 'values' tag there can be either one 'value' tag with a value. Or there are multiple 'value' tags with a 'key'&'value' tag inside. I'm trying to capture this in a Go struct, but no luck so far..

&lt;lines&gt;
&lt;values&gt;
    &lt;key&gt;Key_1&lt;/key&gt;
    &lt;value&gt;Value_1&lt;/value&gt;
&lt;/values&gt;
&lt;values&gt;
    &lt;key&gt;Key_2&lt;/key&gt;
    &lt;value&gt;
        &lt;key&gt;NestedKey_1&lt;/key&gt;
        &lt;value&gt;NestedValue_1&lt;/value&gt;
    &lt;/value&gt;
    &lt;value&gt;
        &lt;key&gt;NestedKey_2&lt;/key&gt;
        &lt;value&gt;NestedValue_2&lt;/value&gt;
    &lt;/value&gt;
&lt;/values&gt;

</lines>

Go struct:

type Value struct {
    Key string `xml:&quot;key&quot;`
    Value string `xml:&quot;value&quot;`
}

type Values struct {
    Key string `xml:&quot;key&quot;`
    Value []Value `xml:&quot;value&quot;`
}

type Lines struct {
    Values []Values `xml:&quot;values&quot;`
}

When I print the unmarshal output, I do see Key_1 and Key_2 and the nested key-value pairs, but I don't see Value_1 in the output. Which is obvious since it's a string and not an array, anyone an idea how to work around this?

Playground: http://play.golang.org/p/I4U0lhPt5U

答案1

得分: 1

两个需要注意的事项:

  • 表示 XML 元素内容的 struct 类型可能有一个特殊字段 "XMLName",类型为 xml.Name

    这可以用来使类型的名称与 XML 元素的名称不同。

  • 通过 "xml" 标签为你的 struct 类型的字段指定的 XML 元素的名称可以使用 > 符号来指定元素的嵌套关系,例如:

      Foo string `xml:"a>b>c"`
    

    这可以用来跳过不需要的中间(包围)元素,直接提取感兴趣的子元素。

所有这些内容在 该包的 Unmarshal() 函数的文档中有详细解释

据说,这些功能应该能够帮助你解决问题。

如果你发现你的情况确实非常特殊,很难通过普通的解组来处理,你可以尝试使用 XPath 来解决。

英文:

Two things to look at:

  • A struct type representing the contents of an XML element might have a special field "XMLName" of type xml.Name.

    This can be used to have types named differently from XML elements.

  • The name of an XML element specified for a field of your struct type via the "xml" tag might specify nesting of elements using the &gt; notation, like in

      Foo string `xml:&quot;a&gt;b&gt;c&quot;`
    

    This can be used to skip unneeded intermediate (enclosing) elements and directly extract their child elements of interest.

All this stuff is explained in the package's doc for the Unmarshal() func.

Supposedly, these capabilities should allow you to deal with your problem.

If you will find that your case is truly pathological and is hard to deal with plain unmarshaling, you might try to approach it using XPath.

huangapple
  • 本文由 发表于 2015年9月12日 00:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/32528184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定