猿问

如何省略外部 XML 元素的名称

我需要生成这样的 XML 文件:


  <package>

    <dc:contributor id="creator1">John Doe</dc:contributor>

    <meta refines="creator1" property="role" scheme="marc:relators">aut</meta>

    <meta refines="creator1" property="role" scheme="onix:codelist17">A01</meta>

    <meta refines="creator1" property="file-as">Doe, John</meta>

    <meta refines="creator1" property="diplay-seq">1</meta>

    <dc:contributor id="reviewer1">Mary Poppins</dc:contributor>

    <meta refines="reviewer1" property="role" scheme="marc:relators">rev</meta>

    <meta refines="reviewer1" property="file-as">Poppins, Mary</meta>

    <meta refines="reviewer1" property="diplay-seq">1</meta>

    <dc:contributor id="translator1">Alfred Weasley</dc:contributor>

    <meta refines="translator1" property="role" scheme="marc:relators">trl</meta>

    <meta refines="translator1" property="role" scheme="onix:codelist17">B06</meta>

    <meta refines="translator1" property="file-as">Weasley, Alfred</meta>

    <meta refines="translator1" property="diplay-seq">1</meta>

  </package>

排序在这里很重要。


我使用这种结构:


type Pack struct {

    XMLName struct{}        `xml:"package"`

    People  []PersonWrapper `xml:""` // <-- is this the problem?

}


type Person struct {

    Id       string `xml:"id,attr"`

    Name     string `xml:"-"`

    Surname  string `xml:"-"`

    FullName string `xml:",innerxml"`

}


type Meta struct {

    Refines  string `xml:"refines,attr"`

    Property string `xml:"property,attr"`

    Scheme   string `xml:"scheme,attr,omitempty"`

    Value    string `xml:",innerxml"`

}


type PersonWrapper struct {

    Person Person `xml:"dc:contributor"`

    Metas  []Meta `xml:"meta"`

}


如何摆脱<People/>外部标签?我试图将内部结构包装到更高级别的其他结构中,但无法达到所需的结构。


沧海一幻觉
浏览 149回答 1
1回答

回首忆惘然

您可以实现xml.Marshaler接口PersonWrapper(并将 xmlnames 添加到嵌套结构中)。type Pack struct {&nbsp; &nbsp; XMLName struct{} `xml:"package"`&nbsp; &nbsp; People&nbsp; []PersonWrapper}type Person struct {&nbsp; &nbsp; XMLName&nbsp; xml.Name `xml:"dc:contributor"`&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"id,attr"`&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"-"`&nbsp; &nbsp; Surname&nbsp; string&nbsp; &nbsp;`xml:"-"`&nbsp; &nbsp; FullName string&nbsp; &nbsp;`xml:",innerxml"`}type Meta struct {&nbsp; &nbsp; XMLName&nbsp; xml.Name `xml:"meta"`&nbsp; &nbsp; Refines&nbsp; string&nbsp; &nbsp;`xml:"refines,attr"`&nbsp; &nbsp; Property string&nbsp; &nbsp;`xml:"property,attr"`&nbsp; &nbsp; Scheme&nbsp; &nbsp;string&nbsp; &nbsp;`xml:"scheme,attr,omitempty"`&nbsp; &nbsp; Value&nbsp; &nbsp; string&nbsp; &nbsp;`xml:",innerxml"`}type PersonWrapper struct {&nbsp; &nbsp; Person Person `xml:"dc:contributor"`&nbsp; &nbsp; Metas&nbsp; []Meta `xml:"meta"`}func (pw PersonWrapper) MarshalXML(e *xml.Encoder, start xml.StartElement) error {&nbsp; &nbsp; if err := e.Encode(pw.Person); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; if err := e.Encode(pw.Metas); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; return nil}https://play.golang.org/p/hzrBdHXHwOB
随时随地看视频慕课网APP

相关分类

Go
我要回答