Golang – 如何将 XML 文件的一部分提取为字符串?

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

Golang - How to extract part of an XML file as a string?

问题

我的 XML 大致如下所示:

<a>
  <b>
    <c>
      <d>TEXT</d>
   </c>
  </b>
</a>

我知道如何通过 xml.Unmarshal 函数将此代码分离,但是否有办法仅对特定深度执行 Unmarshal 操作?例如,如果我想获取一个字符串,该字符串为 "<c><d>TEXT</c></d>" 并将其传递给另一个函数,有没有办法实现?我尝试给 <b> 添加一个子字符集对象,但它仍然尝试解析其余的 XML...

英文:

My XML looks something like this:

&lt;a&gt;
  &lt;b&gt;
    &lt;c&gt;
      &lt;d&gt;TEXT&lt;/d&gt;
   &lt;/c&gt;
  &lt;/b&gt;
&lt;/a&gt;

I know how to separate this code via the xml.Unmarshal function, but is there any way to perform the Unmarshal action only to a certain depth? For example, if I wanted to get a string that says "<c><d>TEXT</c></d>" and pass that into another function? I tried giving <b> a child charset object, but it still tries to parse the rest of the XML...

答案1

得分: 10

我认为这是你要求的内容(考虑到你的评论)。

package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	type Result struct {
		Value string `xml:"b>c>d"`
	}
	v := Result{"none"}

	data := `
		<a>
			<b>
				<c>
					<d>TEXT</d>				
				</c>
			</b>
		</a>
	`
	err := xml.Unmarshal([]byte(data), &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}

	fmt.Printf("Value: %v\n", v.Value)
}

输出:

Value: TEXT

更新:根据lanZG的评论

func main() {
    type InnerResult struct {
	    Value string `xml:",innerxml"`
    }

    type Result struct {
        B InnerResult `xml:"b"`
    }
    v := Result{InnerResult{"none"}}

    data := `
        <a>
            <b>
                <c>
                    <d>TEXT</d>             
                </c>
            </b>
        </a>
    `
    err := xml.Unmarshal([]byte(data), &v)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }

    fmt.Printf("Value: %v\n", v.B.Value)
}

输出:

Value: 
                <c>
                    <d>TEXT</d>             
                </c>
英文:

I think this is what you are asking (consider your comment as well).

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

func main() {
	type Result struct {
		Value  string `xml:&quot;b&gt;c&gt;d&quot;`
	}
	v := Result{&quot;none&quot;}

	data := `
		&lt;a&gt;
			&lt;b&gt;
				&lt;c&gt;
					&lt;d&gt;TEXT&lt;/d&gt;				
				&lt;/c&gt;
			&lt;/b&gt;
		&lt;/a&gt;
	`
	err := xml.Unmarshal([]byte(data), &amp;v)
	if err != nil {
		fmt.Printf(&quot;error: %v&quot;, err)
		return
	}

	fmt.Printf(&quot;Value: %v\n&quot;, v.Value)
}

Output:

Value: TEXT

UPDATE: after lanZG's comment

func main() {
    type InnerResult struct {
	    Value string `xml:&quot;,innerxml&quot;`
    }

    type Result struct {
        B InnerResult `xml:&quot;b&quot;`
    }
    v := Result{InnerResult{&quot;none&quot;}}

    data := `
        &lt;a&gt;
            &lt;b&gt;
                &lt;c&gt;
                    &lt;d&gt;TEXT&lt;/d&gt;             
                &lt;/c&gt;
            &lt;/b&gt;
        &lt;/a&gt;
    `
    err := xml.Unmarshal([]byte(data), &amp;v)
    if err != nil {
        fmt.Printf(&quot;error: %v&quot;, err)
        return
    }

    fmt.Printf(&quot;Value: %v\n&quot;, v.B.Value)
}

Output:

Value: 
                &lt;c&gt;
                    &lt;d&gt;TEXT&lt;/d&gt;             
                &lt;/c&gt;

答案2

得分: 2

你可以使用嵌套的 XML 标签来简化 xml.Unmarshal 的操作。

以下是示例代码的链接:http://play.golang.org/p/XtCX7Dh45u

英文:

You can use nested xml tags to make it easier with xml.Unmarshal

here is how it would work: http://play.golang.org/p/XtCX7Dh45u

huangapple
  • 本文由 发表于 2014年12月17日 22:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/27527561.html
匿名

发表评论

匿名网友

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

确定