1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <iostream>
#include <string>
#include <sstream>
#include "tinyxml.h"
const unsigned int NUM_INDENTS_PER_SPACE = 2;
/**
*
* @brief Display SPACES before elements.
*
* @param numIndents
*
*/
const char *getIndent(unsigned int numIndents)
{
static const char *pINDENT = " + ";
static const unsigned int LENGTH = strlen(pINDENT);
unsigned int n = numIndents * NUM_INDENTS_PER_SPACE;
if (n > LENGTH)
n = LENGTH;
return &pINDENT[LENGTH - n];
}
/**
*
* @brief Same as getIndent but no "+" at the end.
*
* @param numIndents
*
*/
const char * getIndentAlt( unsigned int numIndents )
{
static const char *pINDENT = " ";
static const unsigned int LENGTH = strlen(pINDENT);
unsigned int n = numIndents * NUM_INDENTS_PER_SPACE;
if (n > LENGTH)
n = LENGTH;
return &pINDENT[LENGTH - n];
}
/**
*
* @brief Display the attributs of an element on STDOUT.
*
* @param pElement
* @param indent
*
* @return
*
*/
int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
int cpt = 0;
int ival = 0;
double dval;
TiXmlAttribute *pAttrib;
const char *pIndent;
if (!pElement)
return 0;
pAttrib = pElement->FirstAttribute();
pIndent = getIndent(indent);
printf("\n");
while (pAttrib)
{
printf("%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival) == TIXML_SUCCESS)
printf(" int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval) == TIXML_SUCCESS)
printf(" d=%1.1f", dval);
printf("\n");
++cpt;
pAttrib = pAttrib->Next();
}
return cpt;
}
/**
* @brief Load the named file and dump its structure to STDOUT.
*
* @param pParent
* @param indent
*
*/
void dump_to_stdout(TiXmlNode* pParent, unsigned int indent = 0)
{
int num = 0;
TiXmlNode *pChild;
TiXmlText *pText;
if (!pParent)
return;
printf( "%s", getIndent(indent));
switch (pParent->Type())
{
case TiXmlNode::DOCUMENT:
printf( "Document" );
break;
case TiXmlNode::ELEMENT:
printf( "Element [%s]", pParent->Value() );
num=dump_attribs_to_stdout(pParent->ToElement(), indent+1);
switch(num)
{
case 0: printf( " (No attributes)"); break;
case 1: printf( "%s1 attribute", getIndentAlt(indent)); break;
default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
}
break;
case TiXmlNode::COMMENT:
printf( "Comment: [%s]", pParent->Value());
break;
case TiXmlNode::UNKNOWN:
printf( "Unknown" );
break;
case TiXmlNode::TEXT:
pText = pParent->ToText();
printf( "Text: [%s]", pText->Value() );
break;
case TiXmlNode::DECLARATION:
printf( "Declaration" );
break;
default:
break;
}
printf( "\n" );
for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
dump_to_stdout( pChild, indent+1 );
}
}
/**
* @brief Load the named file and dump its structure to STDOUT.
*
* @param pStr
*
*/
void dump_to_stdout(std::string pStr)
{
TiXmlDocument *doc;
std::istringstream iss(pStr);
// initialisation
doc = new TiXmlDocument();
std::cout << iss.str() << std::endl;
iss >> (*doc);
// parsing
dump_to_stdout(doc);
// liberation de la memoire
delete doc;
doc = 0;
}
/**
* @brief Main Function
*
* @param argc
* @param argv
*
* @return
*
*/
int main(int argc, char *argv[])
{
dump_to_stdout("<invoke name=\"ExternalInterfaceSample.MainForm.DisplayMessage\" returntype=\"xml\"><arguments><string>Hello world</string><number type=\"unsigned\">92</number></arguments></invoke>");
return 0;
} |
Partager