24 ,
const std::string& elemName)
26 tinyxml2::XMLElement * rootElem=
nullptr;
27 tinyxml2::XMLElement* element=
nullptr;
30 std::string elementName = elemName;
31 if(elementName.empty())
32 elementName = this->GetNameOfClass();
34 tinyxml2::XMLDocument doc;
36 if(tinyxml2::XML_SUCCESS == doc.LoadFile(file.c_str()))
40 rootElem = doc.RootElement();
41 endoAssertMsg( rootElem,
"No root element found in " << file );
44 if( std::string(rootElem->Value() !=
nullptr ? rootElem->Value() :
"") == elementName )
46 doc.DeleteChild(rootElem);
52 element = rootElem->FirstChildElement(elementName.c_str());
54 rootElem->DeleteChild(element);
60 doc.InsertEndChild( doc.NewDeclaration() );
66 element = doc.NewElement( elementName.c_str() );
67 this->ToXML( element );
71 rootElem = doc.NewElement( ROOT_NAME.c_str() );
73 rootElem->InsertEndChild(element);
76 if(doc.RootElement() ==
nullptr)
77 doc.InsertEndChild( rootElem );
79 if(tinyxml2::XML_SUCCESS != doc.SaveFile( file.c_str() ))
81 std::ostringstream s; s <<
"File " << file
82 <<
" could not be written. Please check permissions.";
83 throw std::logic_error(s.str());
93 ,
const std::string& elemName)
95 endodebug(
"Trying to read from " << file )
97 tinyxml2::XMLDocument doc;
98 if(tinyxml2::XML_SUCCESS != doc.LoadFile(file.c_str()))
100 std::ostringstream s; s <<
"File " << file
101 <<
" could not be loaded!";
102 throw std::logic_error(s.str().c_str());
105 m_XMLFileName = file;
107 auto* elem = doc.FirstChildElement();
111 std::string elementName = elemName;
112 if(elementName.empty())
113 elementName = this->GetNameOfClass();
115 if(
strcmp(elem->Value(), elementName.c_str()) != 0)
116 elem = elem->FirstChildElement(elementName.c_str());
119 "\" found in " << file );
123 const char* filenameC = elem->Attribute(FILE_REFERENCE_ATTRIBUTE_NAME.c_str());
124 std::string filename =
nullptr != filenameC
127 if(!filename.empty())
129 if( !itksys::SystemTools::FileIsFullPath(filename.c_str()) )
130 filename = itksys::SystemTools::GetFilenamePath(file) +
"/" + filename;
131 this->FromXMLFile(filename);
135 this->FromXML( elem );