Mbed port of the Simple Plain Xml parser. See http://code.google.com/p/spxml/ for more details. This library uses less memory and is much better suited to streaming data than TinyXML (doesn\'t use as much C++ features, and especially works without streams). See http://mbed.org/users/hlipka/notebook/xml-parsing/ for usage examples.
Dependents: spxmltest_weather VFD_fontx2_weather weather_LCD_display News_LCD_display ... more
Diff: spxmlhandle.cpp
- Revision:
- 0:3fa97f2c0505
diff -r 000000000000 -r 3fa97f2c0505 spxmlhandle.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spxmlhandle.cpp Wed Nov 24 20:52:14 2010 +0000 @@ -0,0 +1,125 @@ +/* + * Copyright 2008 Stephen Liu + * LGPL, see http://code.google.com/p/spxml/ + * For license terms, see the file COPYING along with this library. + */ + +#include <stdio.h> +#include <string.h> + +#include "spxmlhandle.hpp" +#include "spxmlnode.hpp" + + +SP_XmlHandle :: SP_XmlHandle( SP_XmlNode * node ) +{ + mNode = node; +} + +SP_XmlHandle :: SP_XmlHandle( const SP_XmlHandle & ref ) +{ + mNode = ref.mNode; +} + +SP_XmlHandle & SP_XmlHandle :: operator=( const SP_XmlHandle & ref ) +{ + mNode = ref.mNode; + return *this; +} + +SP_XmlHandle :: ~SP_XmlHandle() +{ +} + +SP_XmlHandle SP_XmlHandle :: getChild( const char * name, int index ) const +{ + SP_XmlNode * ret = NULL; + + if( NULL != mNode ) { + if( SP_XmlNode::eELEMENT == mNode->getType() ) { + SP_XmlElementNode * element = (SP_XmlElementNode*)mNode; + const SP_XmlNodeList * children = element->getChildren(); + + int tmpIndex = index; + for( int i = 0; i < children->getLength(); i++ ) { + if( SP_XmlNode::eELEMENT == children->get(i)->getType() ) { + SP_XmlElementNode * iter = (SP_XmlElementNode*)children->get(i); + if( 0 == strcmp( name, iter->getName() ) ) { + if( 0 == tmpIndex ) { + ret = iter; + break; + } + tmpIndex--; + } + } + } + } + } + + return SP_XmlHandle( ret ); +} + +SP_XmlHandle SP_XmlHandle :: getChild( int index ) const +{ + SP_XmlNode * ret = NULL; + + if( NULL != mNode ) { + if( SP_XmlNode::eELEMENT == mNode->getType() ) { + SP_XmlElementNode * element = (SP_XmlElementNode*)mNode; + ret = (SP_XmlNode*)element->getChildren()->get( index ); + } + } + + return SP_XmlHandle( ret ); +} + +SP_XmlHandle SP_XmlHandle :: getElement( int index ) const +{ + SP_XmlNode * ret = NULL; + + if( NULL != mNode ) { + if( SP_XmlNode::eELEMENT == mNode->getType() ) { + SP_XmlElementNode * element = (SP_XmlElementNode*)mNode; + const SP_XmlNodeList * children = element->getChildren(); + + int tmpIndex = index; + for( int i = 0; i < children->getLength(); i++ ) { + if( SP_XmlNode::eELEMENT == children->get(i)->getType() ) { + SP_XmlElementNode * iter = (SP_XmlElementNode*)children->get(i); + + if( 0 == tmpIndex ) { + ret = iter; + break; + } + tmpIndex--; + } + } + } + } + + return SP_XmlHandle( ret ); +} + +SP_XmlNode * SP_XmlHandle :: toNode() +{ + return mNode; +} + +SP_XmlElementNode * SP_XmlHandle :: toElement() +{ + if( NULL != mNode && SP_XmlNode::eELEMENT == mNode->getType() ) { + return (SP_XmlElementNode*)mNode; + } + + return NULL; +} + +SP_XmlCDataNode * SP_XmlHandle :: toCData() +{ + if( NULL != mNode && SP_XmlNode::eCDATA == mNode->getType() ) { + return (SP_XmlCDataNode*)mNode; + } + + return NULL; +} +