I need an xmlNode that points to the "tiles" element of this file.
<map>
<export from="v4.3.9" build="4390" date="2023-07-17" time="14:55:14" />
<region floors="1" lowest_floor="1" grid_shape="square">
<name>
<![CDATA[Region 1]]>
</name>
<setup origin="bl" />
<floor index="1">
<tiles>
...
I thought I could simply navigate down the tree by setting the xmlNode to its children.
xmlDoc *doc;
xmlNode *cur;
doc = xmlParseFile("test.xml");
cur = xmlDocGetRootElement(doc);
cur = cur->children; // cur->name at this point returns 'text' which is weird but besides the point
cur = cur->next;
...
At this point, cur is "export", which is what I'd expect. But if I do it again, cur becomes NULL and I get a segfault. I can do cur = cur->next again to get to "region", but I can't get anywhere from there either. I thought that every node would connect to the next but that doesn't seem to be the case and it stops after the first set of children.
// cur->name at this point returns 'text' which is weird but besides the point
This is because the whitespace between nodes is significant, even if we generally don't think so.
That text
node is the newline and two spaces between <map>
and <export>
.
I thought that every node would connect to the next but that doesn't seem to be the case and it stops after the first set of children.
This is because the DOM is hierarchical, not linear. When you're on <region>
, you could go into its node->children
, and keep checking node->next
until you get to <floor>
, etc...
OR, you could use an XPath and just execute an XPath query for map/region/floor/tiles
, and it'll give you the tiles node.
There's an example of using XPath in the list of libxml2 examples