Home > Mobile >  Passing dynamic values in xml in golang
Passing dynamic values in xml in golang

Time:12-17

I have following xml file:

payload := []byte(strings.TrimSpace(`
<Login>
                <OverideLogging xmlns="HitchHiker.FlightAPI.SharedStructs">0</OverideLogging>
                <Password xmlns="HitchHiker.FlightAPI.SharedStructs">password</Password>
                <TransactionID xmlns="HitchHiker.FlightAPI.SharedStructs">Id</TransactionID>
                <UserName xmlns="HitchHiker.FlightAPI.SharedStructs">username</UserName>
            </Login>
`))

Now I have following data:

password:= "abc"
id:= 12
username:= "abc"

When I try to use this dynamic value, It say doesn't understand password field which should be taken from above constant.

if I use

payload := []byte(strings.TrimSpace(`
<Login>
                <OverideLogging xmlns="HitchHiker.FlightAPI.SharedStructs">0</OverideLogging>
                <Password xmlns="HitchHiker.FlightAPI.SharedStructs">abc</Password>
                <TransactionID xmlns="HitchHiker.FlightAPI.SharedStructs">12</TransactionID>
                <UserName xmlns="HitchHiker.FlightAPI.SharedStructs">abc</UserName>
            </Login>
`))
fmt.Println(payload)

It works perfectly.

Now If I want to pass dynamic value like below:

payload := []byte(strings.TrimSpace(`
<Login>
                <OverideLogging xmlns="HitchHiker.FlightAPI.SharedStructs">0</OverideLogging>
                <Password xmlns="HitchHiker.FlightAPI.SharedStructs">password</Password>
                <TransactionID xmlns="HitchHiker.FlightAPI.SharedStructs">Id</TransactionID>
                <UserName xmlns="HitchHiker.FlightAPI.SharedStructs">username</UserName>
            </Login>
`))

fmt.Println(payload)

Then It gives error as following:

password declared but not used
Id declared but not used
username declared but not used

I want to pass these values in xml body dynamically. How do I do it?

CodePudding user response:

Go does not have that kind of string interpolation. Variables will NOT be automatically injected into a string. You need to do it explicitly with plain concatenation or using the fmt package.

password := "abc"
id := 12
username := "abc"

payload := fmt.Sprintf(`<Login>
<OverideLogging xmlns="HitchHiker.FlightAPI.SharedStructs">0</OverideLogging>
<Password xmlns="HitchHiker.FlightAPI.SharedStructs">%s</Password>
<TransactionID xmlns="HitchHiker.FlightAPI.SharedStructs">%d</TransactionID>
<UserName xmlns="HitchHiker.FlightAPI.SharedStructs">%s</UserName>
</Login>`,
    password, // will replace the first verb (%s)
    id,       // will replace the second verb (%d)
    username, // will replace the third verb (%s)
)

https://go.dev/play/p/mdO8UvmZyD2

See the fmt documentation for more info on how the verbs work.

CodePudding user response:

type Login struct {
    Password      string `xml:"Password"`
    UserName      string `xml:"UserName"`
    TransactionID string `xml:"TransactionID"`
}

func populateLogin() *Login {
    req := Login{}
    req.UserName = "abc"
    req.Password = "123"
    req.TransactionID = 23323
    return &req
}

func main(){

payload := []byte(`
                <Password xmlns="HitchHiker.FlightAPI.SharedStructs">{{.Password}}</Password>
                <TransactionID xmlns="HitchHiker.FlightAPI.SharedStructs">{{.TransactionID}}</TransactionID>
                <UserName xmlns="HitchHiker.FlightAPI.SharedStructs">{{.UserName}}</UserName>
            </Login>`)

login := populateLogin()

    template, _ := template.New("login").Parse(string(payload))

    if err != nil {
        panic(err)
    }
    err = template.Execute(os.Stdout, login)
    if err != nil {
        panic(err)
    }
}

This code works exactly. This can help if anyone is looking for answers.

  •  Tags:  
  • go
  • Related