Golang XML etree:在添加外层时出现意外的<></>。

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

Golang XML etree: Unexpected <></> when adding outer layer

问题

我尝试使用名为https://github.com/beevik/etree的golang库向我的XML中添加外部XML。假设我的XML是&lt;Foo&gt;&lt;/Foo&gt;,但是当尝试添加外层时,会出现一个额外的空标签,如&lt;&gt;&lt;/&gt;。如何移除它?

这是我的代码片段:
https://go.dev/play/p/z6E5Ha3hWmm

结果是:

&lt;soap:Envelope&gt;
  &lt;soap:Header/&gt;
  &lt;soap:Body/&gt;
  &lt;&gt;&lt;Foo/&gt;&lt;/&gt;
&lt;/soap:Envelope&gt;

我期望的结果是:

&lt;soap:Envelope&gt;
  &lt;soap:Header/&gt;
  &lt;soap:Body/&gt;
  &lt;Foo/&gt;
&lt;/soap:Envelope&gt;

&lt;Foo/&gt;之间有&lt;&gt;&lt;/&gt;

编辑
&lt;Foo/&gt;是动态的,所以它可以是&lt;Bar/&gt;或其他任何内容。

英文:

I tried to add outer XML to my XML using golang library that is called https://github.com/beevik/etree
assume my XML is &lt;Foo&gt;&lt;/Foo&gt; but when trying to add the outer layer there's an additional empty tag like &lt;&gt;&lt;/&gt;
how to remove that?

Here's my snippet:
https://go.dev/play/p/z6E5Ha3hWmm

and the result is

&lt;soap:Envelope&gt;
  &lt;soap:Header/&gt;
  &lt;soap:Body/&gt;
  &lt;&gt;&lt;Foo/&gt;&lt;/&gt;
&lt;/soap:Envelope&gt;

I expected the result is

&lt;soap:Envelope&gt;
  &lt;soap:Header/&gt;
  &lt;soap:Body/&gt;
  &lt;Foo/&gt;
&lt;/soap:Envelope&gt;

There's <></> between <Foo/>

EDIT
The &lt;Foo/&gt; is dynamic, so it can be &lt;Bar/&gt; or anything else

答案1

得分: 1

原文:Turns out, I just need to add .Root()
So it should be soapBody.AddChild(result.Root())

翻译结果:原来如此,我只需要添加.Root()
所以应该是soapBody.AddChild(result.Root())

英文:

Turns out, I just need to add .Root()
So it should be soapBody.AddChild(result.Root())

https://go.dev/play/p/F84DOOGo0p-

答案2

得分: 0

我对etree比对golang更熟悉,所以你可能需要对以下内容持保留态度(并重新编写),但你需要完成一些基本的与xml相关的操作:

首先,你需要将&lt;Foo&gt;元素包装在一个根元素中,以使其成为一个格式良好的xml文档,然后在该文档中选择该元素。

其次,在第二个(root)文档中,选择要添加&lt;Foo&gt;元素的位置,然后将其添加进去。

代码应该如下所示:

    result := etree.NewDocument()
	root := etree.NewDocument()

	root.ReadFromString("&lt;soap:Envelope&gt;&lt;soap:Header/&gt;&lt;soap:Body/&gt;&lt;/soap:Envelope&gt;")
	result.ReadFromString("&lt;root&gt;&lt;Foo/&gt;&lt;/root&gt;")

	source := result.FindElements("//Foo")[0]
	destination := root.FindElements("//soap:Envelope")[0]

	destination.AddChild(source)

参考这里的更新代码片段。

英文:

I'm far more familiar with etree than with golang, so you may have to take the following with a grain (or two) of salt (and re-write it), but you need to do a couple of basic xml-related things:

First, you have to wrap your &lt;Foo&gt; element in a root element to make it well formed xml document and then, inside that document, select that element.

Second, in the second (root) document, select the location where you want the &lt;Foo&gt; element to be added and then add it.

So it should look like this:

    result := etree.NewDocument()
	root := etree.NewDocument()

	root.ReadFromString(&quot;&lt;soap:Envelope&gt;&lt;soap:Header/&gt;&lt;soap:Body/&gt;&lt;/soap:Envelope&gt;&quot;)
	result.ReadFromString(&quot;&lt;root&gt;&lt;Foo/&gt;&lt;/root&gt;&quot;)

	source := result.FindElements(&quot;//Foo&quot;)[0]
	destination := root.FindElements(&quot;//soap:Envelope&quot;)[0]

	destination.AddChild(source)

See the updated snippet here.

huangapple
  • 本文由 发表于 2022年10月5日 00:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73950771.html
匿名

发表评论

匿名网友

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

确定