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)
}
}
xml

2个回答



您可能需要使用“,chardata”结构标记来写入字符数据。 这是 doc 所说的</ p>

\ n

  • 带有标签“,chardata”的字段被写为字符数据,而不是XML元素。</ li>
    </ ul>
    </ blockquote>

    < p>您可以按以下方式定义结构</ p>

     类型属性struct {
    XMLName xml.Namexml:Attribute“
    名称字符串xml:”,attr“
    属性字符串xml:“,chardata”
    }
    </ code> </ pre>

    此处是播放链接: </ p>
    </ div>

展开原文

原文

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

douxuan1284
douxuan1284 我很欣赏这两个答案,实际上尝试检查这两个答案,但显然只能检查一个答案。
3 年多之前 回复
dongshanxun6479
dongshanxun6479 我先回答了...好吧,..不用担心。
3 年多之前 回复
duanmo7075
duanmo7075 感谢您的快速响应,chardata在Sarathsp和Melvin两种情况下都成功了。 对此快速反应感到震惊,再次感谢!
3 年多之前 回复



将属性字段设置为 xml:“,chardata” </ code>。 看到此内容: https://play.golang.org/p/NeTJW2iegw </ p>

 类型属性struct {
名称字符串xml:“,attr”
属性字符串xml:“,chardata”
}
</ code> </ pre >

希望这对您有帮助... </ p>
</ div>

展开原文

原文

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

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

Hope this helps...

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问