Questions about using GXml

@esodan
You asked me to create a new discussion.
I would like to clarify that the first goal for me is to create a visualization of a universal XML document, not specifically .UI GTK. And only then adding such features as packages, with which you can use DnD to create new XML documents(From a pre-prepared set(gtk widgets)) from scratch(I also have some ideas about the possibility of adding custom scripts, for example, to visualize the result like glade)

The collections you provided will make it very easy to create packages that I described on Twitter. But now I still have an unresolved question, I’ll just move it here.

Hi, have a small question. I’m just playing with GXML for now. Just made bfs:

            var g = new GXml.Document.from_file (f);
            var q = new Gee.LinkedList<DomNode>();
            var temp = new Gee.ArrayList<DomNode>();
            q.add(g);
            while (!q.is_empty) {
                while (!q.is_empty) {
                    prin("current level length: ", q.size);
                    prin("deleted node: ", q.peek().node_name, " : ",q.peek().text_content!=null?q.peek().text_content:"no content");
                    temp.add_all(q.poll().child_nodes);
                }
                q.add_all(temp);
                temp.clear();// можно наверно оптимизировать сделав temp стаком
            }

Testing on this example

<A>
    <B>
        <D>d_content</D>
        <E>e_content</E>
    </B>
    <C>
        c_content
    </C>
</A>

I get the following output:

current level length: 1
deleted node: #document : no content
current level length: 1
deleted node: A : 
    
    

current level length: 5
deleted node: #text : no content
current level length: 4
deleted node: B : 
        
        
        
    
current level length: 3
deleted node: #text : no content
current level length: 2
deleted node: C : 
        c_content
    
current level length: 1
deleted node: #text : no content
current level length: 6
deleted node: #text : no content
current level length: 5
deleted node: D : d_content
current level length: 4
deleted node: #text : no content
current level length: 3
deleted node: E : e_content
current level length: 2
deleted node: #text : no content
current level length: 1
deleted node: #text : no content
current level length: 2
deleted node: #text : no content
current level length: 1
deleted node: #text : no content

I didn’t expect to get these starting with # nodes. I roughly understand what it is(or not xd), but I did not think that they will come back from the child_nodes() method. Is there a way to avoid them, or might they be needed(I’m not an expert in XML). Now I assume that I will just do filtering on the first character when I redo everything on gpseq.

You should check the GXml, because you child_nodes is a property of type DomNodeList which in turn is a Gee.BidirList, so you can iterate over it’s child node. Use node is GXml.DomElement if you want to skip text nodes, with the node_name as #text.

You can access the root element using DomDocument.document_element

1 Like

Okay, sorry, I’m just more used to asking something on twitter. To save the history for the rest, I discount the content of tweets.


Can you please look at this snippet. https://gitlab.com/snippets/1946233… I get large empty spaces after A and B. They are the text_content property. I can’t figure out where they come from.


Изображение

Wait, I’m beginning to understand. In A, B and C are placed, this is 2(2+1=3). B contains D, E, and an extra empty string, which is 3+1=4. This is a very strange pattern, but it is confirmed by other examples, such as the CD example.


[

esodan
Remember: a New line is a DomText node and the content of a DomElement. DOM4 implementation in #GXml is more close to the specification and Object Oriented, than libxml2


Well, then I ask you to show the correct way to iterate over XML using GXml, so that you get only content, without extra characters(I provided a way that I’m trying to do this, maybe it’s enough to fix something there). Since it seems to me that to do something like filtering on str.strip.length == 0 will be too expensive to check for content exists. In the examples section, only examples of creating documents in different ways, and on the project Wiki, only examples of serialization and deserialization in GObject (which looks amazing, I know that GObject also supports deserialization in Json (So now Vala can do this with the two most popular formats!). But I need to parse a previously unknown structure for visualization).

var doc = new GXml.Document.from_string ("<PUT HEARE YOUR XML DEFINITION");
int level = 0;
foreach (DomNode n in doc.document_element.child_nodes) {
    stodout.printf ("Current Level: %d", level);
    if (n is DomElement) {
        // You can choose to iterage on n.child_nodes
    }
}

You can also define a method and call it recursively, but there should be a limit, because that may cause issues.

void Iterate (DomElement element, out int level) {
    foreach (DomNode n in doc.document_element) {
        if (n is DomElement) { // this will skip not element nodes, like text ones
            level++;
            iterate (n as DomElement, out level);
        }
    }
}

In your UI, may you want to iterate on children, just if the user request it.

1 Like

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