I
I
ivos2016-07-15 19:02:21
go
ivos, 2016-07-15 19:02:21

How to create xml with repeated tag?

Hello. Please help me to create xml. My code is here: https://play.golang.org/p/NkEPfpSIS9
Unable to create duplicate Value fields:

<Field Id="2">
    <Value>Two</Value>
    <Value>Three</Value>
    <Value>four</Value>
</Field>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-07-15
@ivos

https://play.golang.org/p/O8eNLkLA6s

package main

import (
  "encoding/xml"
  "fmt"
)

type Plans struct {
  XMLName xml.Name `xml:"Plans"`
  Xsd     string   `xml:"xmlns:xsd,attr"`
  Xsi     string   `xml:"xmlns:xsi,attr"`
  Fields  Fields
}

type Fields struct {
  Field []Field
}

type Field struct {
  Id int `xml:"Id,attr"`
  Values []string `xml:"Value"`
}


func main() {
  v := &Plans{Xsd: "http://www.w3.org/2001/XMLSchema", Xsi: "http://www.w3.org/2001/XMLSchema-instance"}

  v.Fields = Fields{
    []Field{
      Field{Id: 1, Values: []string{ "One","two"}},
      Field{Id: 2, Values: []string{ "Two"}},
    },
  }

  out, err := xml.MarshalIndent(v, "", "   ")
  if err != nil {
    fmt.Println(err)
  }

  fmt.Println(xml.Header + string(out))
}

<?xml version="1.0" encoding="UTF-8"?>
<Plans xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Fields>
      <Field Id="1">
         <Value>One</Value>
         <Value>two</Value>
      </Field>
      <Field Id="2">
         <Value>Two</Value>
      </Field>
   </Fields>
</Plans>

I
ivos, 2016-07-15
@ivos

thanks Sharp !

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question