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
spdomiterator.cpp@0:3fa97f2c0505, 2010-11-24 (annotated)
- Committer:
- hlipka
- Date:
- Wed Nov 24 20:52:14 2010 +0000
- Revision:
- 0:3fa97f2c0505
initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:3fa97f2c0505 | 1 | /* |
hlipka | 0:3fa97f2c0505 | 2 | * Copyright 2007 Stephen Liu |
hlipka | 0:3fa97f2c0505 | 3 | * LGPL, see http://code.google.com/p/spxml/ |
hlipka | 0:3fa97f2c0505 | 4 | * For license terms, see the file COPYING along with this library. |
hlipka | 0:3fa97f2c0505 | 5 | */ |
hlipka | 0:3fa97f2c0505 | 6 | |
hlipka | 0:3fa97f2c0505 | 7 | #include <stdio.h> |
hlipka | 0:3fa97f2c0505 | 8 | #include <stdlib.h> |
hlipka | 0:3fa97f2c0505 | 9 | #include <assert.h> |
hlipka | 0:3fa97f2c0505 | 10 | |
hlipka | 0:3fa97f2c0505 | 11 | #include "spdomiterator.hpp" |
hlipka | 0:3fa97f2c0505 | 12 | #include "spxmlnode.hpp" |
hlipka | 0:3fa97f2c0505 | 13 | |
hlipka | 0:3fa97f2c0505 | 14 | SP_DomIterator :: SP_DomIterator( const SP_XmlNode * node ) |
hlipka | 0:3fa97f2c0505 | 15 | { |
hlipka | 0:3fa97f2c0505 | 16 | mRoot = node; |
hlipka | 0:3fa97f2c0505 | 17 | mCurrent = node; |
hlipka | 0:3fa97f2c0505 | 18 | } |
hlipka | 0:3fa97f2c0505 | 19 | |
hlipka | 0:3fa97f2c0505 | 20 | SP_DomIterator :: ~SP_DomIterator() |
hlipka | 0:3fa97f2c0505 | 21 | { |
hlipka | 0:3fa97f2c0505 | 22 | } |
hlipka | 0:3fa97f2c0505 | 23 | |
hlipka | 0:3fa97f2c0505 | 24 | const SP_XmlNode * SP_DomIterator :: getNext() |
hlipka | 0:3fa97f2c0505 | 25 | { |
hlipka | 0:3fa97f2c0505 | 26 | if( NULL == mCurrent ) return NULL; |
hlipka | 0:3fa97f2c0505 | 27 | |
hlipka | 0:3fa97f2c0505 | 28 | const SP_XmlNode * retNode = mCurrent; |
hlipka | 0:3fa97f2c0505 | 29 | |
hlipka | 0:3fa97f2c0505 | 30 | // find first left child |
hlipka | 0:3fa97f2c0505 | 31 | if( SP_XmlNode::eXMLDOC == mCurrent->getType() ) { |
hlipka | 0:3fa97f2c0505 | 32 | SP_XmlDocument * document = static_cast<SP_XmlDocument*>((SP_XmlNode*)mCurrent); |
hlipka | 0:3fa97f2c0505 | 33 | |
hlipka | 0:3fa97f2c0505 | 34 | mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 35 | retNode = document->getDocDecl(); |
hlipka | 0:3fa97f2c0505 | 36 | if( NULL == retNode ) retNode = document->getDocType(); |
hlipka | 0:3fa97f2c0505 | 37 | if( NULL == retNode ) { |
hlipka | 0:3fa97f2c0505 | 38 | retNode = document->getRootElement(); |
hlipka | 0:3fa97f2c0505 | 39 | if( NULL != retNode ) mCurrent = document->getRootElement()->getChildren()->get( 0 ); |
hlipka | 0:3fa97f2c0505 | 40 | } |
hlipka | 0:3fa97f2c0505 | 41 | } else if( SP_XmlNode::eELEMENT == mCurrent->getType() ) { |
hlipka | 0:3fa97f2c0505 | 42 | SP_XmlElementNode * element = static_cast<SP_XmlElementNode*>((SP_XmlNode*)mCurrent); |
hlipka | 0:3fa97f2c0505 | 43 | const SP_XmlNodeList * children = element->getChildren(); |
hlipka | 0:3fa97f2c0505 | 44 | mCurrent = children->get( 0 ); |
hlipka | 0:3fa97f2c0505 | 45 | } else { |
hlipka | 0:3fa97f2c0505 | 46 | mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 47 | } |
hlipka | 0:3fa97f2c0505 | 48 | |
hlipka | 0:3fa97f2c0505 | 49 | // find next sibling |
hlipka | 0:3fa97f2c0505 | 50 | if( NULL == mCurrent ) { |
hlipka | 0:3fa97f2c0505 | 51 | mCurrent = retNode; |
hlipka | 0:3fa97f2c0505 | 52 | |
hlipka | 0:3fa97f2c0505 | 53 | const SP_XmlNode * parent = NULL; |
hlipka | 0:3fa97f2c0505 | 54 | if( NULL != mCurrent ) parent = mCurrent->getParent(); |
hlipka | 0:3fa97f2c0505 | 55 | |
hlipka | 0:3fa97f2c0505 | 56 | for( ; NULL != parent; ) { |
hlipka | 0:3fa97f2c0505 | 57 | if( SP_XmlNode::eXMLDOC == parent->getType() ) { |
hlipka | 0:3fa97f2c0505 | 58 | SP_XmlDocument * document = static_cast<SP_XmlDocument*>((SP_XmlNode*)parent); |
hlipka | 0:3fa97f2c0505 | 59 | if( mCurrent == document->getDocDecl() ) { |
hlipka | 0:3fa97f2c0505 | 60 | mCurrent = document->getDocType(); |
hlipka | 0:3fa97f2c0505 | 61 | if( NULL == mCurrent ) mCurrent = document->getRootElement(); |
hlipka | 0:3fa97f2c0505 | 62 | } else if( mCurrent == document->getDocType() ) { |
hlipka | 0:3fa97f2c0505 | 63 | mCurrent = document->getRootElement(); |
hlipka | 0:3fa97f2c0505 | 64 | } else { |
hlipka | 0:3fa97f2c0505 | 65 | mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 66 | } |
hlipka | 0:3fa97f2c0505 | 67 | } else if( SP_XmlNode::eELEMENT == parent->getType() ) { |
hlipka | 0:3fa97f2c0505 | 68 | SP_XmlElementNode * element = static_cast<SP_XmlElementNode*>((SP_XmlNode*)parent); |
hlipka | 0:3fa97f2c0505 | 69 | const SP_XmlNodeList * children = element->getChildren(); |
hlipka | 0:3fa97f2c0505 | 70 | |
hlipka | 0:3fa97f2c0505 | 71 | int index = -1; |
hlipka | 0:3fa97f2c0505 | 72 | for( int i = 0; i < children->getLength(); i++ ) { |
hlipka | 0:3fa97f2c0505 | 73 | if( mCurrent == children->get( i ) ) { |
hlipka | 0:3fa97f2c0505 | 74 | index = i; |
hlipka | 0:3fa97f2c0505 | 75 | break; |
hlipka | 0:3fa97f2c0505 | 76 | } |
hlipka | 0:3fa97f2c0505 | 77 | } |
hlipka | 0:3fa97f2c0505 | 78 | |
hlipka | 0:3fa97f2c0505 | 79 | if( index >= 0 && index < ( children->getLength() - 1 ) ) { |
hlipka | 0:3fa97f2c0505 | 80 | mCurrent = children->get( index + 1 ); |
hlipka | 0:3fa97f2c0505 | 81 | } else { |
hlipka | 0:3fa97f2c0505 | 82 | mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 83 | } |
hlipka | 0:3fa97f2c0505 | 84 | } else { |
hlipka | 0:3fa97f2c0505 | 85 | mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 86 | assert( 0 ); // should not occur |
hlipka | 0:3fa97f2c0505 | 87 | } |
hlipka | 0:3fa97f2c0505 | 88 | |
hlipka | 0:3fa97f2c0505 | 89 | if( NULL == mCurrent ) { |
hlipka | 0:3fa97f2c0505 | 90 | mCurrent = parent; |
hlipka | 0:3fa97f2c0505 | 91 | parent = mCurrent->getParent(); |
hlipka | 0:3fa97f2c0505 | 92 | if( NULL == parent ) mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 93 | if( mRoot == mCurrent ) mCurrent = NULL; |
hlipka | 0:3fa97f2c0505 | 94 | } else { |
hlipka | 0:3fa97f2c0505 | 95 | break; |
hlipka | 0:3fa97f2c0505 | 96 | } |
hlipka | 0:3fa97f2c0505 | 97 | } |
hlipka | 0:3fa97f2c0505 | 98 | } |
hlipka | 0:3fa97f2c0505 | 99 | |
hlipka | 0:3fa97f2c0505 | 100 | return retNode; |
hlipka | 0:3fa97f2c0505 | 101 | } |
hlipka | 0:3fa97f2c0505 | 102 |