doubaben7394 2017-01-26 05:40
浏览 504
已采纳

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

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:

`

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

`

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: `

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

`

However I need it to look like this: `

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

` 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 (
    "encoding/xml"
    "fmt"
    "time"
)

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

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\" ?>
")
    m = append([]byte(xmlheader), m...)
    fmt.Printf("
%s

", m)
}
}
  • 写回答

2条回答 默认 最新

  • douyinyi7766 2017-01-26 05:58
    关注

    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"`
        Name      string   `xml:",attr"`
        Attribute string   `xml:",chardata"`
    }
    

    Here is play link : go play

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿