在Golang中,如何对包含空格的XML参数进行编码?

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

In golang how do I encode an xml parameter containing a space?

问题

我有一个需要使用的 XML API 结构(这个结构不是我定义的,我无法更改):

<path><farmer id="ME7"/></path>

在 Go 语言中,我有以下代码:

type Path struct {
  XMLName xml.Name `xml:"path"`
  FarmerId string `xml:"farmer id,attr"`
}

pMux := &Path{FarmerId: "ME7"}

然而,Go 会对 pMux 进行编码并打印如下结果:

<path xmlns:farmer="farmer" farmer:id="ME7"></path>

我想要的结果是:

<path><farmer id="ME7"/></path>

我该如何实现这个目标?

谢谢。

英文:

I have this xml api construct I need to use (this construct is not defined by me and I cannot change it):

&lt;path&gt;&lt;farmer id=&quot;ME7&quot;/&gt;&lt;/path&gt;

In go I have:

type Path struct {
  XMLName xml.Name `xml:&quot;path&quot;`
  FarmerId string `xml:&quot;farmer id,attr&quot;`
}

pMux := &amp;Path{FarmerId: &quot;ME7&quot;}

However go encodes pMux and prints it as this:

&lt;path xmlns:farmer=&quot;farmer&quot; farmer:id=&quot;ME7&quot; &lt;/path&gt;

What I want is this:

&lt;path&gt;&lt;farmer id=&quot;ME7&quot;/&gt; &lt;/path&gt;

How can I achieve this?

Thx

答案1

得分: 1

这不是有效的XML。

我建议你再次检查一下你提到的API合同,因为他们不太可能要求无效的XML。

你可以得到最接近的有效XML如下:

<path><farmer id="ME7"></farmer></path>

为了生成上述(有效)的XML,你应该将你的类型定义为:

type Farmer struct {
   XMLName   xml.Name    `xml:"farmer"`
   Id        string      `xml:"id,attr"`
}

type Path struct {
    XMLName xml.Name `xml:"path"`
    Farmer  Farmer   `xml:"farmer"`
}

然后创建该值:

v := Path{Farmer: Farmer{Id: "ME7"}}

这里有一个运行示例:https://play.golang.org/p/abEqMc6HdK

英文:

That's not valid XML.

I would double check the API contract you mention since it's unlikely they require invalid XML.

The closest you can get to that is this valid XML.

&lt;path&gt;&lt;farmer id=&quot;ME7&quot;&gt;&lt;/farmer&gt;&lt;/path&gt;

In order to generate the above (valid) XML, you should define your types as:

type Farmer struct {
   XMLName   xml.Name    `xml:&quot;farmer&quot;`
   Id        string      `xml:&quot;id,attr&quot;`
}

type Path struct {
    XMLName xml.Name `xml:&quot;path&quot;`
    Farmer  Farmer   `xml:&quot;farmer&quot;`
}

And then create the value as:

v := Path{Farmer: Farmer{Id: &quot;ME7&quot;}}

See here for a running example: https://play.golang.org/p/abEqMc6HdK

答案2

得分: 1

XML是无效的,但如果你真的需要它以那种方式输出,可以使用正则表达式来修复它。这里有一个例子。

我假设你真的想要打开标签&lt;path farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;,而不是没有打开标签&lt;path farmer id=&quot;ME7&quot; &lt;/path&gt;,但无论哪种方式都可以使用正则表达式实现。

顺便说一下,你的问题在于你想要的东西不一致。你开始时想要&lt;path&gt;&lt;farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;,而@eugenioy的答案可以满足这个要求。然后以"我想要的是这样的:&lt;path farmer id=&quot;ME7&quot; &lt;/path&gt;"结束。而我的答案是针对这个要求的。

https://play.golang.org/p/A-sJhIgFZW

package main

import (
	"encoding/xml"
	"fmt"
	"regexp"
)

type Path struct {
	XMLName  xml.Name `xml:"path"`
	Farmer   string   `xml:"farmer,attr"`
	FarmerId string   `xml:"id,attr"`
}

func main() {
	path := &Path{
		FarmerId: "ME7",
	}
	data, err := xml.Marshal(path)
	if err != nil {
		fmt.Println(err)
		return
	}

	strData := string(data)

	// 使用正则表达式修复
	reg := regexp.MustCompile(`(farmer)(=&quot;&quot;)`)
	strData = reg.ReplaceAllString(strData, "$1")
	fmt.Println(strData) // &lt;path farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;
}
英文:

The XML is invalid, but if you really need it to come out like that, use a regular expression to fix it afterward. Here is an example.

I am assuming that you really want the open tag valid like so &lt;path farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;, instead of not having the open tag valid as you posted &lt;path farmer id=&quot;ME7&quot; &lt;/path&gt;, but either way is doable with regex.

BTW, your question is inconsistent about what you want. You start with wanting &lt;path&gt;&lt;farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;, which @eugenioy 's answer will accommodate. Then end with "What I want is this: &lt;path farmer id=&quot;ME7&quot; &lt;/path&gt;". Which my answer is geared toward.

https://play.golang.org/p/A-sJhIgFZW

package main

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

type Path struct {
	XMLName  xml.Name `xml:&quot;path&quot;`
	Farmer   string   `xml:&quot;farmer,attr&quot;`
	FarmerId string   `xml:&quot;id,attr&quot;`
}

func main() {
	path := &amp;Path{
		FarmerId: &quot;ME7&quot;,
	}
	data, err := xml.Marshal(path)
	if err != nil {
		fmt.Println(err)
		return
	}

	strData := string(data)

	// fix with regex
	reg := regexp.MustCompile(`(farmer)(=&quot;&quot;)`)
	strData = reg.ReplaceAllString(strData, &quot;$1&quot;)
	fmt.Println(strData) // &lt;path farmer id=&quot;ME7&quot;&gt;&lt;/path&gt;
}

huangapple
  • 本文由 发表于 2017年9月14日 02:36:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/46204640.html
匿名

发表评论

匿名网友

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

确定