diff --git a/node.go b/node.go
index 5972d05..3add181 100644
--- a/node.go
+++ b/node.go
@@ -66,6 +66,13 @@ func (this *Node) GetValue() string {
return res
}
+func (this *Node) SetValue(val string) {
+ t := NewNode(NT_TEXT)
+ t.Value = string([]byte(val))
+ t.Parent = this
+ this.Children = []*Node{t} // brutally replace all other children
+}
+
// Get node value as string
func (this *Node) S(namespace, name string) string {
foundNode := rec_SelectNode(this, namespace, name)
diff --git a/xmlx_test.go b/xmlx_test.go
index 1da420b..2f50fbd 100644
--- a/xmlx_test.go
+++ b/xmlx_test.go
@@ -251,3 +251,78 @@ func TestElementNodeValueFetch(t *testing.T) {
t.Errorf("Failed to get availability using B, got: %v, wanted: true", v)
}
}
+
+func TestElementNodeValueFetchAndSet(t *testing.T) {
+ IndentPrefix = ""
+
+ data := `
+ r
+ edBMW50
+ .256
+ 2
+
+ Tr
+
+ ue`
+ doc := New()
+
+ if err := doc.LoadString(data, nil); nil != err {
+ t.Fatalf("LoadString(): %s", err)
+ }
+
+ carN := doc.SelectNode("", "car")
+ if carN == nil {
+ t.Fatalf("Failed to get the car, got nil, wanted Node{car}")
+ }
+
+ colorNode := carN.SelectNode("", "color")
+ if colorNode == nil {
+ t.Fatalf("Failed to get the color, got nil, wanted Node{color}")
+ }
+
+ colorVal := colorNode.GetValue()
+ if colorVal != "red" {
+ t.Fatalf("Failed to get the color, got %v, wanted 'red'", colorVal)
+ }
+
+ colorNode.SetValue("blue")
+
+ expected := `blueBMW50
+ .256
+ 2
+
+ Tr
+
+ ue`
+
+
+ if got := doc.Root.String(); got != expected {
+ t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
+ }
+
+ // now set the brand
+ brandNode := carN.SelectNode("", "brand")
+ if brandNode == nil {
+ t.Fatalf("Failed to get the color, got nil, wanted Node{brand}")
+ }
+
+ brandVal := brandNode.GetValue()
+ if brandVal != "BMW" {
+ t.Fatalf("Failed to get the brand, got %v, wanted 'BMW'", brandVal)
+ }
+
+ brandNode.SetValue("Trabant")
+
+ // Notice, we lose the tag in BMW, that's intentional
+ expected = `blueTrabant50
+ .256
+ 2
+
+ Tr
+
+ ue`
+
+ if got := doc.Root.String(); got != expected {
+ t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
+ }
+}