Go, encoding/xml: How can I marshal self-closing elements?

我只是一个虾纸丫 提交于 2021-02-08 12:57:12

问题


I'm writing XML from the following struct:

type OrderLine struct {
    LineNumber     string `xml:"LineNumber"`
    Product        string `xml:"Product"`
    Ref            string `xml:"Ref"`
    Quantity       string `xml:"Quantity"`
    Price          string `xml:"Price"`
    LineTotalGross string `xml:"LineTotalGross"`
}

If the Ref field is empty, I'd like the element to display, but be self-closing, i.e.

<Ref />

and not:

<Ref></Ref>

AFAIK, these two are semantically equivalent, but I would prefer a self-closing tag, as it matches the output from other systems. Is this possible?


回答1:


I found a way to do it "hacking" marshal package, but I didn't test it. If you want me to show you the link, let me now, then I post it in comments of this reply.

I did some manually code:

package main

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

type ParseXML struct {
    Person struct {
        Name     string `xml:"Name"`
        LastName string `xml:"LastName"`
        Test     string `xml:"Abc"`
    } `xml:"Person"`
}

func main() {

    var err error
    var newPerson ParseXML

    newPerson.Person.Name = "Boot"
    newPerson.Person.LastName = "Testing"

    var bXml []byte
    var sXml string
    bXml, err = xml.Marshal(newPerson)
    checkErr(err)

    sXml = string(bXml)

    r, err := regexp.Compile(`<([a-zA-Z0-9]*)><(\\|\/)([a-zA-Z0-9]*)>`)
    checkErr(err)
    matches := r.FindAllString(sXml, -1)

    fmt.Println(sXml)

    if len(matches) > 0 {
        r, err = regexp.Compile("<([a-zA-Z0-9]*)>")
        for i := 0; i < len(matches); i++ {

            xmlTag := r.FindString(matches[i])
            xmlTag = strings.Replace(xmlTag, "<", "", -1)
            xmlTag = strings.Replace(xmlTag, ">", "", -1)
            sXml = strings.Replace(sXml, matches[i], "<"+xmlTag+" />", -1)

        }
    }

    fmt.Println("")
    fmt.Println(sXml)

}

func checkErr(chk error) {
    if chk != nil {
        panic(chk)
    }
}


来源:https://stackoverflow.com/questions/38118100/go-encoding-xml-how-can-i-marshal-self-closing-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!