unsigned int mitk::NavigationDataPlayer::GetFileVersion(std::istream* stream) { if (stream==NULL) { MITK_ERROR << "No input stream set!"; mitkThrowException(mitk::IGTException)<<"No input stream set!"; } if (!stream->good()) { MITK_ERROR << "Stream is not good!"; mitkThrowException(mitk::IGTException)<<"Stream is not good!"; } int version = 1; TiXmlDeclaration* dec = new TiXmlDeclaration(); *stream >> *dec; if(strcmp(dec->Version(),"") == 0) { MITK_ERROR << "The input stream seems to have XML incompatible format"; mitkThrowException(mitk::IGTIOException) << "The input stream seems to have XML incompatible format"; } m_parentElement = new TiXmlElement(""); *stream >> *m_parentElement; //2nd line this is the file version std::string tempValue = m_parentElement->Value(); if(tempValue != "Version") { if(tempValue == "Data"){ m_parentElement->QueryIntAttribute("version",&version); } } else { m_parentElement->QueryIntAttribute("Ver",&version); } if (version > 0) return version; else return 0; }
void readSchoolXml() { using namespace std; const char *xmlFile = "conf/school.xml"; TiXmlDocument doc; if (!doc.LoadFile(xmlFile)) { cout << "Load xml file failed.\t" << xmlFile << endl; return; } cout << "Load xml file OK." << endl; TiXmlDeclaration *decl = doc.FirstChild()->ToDeclaration(); if (!decl) { cout << "decl is null.\n" << endl; return; } cout << decl->Encoding(); cout << decl->Version(); cout << decl->Standalone() << endl; TiXmlHandle docHandle(&doc); TiXmlElement *child = docHandle.FirstChild("School").FirstChild("Class").Child("Student", 0).ToElement(); for ( ; child != NULL; child = child->NextSiblingElement()) { TiXmlAttribute *attr = child->FirstAttribute(); for (; attr != NULL; attr = attr->Next()) { cout << attr->Name() << " : " << attr->Value() << endl; } TiXmlElement *ct = child->FirstChildElement(); for (; ct != NULL; ct = ct->NextSiblingElement()) { char buf[1024] = {0}; u2g(ct->GetText(), strlen(ct->GetText()), buf, sizeof(buf)); cout << ct->Value() << " : " << buf << endl; } cout << "=====================================" << endl; } TiXmlElement *schl = doc.RootElement(); const char *value_t =schl->Attribute("name"); char buf[1024] = {0}; if ( u2g(value_t, strlen(value_t), buf, sizeof(buf)) == -1) { return; } cout << "Root Element value: " << buf << endl; schl->RemoveAttribute("name"); schl->SetValue("NewSchool"); cout << "Save file: " << (doc.SaveFile("conf/new.xml") ? "Ok" : "Failed") << endl; return ; TiXmlElement *rootElement = doc.RootElement(); TiXmlElement *classElement = rootElement->FirstChildElement(); TiXmlElement *studentElement = classElement->FirstChildElement(); // N 个 Student 节点 for ( ; studentElement!= NULL; studentElement = studentElement->NextSiblingElement()) { // 获得student TiXmlAttribute *stuAttribute = studentElement->FirstAttribute(); for (; stuAttribute != NULL; stuAttribute = stuAttribute->Next()) { cout << stuAttribute->Name() << " : " << stuAttribute->Value() << endl; } // 获得student的第一个联系方式 TiXmlElement *contact = studentElement->FirstChildElement(); for (; contact != NULL; contact = contact->NextSiblingElement()) { const char *text = contact->GetText(); char buf[1024] = {0}; if ( u2g(text, strlen(text), buf, sizeof(buf)) == -1) { continue; } cout << contact->Value() << " : " << buf << endl; } } }