I have an XML string
str := "<data><node><value>Foo</value></node></data>"
I need to replace value with another value (Bar
). Given a path
"data.node.value"
Is there a way in Go to find and replace that value? So calling Replace(str, "data.node.value","Bar")
<data><node><value>Bar</value></node></data>
I don't think its possible, since you just have a string. You need to
Something like this:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
Node Node `xml:"node"`
}
type Node struct {
Value string `xml:"value"`
}
func main() {
str := "<data><node><value>Foo</value></node></data>"
var d Data
xml.Unmarshal([]byte(str), &d) // unmarshal it
d.Node.Value = "Bar" // manipulate the struct
xmlout, _ := xml.Marshal(d) // re-marshal it
fmt.Println("result: ", string(xmlout))
}