GOlang XML无法使用xml.MarshalIndent在一个标签中创建值和属性。

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

GOlang XML can't create a value and an attribute in one tag using xml.MarshalIndent

问题

新手学习Go语言,想要使用"encoding/xml"包中的xml.MarshalIndent函数创建一个XML文件。其中一个要求是创建如下的标签:

<Attributes>
    <Attribute name="Me">I really like this GO lang</Attribute>
    <Attribute name="problem">the xml package is not behaving</Attribute>
</Attributes>

标签是一个带有值和属性的标签数组,但是无论我怎么尝试(我尝试了很多不同的方法),都无法使其正常工作。

我编写的代码生成了以下结果:

<Attributes>
    <Attribute Name="J3K Solutions">
        <Attribute>IT Support</Attribute>
    </Attribute>
    <Attribute Name="stakoverflow">
        <Attribute>The place I go for help</Attribute>
    </Attribute>
</Attributes>

然而,我需要它看起来像这样:

<Attributes>
    <Attribute Name="J3K Solutions">IT Support</Attribute>
    <Attribute Name="stakoverflow">The place I go for help</Attribute>
</Attributes>

我已经阅读了文档,并且尝试了几个小时,但还是无法解决。有人知道是否可行吗?

代码:

package main

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

type attributes struct {
	Name      string `xml:",attr"`
	Attribute string `xml:",chardata"`
}

type fXML struct {
	MessageID string `xml:"MessageID,attr"`
	Timestamp string `xml:"timestamp,attr"`
	Version   string `xml:"version,attr"`
	Header    struct {
		From struct {
			Credential struct {
				Domain   string `xml:"domain,attr"`
				Identity string `xml:"Identity"`
			} `xml:"Credential"`
		} `xml:"From"`
		To struct {
			Credential struct {
				Domain   string `xml:"domain,attr"`
				Identity string `xml:"Identity"`
			} `xml:"Credential"`
		} `xml:"To"`
		Attributes struct {
			Attribute []attributes
		}
	} `xml:"Header"`
}

func main() {

	rdata := &fXML{}
	t := time.Now()
	rdata.MessageID = "I'm really liken this GO language!"
	rdata.Timestamp = t.Format("2006-01-02T15:04:05")
	rdata.Version = "1.0"
	rdata.Header.From.Credential.Domain = "j3ksolutions.com"
	rdata.Header.From.Credential.Identity = "J3K Solutions"
	rdata.Header.To.Credential.Domain = "stackoverflow.com"
	rdata.Header.To.Credential.Identity = "stackoverflow"
	rdata.Header.Attributes.Attribute = append(rdata.Header.Attributes.Attribute,
		attributes{
			Name:      "J3K Solutions",
			Attribute: "IT Support and Custom Programing",
		})
	rdata.Header.Attributes.Attribute = append(rdata.Header.Attributes.Attribute,
		attributes{
			Name:      "stakoverflow",
			Attribute: "The place I go for help",
		})

	if m, err2 := xml.MarshalIndent(rdata, "", "\t"); err2 != nil {
		panic("xml.MarshalIndent FAILED: " + err2.Error())
	} else {
		xmlheader := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n")
		m = append([]byte(xmlheader), m...)
		fmt.Printf("\n%s\n\n", m)
	}
}
英文:

New to Go and have got to create and xml file using xml.MarshalIndent from the "encoding/xml" package. One requirement is to create a tag like so:

`

   &lt;Attributes&gt;
&lt;Attribute name=&quot;Me&quot;&gt;I really like this GO lang&lt;/Attribute&gt;
&lt;Attribute name=&quot;problem&quot;&gt;the xml package is not behaving&lt;/Attribute&gt;
&lt;/Attributes&gt;

`

An array of tags with a value and an attribute, but no matter what I do (an i have tried lots a different ways), I just can not get it to work.

The code I wrote generates this:
`

&lt;Attributes&gt;
&lt;Attribute Name=&quot;J3K Solutions&quot;&gt;
&lt;Attribute&gt;IT Support&lt;/Attribute&gt;
&lt;/Attribute&gt;
&lt;Attribute Name=&quot;stakoverflow&quot;&gt;
&lt;Attribute&gt;The place I go for help&lt;/Attribute&gt;
&lt;/Attribute&gt;
&lt;/Attributes&gt;

`

However I need it to look like this:
`

&lt;Attributes&gt;
&lt;Attribute Name=&quot;J3K Solutions&quot; &gt;IT Support&lt;/Attribute&gt;
&lt;Attribute Name=&quot;stakoverflow&quot; &gt;The place I go for help&lt;/Attribute&gt;
&lt;/Attributes&gt;

`
I've been reading the docs, and trying to get it working for hours now.
Anyone know if this is doable?

Code:

 package main
import (
&quot;encoding/xml&quot;
&quot;fmt&quot;
&quot;time&quot;
)
type attributes struct {
Name      string `xml:&quot;,attr&quot;`
Attribute string `xml:&quot;Attribute&quot;`
}
type fXML struct {
MessageID string `xml:&quot;MessageID,attr&quot;`
Timestamp string `xml:&quot;timestamp,attr&quot;`
Version   string `xml:&quot;version,attr&quot;`
Header    struct {
From struct {
Credential struct {
Domain   string `xml:&quot;domain,attr&quot;`
Identity string `xml:&quot;Identity&quot;`
} `xml:&quot;Credential&quot;`
} `xml:&quot;From&quot;`
To struct {
Credential struct {
Domain   string `xml:&quot;domain,attr&quot;`
Identity string `xml:&quot;Identity&quot;`
} `xml:&quot;Credential&quot;`
} `xml:&quot;To&quot;`
Attributes struct {
Attribute []attributes
}
} `xml:&quot;Header&quot;`
}
func main() {
rdata := &amp;fXML{}
t := time.Now()
rdata.MessageID = &quot;I&#39;m really liken this GO language!&quot;
rdata.Timestamp = t.Format(&quot;2006-01-02T15:04:05&quot;)
rdata.Version = &quot;1.0&quot;
rdata.Header.From.Credential.Domain = &quot;j3ksolutions.com&quot;
rdata.Header.From.Credential.Identity = &quot;J3K Solutions&quot;
rdata.Header.To.Credential.Domain = &quot;stackoverflow.com&quot;
rdata.Header.To.Credential.Identity = &quot;stackoverflow&quot;
rdata.Header.Attributes.Attribute = append(rdata.Header.Attributes.Attribute,
attributes{
Name:      &quot;J3K Solutions&quot;,
Attribute: &quot;IT Support and Custom Programing&quot;,
})
rdata.Header.Attributes.Attribute = append(rdata.Header.Attributes.Attribute,
attributes{
Name:      &quot;stakoverflow&quot;,
Attribute: &quot;The place I go for help&quot;,
})
if m, err2 := xml.MarshalIndent(rdata, &quot;&quot;, &quot;\t&quot;); err2 != nil {
panic(&quot;xml.MarshalIndent FAILED: &quot; + err2.Error())
} else {
xmlheader := fmt.Sprintf(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;ISO-8859-1\&quot; ?&gt;\n&quot;)
m = append([]byte(xmlheader), m...)
fmt.Printf(&quot;\n%s\n\n&quot;, m)
}
}

答案1

得分: 4

你可能需要使用",chardata"结构标签来编写字符数据。这是文档中的说明:

  • 带有",chardata"标签的字段将被写入为字符数据,而不是作为XML元素。

你可以按照以下方式定义结构体:

type attributes struct {
	XMLName   xml.Name `xml:"Attribute"`
	Name      string   `xml:"attr"`
	Attribute string   `xml:",chardata"`
}

这是play链接:go play

英文:

You may need to use ",chardata" struct tag to write character data. Here is what doc says

> - a field with tag ",chardata" is written as character data, not as an XML element.

You may define the struct as follows

type attributes struct {
XMLName   xml.Name `xml:Attribute&quot;`
Name      string   `xml:&quot;,attr&quot;`
Attribute string   `xml:&quot;,chardata&quot;`
}

Here is play link : go play

答案2

得分: 2

将你的属性字段设置为xml:"chardata"。参考这个链接:https://play.golang.org/p/NeTJW2iegw

type attributes struct {
    Name      string `xml:"attr"`
    Attribute string `xml:"chardata"`
}

希望对你有帮助...

英文:

Set your Attribute Field to xml:&quot;,chardata&quot;. See this: https://play.golang.org/p/NeTJW2iegw

type attributes struct {
Name      string `xml:&quot;,attr&quot;`
Attribute string `xml:&quot;,chardata&quot;`
}

Hope this helps...

huangapple
  • 本文由 发表于 2017年1月26日 13:40:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/41867529.html
匿名

发表评论

匿名网友

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

确定