From: <sv...@ww...> - 2006-12-09 11:46:06
|
Author: nsmoooose Date: 2006-12-09 03:45:59 -0800 (Sat, 09 Dec 2006) New Revision: 2026 Modified: trunk/csp/csplib/xml/XmlParser.cpp trunk/csp/csplib/xml/XmlParser.h Log: Added support for very basic xpath queries like A/B/C. Made the xml parser available to cspsim library. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=2026 Modified: trunk/csp/csplib/xml/XmlParser.cpp =================================================================== --- trunk/csp/csplib/xml/XmlParser.cpp 2006-12-09 07:26:24 UTC (rev 2025) +++ trunk/csp/csplib/xml/XmlParser.cpp 2006-12-09 11:45:59 UTC (rev 2026) @@ -102,6 +102,7 @@ #include <cstdio> #include <cstring> #include <memory.h> +#include <string> CSP_NAMESPACE @@ -2236,6 +2237,32 @@ return getChildNode(name,&i); } +XMLNode XMLNode::selectSingleNode(CSP_XMLCSTR xpath) { + std::string path = xpath; + + XMLNode currentNode = *this; + std::string::size_type pos = 0; + while(true) + { + std::string::size_type nextpos = path.find('/', pos); + std::string nodeName = path.substr(pos, nextpos == std::string::npos ? std::string::npos : nextpos - pos); + XMLNode childNode = currentNode.getChildNode(nodeName.c_str()); + + // Test to see if this was the last one. + if(nextpos == std::string::npos) { + return childNode; + } + // If we didn't find a child node then we return an empty one. + if(childNode.isEmpty()) { + return childNode; + } + + // Iterate into the next node... + pos = nextpos+1; + currentNode = childNode; + } +} + XMLNode XMLNode::getChildNodeWithAttribute(CSP_XMLCSTR name,CSP_XMLCSTR attributeName,CSP_XMLCSTR attributeValue, int *k) { int i=0,j; Modified: trunk/csp/csplib/xml/XmlParser.h =================================================================== --- trunk/csp/csplib/xml/XmlParser.h 2006-12-09 07:26:24 UTC (rev 2025) +++ trunk/csp/csplib/xml/XmlParser.h 2006-12-09 11:45:59 UTC (rev 2026) @@ -49,6 +49,7 @@ #ifndef __CSPLIB_XML_XMLPARSER_H__ #define __CSPLIB_XML_XMLPARSER_H__ +#include <csp/csplib/util/Export.h> #include <csp/csplib/util/Namespace.h> #include <cstdlib> @@ -156,7 +157,7 @@ struct XMLNodeContents; -typedef struct XMLNode +typedef struct CSPLIB_EXPORT XMLNode { private: @@ -208,6 +209,9 @@ // (return an empty node if failing) XMLNode getChildNode(CSP_XMLCSTR name, int *i=NULL); + // A method for very basic xpath queries. + XMLNode XMLNode::selectSingleNode(CSP_XMLCSTR xpath); + XMLNode getChildNodeWithAttribute(CSP_XMLCSTR tagName, // return child node with specific name/attribute CSP_XMLCSTR attributeName, // (return an empty node if failing) CSP_XMLCSTR attributeValue=NULL, // |