/** * 从xml文件中读取“键-值”对 load "key-value" from xml file * @param filename 读取的xml文件,必须是按照规定的格式 * @return 布尔值 读取成功与否 */ bool Properties::loadFromXML(const string filename) { TiXmlDocument doc(filename.c_str()); bool loadOkay = doc.LoadFile();//以utf-8格式读取xml文件 if (!loadOkay) { return false; } TiXmlNode *node = doc.FirstChild("properties"); #ifdef DEBUG_PROPERTIES_H node->Print(stdout, 1); cout << endl; #endif TiXmlElement* propertiesElement = node->ToElement(); for(TiXmlElement *it = propertiesElement->FirstChildElement("entry") ; it!=NULL ; it = it->NextSiblingElement("entry") ) { TiXmlAttribute *att = it->FirstAttribute(); this->setProperty(att->Value(), it->GetText()); #ifdef DEBUG_PROPERTIES_H cout << "[" << att->Name() << ":" << att->Value() << "->" << it->GetText() << "]" << endl; #endif } return true; }
void TiXmlElement::Print(FILE* cfile, int depth) const { int i; assert(cfile); for (i = 0; i < depth; i++) { fprintf(cfile, " "); } fprintf(cfile, "<%s", value.c_str()); const TiXmlAttribute* attrib; for (attrib = attributeSet.First(); attrib; attrib = attrib->Next()) { fprintf(cfile, " "); attrib->Print(cfile, depth); } // There are 3 different formatting approaches: // 1) An element without children is printed as a <foo /> node // 2) An element with only a text child is printed as <foo> text </foo> // 3) An element with children is printed on multiple lines. TiXmlNode* node; if (!firstChild) { fprintf(cfile, " />"); } else if (firstChild == lastChild && firstChild->ToText()) { fprintf(cfile, ">"); firstChild->Print(cfile, depth + 1); fprintf(cfile, "</%s>", value.c_str()); } else { fprintf(cfile, ">"); for (node = firstChild; node; node = node->NextSibling()) { if (!node->ToText()) { fprintf(cfile, "\n"); } node->Print(cfile, depth + 1); } fprintf(cfile, "\n"); for (i = 0; i < depth; ++i) { fprintf(cfile, " "); } fprintf(cfile, "</%s>", value.c_str()); } }
void TiXmlDocument::Print( FILE* cfile, int depth ) const { TiXmlNode* node; for ( node=FirstChild(); node; node=node->NextSibling() ) { node->Print( cfile, depth ); generic_fprintf( cfile, TEXT("\n") ); } }
void TiXmlDocument::Print( FILE* fp, int ) { TiXmlNode* node; for ( node=FirstChild(); node; node=node->NextSibling() ) { node->Print( fp, 0 ); fprintf( fp, "\n" ); } }
void TiXmlElement::Print( FILE* cfile, int depth ) const { int i; assert( cfile ); for ( i=0; i<depth; i++ ) { fprintf( cfile, " " ); } fprintf( cfile, "<%s", value.c_str() ); const TiXmlAttribute* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { fprintf( cfile, " " ); attrib->Print( cfile, depth ); } TiXmlNode* node; if ( !firstChild ) { fprintf( cfile, " />" ); } else if ( firstChild == lastChild && firstChild->ToText() ) { fprintf( cfile, ">" ); firstChild->Print( cfile, depth + 1 ); fprintf( cfile, "</%s>", value.c_str() ); } else { fprintf( cfile, ">" ); for ( node = firstChild; node; node=node->NextSibling() ) { if ( !node->ToText() ) { fprintf( cfile, "\n" ); } node->Print( cfile, depth+1 ); } fprintf( cfile, "\n" ); for( i=0; i<depth; ++i ) { fprintf( cfile, " " ); } fprintf( cfile, "</%s>", value.c_str() ); } }
void TiXmlElement::Print( FILE* fp, int depth ) { int i; for ( i=0; i<depth; i++ ) fprintf( fp, " " ); fprintf( fp, "<%s", value.c_str() ); TiXmlAttribute* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { fprintf( fp, " " ); attrib->Print( fp, 0 ); } // If this node has children, give it a closing tag. Else // make it an empty tag. TiXmlNode* node; if ( firstChild ) { fprintf( fp, ">" ); for ( node = firstChild; node; node=node->NextSibling() ) { if ( !node->ToText() ) fprintf( fp, "\n" ); node->Print( fp, depth+1 ); } fprintf( fp, "\n" ); for ( i=0; i<depth; i++ ) fprintf( fp, " " ); fprintf( fp, "</%s>", value.c_str() ); } else { fprintf( fp, " />" ); } }
void TiXmlElement::Print( FileOps *f, int depth ) const { const char *newLine = "\n"; const char *tabSpacer = " "; const char *startTag = "<"; const char *startEndTag = "</"; const char *closeEmptyElement = " />"; const char *closeTag = ">"; const char *singleSpace = " "; int i; assert( f ); for ( i=0; i<depth; i++ ) { f->write(tabSpacer, 3, 1); } f->write(startTag, 1, 1); f->write(value.c_str(), value.size(), 1); const TiXmlAttribute* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { f->write(singleSpace, 1, 1); attrib->Print( f, depth ); } // There are 3 different formatting approaches: // 1) An element without children is printed as a <foo /> node // 2) An element with only a text child is printed as <foo> text </foo> // 3) An element with children is printed on multiple lines. TiXmlNode* node; if ( !firstChild ) { f->write(closeEmptyElement, 3, 1); } else if ( firstChild == lastChild && firstChild->ToText() ) { f->write(closeTag, 1, 1); firstChild->Print( f, depth + 1 ); f->write(startEndTag, 2, 1); f->write(value.c_str(), value.size(), 1); f->write(closeTag, 1, 1); } else { f->write(closeTag, 1, 1); for ( node = firstChild; node; node=node->NextSibling() ) { if ( !node->ToText() ) { f->write(newLine, strlen(newLine), 1); } node->Print( f, depth+1 ); } f->write(newLine, strlen(newLine), 1); for( i=0; i<depth; ++i ) { f->write(tabSpacer, 3, 1); } f->write(startEndTag, 2, 1); f->write(value.c_str(), value.size(), 1); f->write(closeTag, 1, 1); } }