英文:
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):
<path><farmer id="ME7"/></path>
In go I have:
type Path struct {
XMLName xml.Name `xml:"path"`
FarmerId string `xml:"farmer id,attr"`
}
pMux := &Path{FarmerId: "ME7"}
However go encodes pMux and prints it as this:
<path xmlns:farmer="farmer" farmer:id="ME7" </path>
What I want is this:
<path><farmer id="ME7"/> </path>
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.
<path><farmer id="ME7"></farmer></path>
In order to generate the above (valid) XML, you should define your types as:
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"`
}
And then create the value as:
v := Path{Farmer: Farmer{Id: "ME7"}}
See here for a running example: https://play.golang.org/p/abEqMc6HdK
答案2
得分: 1
XML是无效的,但如果你真的需要它以那种方式输出,可以使用正则表达式来修复它。这里有一个例子。
我假设你真的想要打开标签<path farmer id="ME7"></path>
,而不是没有打开标签<path farmer id="ME7" </path>
,但无论哪种方式都可以使用正则表达式实现。
顺便说一下,你的问题在于你想要的东西不一致。你开始时想要<path><farmer id="ME7"></path>
,而@eugenioy的答案可以满足这个要求。然后以"我想要的是这样的:<path farmer id="ME7" </path>
"结束。而我的答案是针对这个要求的。
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)(="")`)
strData = reg.ReplaceAllString(strData, "$1")
fmt.Println(strData) // <path farmer id="ME7"></path>
}
英文:
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 <path farmer id="ME7"></path>
, instead of not having the open tag valid as you posted <path farmer id="ME7" </path>
, but either way is doable with regex.
BTW, your question is inconsistent about what you want. You start with wanting <path><farmer id="ME7"></path>
, which @eugenioy 's answer will accommodate. Then end with "What I want is this: <path farmer id="ME7" </path>
". Which my answer is geared toward.
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)
// fix with regex
reg := regexp.MustCompile(`(farmer)(="")`)
strData = reg.ReplaceAllString(strData, "$1")
fmt.Println(strData) // <path farmer id="ME7"></path>
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论