📄 xmlparser.h
字号:
/** **************************************************************************** * <P> XML.c - implementation file for basic XML parser written in ANSI C++ * for portability. It works by using recursion and a node tree for breaking * down the elements of an XML document. </P> * * @version V2.31 * @author Frank Vanden Berghen * * BSD license: * Copyright (c) 2002, Frank Vanden Berghen * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the Frank Vanden Berghen nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * **************************************************************************** */#ifndef __INCLUDE_XML_NODE__#define __INCLUDE_XML_NODE__#include <stdlib.h>#ifdef _UNICODE// If you comment the next "define" line then the library will never "switch to" _UNICODE (wchar_t*) mode (16/32 bits per characters).// This is useful when you get error messages like:// 'XMLNode::openFileHelper' : cannot convert parameter 2 from 'const char [5]' to 'const wchar_t *'// The _XMLWIDECHAR preprocessor variable force the XMLParser library into either utf16/32-mode (the proprocessor variable// must be defined) or utf8-mode(the pre-processor variable must be undefined).#define _XMLWIDECHAR#endif#if defined(WIN32) || defined(UNDER_CE)// comment the next line if you are under windows and the compiler is not Microsoft Visual Studio (6.0 or .NET)#define _XMLWINDOWS#endif#ifdef XMLDLLENTRY#undef XMLDLLENTRY#endif#ifdef _USE_XMLPARSER_DLL#ifdef _DLL_EXPORTS_#define XMLDLLENTRY __declspec(dllexport)#else#define XMLDLLENTRY __declspec(dllimport)#endif#else#define XMLDLLENTRY#endif// uncomment the next line if you want no support for wchar_t* (no need for the <wchar.h> or <tchar.h> libraries anymore to compile)//#define XML_NO_WIDE_CHAR#ifdef XML_NO_WIDE_CHAR#undef _XMLWINDOWS#undef _XMLWIDECHAR#endif#ifdef _XMLWINDOWS#include <tchar.h>#else#define XMLDLLENTRY#ifndef XML_NO_WIDE_CHAR#include <wchar.h> // to have 'wcsrtombs' for ANSI version // to have 'mbsrtowcs' for WIDECHAR version#endif#endif// Some common types for char set portable code#ifdef _XMLWIDECHAR #ifndef _X #define _X(c) L ## c #endif #define XMLCSTR const wchar_t * #define XMLSTR wchar_t * #define XMLCHAR wchar_t#else #ifndef _X #define _X(c) c #endif #define XMLCSTR const char * #define XMLSTR char * #define XMLCHAR char#endif#ifndef FALSE #define FALSE 0#endif /* FALSE */#ifndef TRUE #define TRUE 1#endif /* TRUE */// Enumeration for XML parse errors.typedef enum XMLError{ eXMLErrorNone = 0, eXMLErrorMissingEndTag, eXMLErrorNoXMLTagFound, eXMLErrorEmpty, eXMLErrorMissingTagName, eXMLErrorMissingEndTagName, eXMLErrorUnmatchedEndTag, eXMLErrorUnmatchedEndClearTag, eXMLErrorUnexpectedToken, eXMLErrorNoElements, eXMLErrorFileNotFound, eXMLErrorFirstTagNotFound, eXMLErrorUnknownCharacterEntity, eXMLErrorCharConversionError, eXMLErrorCannotOpenWriteFile, eXMLErrorCannotWriteFile, eXMLErrorBase64DataSizeIsNotMultipleOf4, eXMLErrorBase64DecodeIllegalCharacter, eXMLErrorBase64DecodeTruncatedData, eXMLErrorBase64DecodeBufferTooSmall} XMLError;// Enumeration used to manage type of data. Use in conjunction with structure XMLNodeContentstypedef enum XMLElementType{ eNodeChild=0, eNodeAttribute=1, eNodeText=2, eNodeClear=3, eNodeNULL=4} XMLElementType;// Structure used to obtain error details if the parse fails.typedef struct XMLResults{ enum XMLError error; int nLine,nColumn;} XMLResults;// Structure for XML clear (unformatted) node (usually comments)typedef struct XMLClear { XMLCSTR lpszValue; XMLCSTR lpszOpenTag; XMLCSTR lpszCloseTag;} XMLClear;// Structure for XML attribute.typedef struct XMLAttribute { XMLCSTR lpszName; XMLCSTR lpszValue;} XMLAttribute;struct XMLNodeContents;typedef struct XMLDLLENTRY XMLNode{ private: struct XMLNodeDataTag; // protected constructors: use one of these four methods to get your first instance of XMLNode: // - parseString // - parseFile // - openFileHelper // - createXMLTopNode XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDeclaration); XMLNode(struct XMLNodeDataTag *p); public: // You can create your first instance of XMLNode with these 4 functions: // (see complete explanation of parameters below) static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE); static XMLNode parseString (XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL); static XMLNode parseFile (XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL); static XMLNode openFileHelper(XMLCSTR filename, XMLCSTR tag=NULL ); // The tag parameter should be the name of the first tag inside the XML file. // If the tag parameter is omitted, the 3 functions return a node that represents // the head of the xml document including the declaration term (<? ... ?>). // The "openFileHelper" reports to the screen all the warnings & errors that occurred during // parsing of the XML file. Since each application has its own way to report and deal with errors, // you should rather use the "parseFile" function to parse XML files and program yourself thereafter // an "error reporting" tailored for your needs (instead of using the very crude "error reporting" // mechanism included inside the "openFileHelper" function). // If the XML document is corrupted: // * The "openFileHelper" method will: // - display an error message on the console (or inside a messageBox for windows). // - stop execution (exit). // I suggest that you write your own "openFileHelper" method tailored to your needs. // * The 2 other methods will initialize the "pResults" variable with some information that // can be used to trace the error. // * If you still want to parse the file, you can use the APPROXIMATE_PARSING option as // explained inside the note at the beginning of the "xmlParser.cpp" file. // You can have a user-friendly explanation of the parsing error with this function: static XMLCSTR getError(XMLError error); static XMLCSTR getVersion(); XMLCSTR getName() const; // name of the node XMLCSTR getText(int i=0) const; // return ith text field int nText() const; // nbr of text field XMLNode getParentNode() const; // return the parent node XMLNode getChildNode(int i=0) const; // return ith child node XMLNode getChildNode(XMLCSTR name, int i) const; // return ith child node with specific name // (return an empty node if failing) XMLNode getChildNode(XMLCSTR name, int *i=NULL) const; // return next child node with specific name // (return an empty node if failing) XMLNode getChildNodeWithAttribute(XMLCSTR tagName, // return child node with specific name/attribute XMLCSTR attributeName, // (return an empty node if failing) XMLCSTR attributeValue=NULL, // int *i=NULL) const; // int nChildNode(XMLCSTR name) const; // return the number of child node with specific name int nChildNode() const; // nbr of child node XMLAttribute getAttribute(int i=0) const; // return ith attribute XMLCSTR getAttributeName(int i=0) const; // return ith attribute name XMLCSTR getAttributeValue(int i=0) const; // return ith attribute value char isAttributeSet(XMLCSTR name) const; // test if an attribute with a specific name is given XMLCSTR getAttribute(XMLCSTR name, int i) const; // return ith attribute content with specific name // (return a NULL if failing) XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const; // return next attribute content with specific name // (return a NULL if failing) int nAttribute() const; // nbr of attribute XMLClear getClear(int i=0) const; // return ith clear field (comments) int nClear() const; // nbr of clear field XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const; // create XML string starting from current XMLNode // if nFormat==0, no formatting is required // otherwise this returns an user friendly XML string from a // given element with appropriate white spaces and carriage returns. // if pnSize is given it returns the size in character of the string. XMLError writeToFile(XMLCSTR filename, const char *encoding=NULL, char nFormat=1) const; // Save the content of an xmlNode inside a file. // The nFormat parameter has the same meaning as in the // createXMLString function. If the global parameter // "characterEncoding==encoding_UTF8", then the "encoding" parameter is // ignored and always set to "utf-8". If the global parameter // "characterEncoding==encoding_ShiftJIS", then the "encoding" parameter // is ignored and always set to "SHIFT-JIS". If "_XMLWIDECHAR=1", then // the "encoding" parameter is ignored and always set to "utf-16". // If no "encoding" parameter is given the "ISO-8859-1" encoding is used. XMLNodeContents enumContents(int i) const; // enumerate all the different contents (attribute,child,text, // clear) of the current XMLNode. The order is reflecting // the order of the original file/string. // NOTE: 0 <= i < nElement(); int nElement() const; // nbr of different contents for current node char isEmpty() const; // is this node Empty? char isDeclaration() const; // is this node a declaration <? .... ?> static XMLNode emptyNode(); // return XMLNode::emptyXMLNode;// to allow shallow/fast copy: ~XMLNode(); XMLNode(const XMLNode &A); XMLNode& operator=( const XMLNode& A ); XMLNode(): d(NULL){}; static XMLNode emptyXMLNode; static XMLClear emptyXMLClear; static XMLAttribute emptyXMLAttribute; // The following functions allows you to create from scratch (or update) a XMLNode structure // Start by creating your top node with the "createXMLTopNode" function and then add new nodes with the "addChild" function.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -