GXml: Usage of DomNode's text_content

What am I doing wrong here when using GXml.DomNode.text_content? I would’n have expected to concatenate the strings:

Period:

<?xml version="1.0"?>
<Period unit="ms">25</Period>

Period:

<?xml version="1.0"?>
<Period unit="ns">25625</Period>

results from:

public class Period : GXml.Element {
    private const string NODE_NAME = "Period";

    public int period { 
        get { return (int.parse(text_content)); }
        set { text_content = value.to_string(); }
    }

    public string unit { 
        owned get { return get_attribute("unit"); }
        set { set_attribute("unit", value); }
    }

    public Period(int p, string u) {
        unit   = u;
        period = p;
    }

    construct {
      // This is the Element's node's name to be used
      try { initialize (NODE_NAME); }
      catch (GLib.Error e ) {
        warning ("Error: "+e.message);
      }
    }
}

void main () {
    try {
        var p = new Period(25, "ms");

        stdout.printf ("Period:\n\n%s\n", p.write_string());

        p.period = 625;
        p.unit = "ns";

        stdout.printf ("Period:\n\n%s\n", p.write_string());

    } catch (GLib.Error e) {
        warning ("Error: "+e.message);
    }

    stdout.printf("\n");
}

GXml seems to implement DOM

‘DomNode’ maps directly to Node and text_content to textContent:

The textContent getter steps are to return the following, switching on this:

Since your Period is an Element we follow the link to

The descendant text content of a node node is the concatenation of the data of all the Text node descendants of node, in tree order.

1 Like

Great, now I’ve understood what’s going on, thank you!

Not having any deeper clue of DOM I was just peeking at the methods properties and pitched on text_content. Obviously wrong for my intention, since I wanted to replace the node’s data with the new string.

So I’m still curious what would be the right way to replace the node’s data so that issuing p.period = 625; will have the desired effect? Since using node_value instead of text_content in

   public int period { 
        get { return (int.parse(node_value)); }
        set { node_value = value.to_string(); }
    }

results in an empty element:

Period:

<?xml version="1.0"?>
<Period unit="ms"/>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.