1
\$\begingroup\$

I'm parsing an XML file and searching for a child value.

As I'm familiar with this XML file structure, I know that the final value is a child of a child, and so I wrote the following code section:

/*Parse XML file and find device friendlyName*/
pxUpnp = ezxml_parse_file(XML_FILE_PATH);

pxUpnpChild = ezxml_child(pxUpnp, "device");

/*Looking for friendlyName sub-child*/
pxUpnpSubChild = ezxml_child(pxUpnpChild, "friendlyName");

This code works, but I wander if there is a better or more elegant method to parse a 'child of a child' value using ezxml library?

Thank you all in advance!

\$\endgroup\$
1
\$\begingroup\$

ezxml has a variadic function which allows you to traverse directly to a specific node, terminated by either -1 as the index, or an empty string for the node name:

pxUpnpSubChild  = ezxml_get(pxUpnp, "device", 0, "friendlyName", 0, "");

This would give you directly the first friendlyName node of the first device node. Internally, it does mostly the same as what you did manually.


The more elegant solution would to use standard XPath expressions, however that isn't supported by ezxml. Libraries for C supporting XPath would e.g. libxml2 or Expat

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.