From aa3e81f4fc1208edcd23403ee8923b5dc2c5095d Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Wed, 10 Jul 2019 09:21:16 +0800 Subject: [PATCH] Base::Reader: support reading child element with the same name as parent Future GuiDocument.xml will save recursive tree item expansion status using child element with the same name as the parent. --- src/Base/Reader.cpp | 17 ++++++++++++++--- src/Base/Reader.h | 20 ++++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Base/Reader.cpp b/src/Base/Reader.cpp index f6b4872f79..8346c225a3 100644 --- a/src/Base/Reader.cpp +++ b/src/Base/Reader.cpp @@ -252,10 +252,18 @@ void Base::XMLReader::readElement(const char* ElementName) (ElementName && LocalName != ElementName)); } -void Base::XMLReader::readEndElement(const char* ElementName) +int Base::XMLReader::level() const { + return Level; +} + +void Base::XMLReader::readEndElement(const char* ElementName, int level) { // if we are already at the end of the current element - if (ReadType == EndElement && LocalName == ElementName) { + if (ReadType == EndElement + && ElementName + && LocalName == ElementName + && (level<0 || level==Level)) + { return; } else if (ReadType == EndDocument) { @@ -268,7 +276,10 @@ void Base::XMLReader::readEndElement(const char* ElementName) ok = read(); if (!ok) break; if (ReadType == EndDocument) break; - } while (ReadType != EndElement || (ElementName && LocalName != ElementName)); + } while (ReadType != EndElement + || (ElementName + && (LocalName != ElementName + || (level>=0 && level!=Level)))); } void Base::XMLReader::readCharacters(void) diff --git a/src/Base/Reader.h b/src/Base/Reader.h index d25304b0f8..08c96018f8 100644 --- a/src/Base/Reader.h +++ b/src/Base/Reader.h @@ -134,10 +134,26 @@ public: //@{ /// get the local name of the current Element const char* localName(void) const; + /// get the current element level + int level() const; /// read until a start element is found (\) or start-end element (\) (with special name if given) void readElement (const char* ElementName=0); - /// read until an end element is found (with special name if given) - void readEndElement(const char* ElementName=0); + + /** read until an end element is found + * + * @param ElementName: optional end element name to look for. If given, then + * the parser will read until this name is found. + * + * @param level: optional level to look for. If given, then the parser will + * read until this level. Note that the parse only increase the level when + * finding a start element, not start-end element, and decrease the level + * after finding an end element. So, if you obtain the parser level after + * calling readElement(), you should specify a level minus one when calling + * this function. This \c level parameter is only useful if you know the + * child element may have the same name as its parent, otherwise, using \c + * ElementName is enough. + */ + void readEndElement(const char* ElementName=0, int level=-1); /// read until characters are found void readCharacters(void); /// read binary file