I never thought of it ... I just code as
suggested: node->parent == node->doc or node->parent->type == XML_ELEMENT_NODE/XML_DOCUMENT_NODE and so on before you assume it really is a xmlNode In fact, I always test the type before doing anything EXCEPT that one node->parent == node->doc test which somehow I picked up someplace (old samples?) I just don't find it strange AT ALL in the C world to have pointers that point to different things and need conditional processing based on a type. Try coding with Oracle's C interface for example (not the SQL or higher language, they still have an actual C interface). It has all kinds of pointers to all kinds of things that require tests if they are non-NULL and what type they are. C is not a language that has objects and protections against everything ... it is a language great for performance when coded properly. As an example, I have a customer that hired a new IT manager who decided that using my old fashioned non-object oriented "hard to program" (if you don't know C :-) ), etc. server program was awful and needed to be replaced. It is a simple server fired up by inetd and reads in an XML document and returns one (generally after doing some database calls -- it servers Web pages and large XML file uploads for example 1,000 orders in a batch from Walmart. On a VERY modest AIX box I process in and out about 2.5 million documents in a day. So they paid a nice young man to replace my server with a JAVA server (why do people feel compelled to replace working programs I'll never know -- guess the new IT person had to feel important). JAVA has built-in XML and TCP/IP and it was so "easy" to program, yack yack ... it also fell to it's knees at volumes that would be about 10,000 documents per day if it did not crash and burn when the load got high. That is less than 1/2% of what my libxml2 server can process. This, of course, led to AIX trashing and the IT guy wanted a room full of Linux boxes with server load balancing and all kinds of things. Fortunately sanity prevailed and the new IT guys is gone. The cheap little AIX box purrs doing all that work (and running the order desk, inventory, warehouse, shipping, credit, accounting -- all in real time all on one little box) ... E On 5/3/2013 9:15 PM, Callum Gibson wrote: Thanks for that. I guess my only comment is that doc->type and node->type are there for a reason. But I agree it's a bit disingenuous to be traversing up a tree via ->parent and discover that you've hit a node which is actually a xmlDoc. In practice this means always having to have a special case test for node->parent == node->doc or node->parent->type == XML_ELEMENT_NODE/XML_DOCUMENT_NODE and so on before you assume it really is a xmlNode. I'm not even sure why the root node parent of a doc points to the xmlDoc given that there is a node->doc value to use if you want it. On 04May13 07:35, Nikita Churaev wrote: }this gives out an error: } }#include <libxml/parser.h> }#include <libxml/tree.h> }#include <stdio.h> } }int }main (int argc, char** argv) }{ } xmlDocPtr doc = xmlReadMemory("<doc/>", 6, "egg.xml", NULL, 0); } } /* XmlNode */ /* XmlDoc */ } printf("%s\n", (doc->children->parent == doc) ? "true" : "false"); } return 0; }} } }and this crashes on my machine (as xmlDoc has different fields than }xmlName): } }#include <libxml/parser.h> }#include <libxml/tree.h> }#include <stdio.h> } }int }main (int argc, char** argv) }{ } xmlDocPtr doc = xmlReadMemory("<doc/>", 6, "egg.xml", NULL, 0); } } if (doc->children->parent->ns && doc->children->parent->ns->href) } printf("%s\n", doc->children->parent->ns->href); } } return 0; }} -- Eric S. Eberhard VICS 2933 W Middle Verde Road Camp Verde, AZ 86322 928-567-3727 work 928-301-7537 cell http://www.vicsmba.com/index.html (our work) http://www.vicsmba.com/ourpics/index.html (fun pictures) |