19 #include <itksys/SystemTools.hxx>
27 ,
const std::string& elemName)
29 TiXmlElement * rootElem=
nullptr;
30 TiXmlElement * element=
nullptr;
33 std::string elementName = elemName;
34 if(elementName.empty())
35 elementName = this->GetNameOfClass();
37 TiXmlDocument doc( file.c_str() );
38 bool loadOkay = doc.LoadFile();
44 rootElem = doc.RootElement();
45 endoAssertMsg( rootElem,
"No root element found in " << file );
48 if( rootElem->ValueStr() == elementName )
50 doc.RemoveChild(rootElem);
56 element = rootElem->FirstChildElement(elementName);
58 rootElem->RemoveChild(element);
64 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"" );
65 doc.LinkEndChild( decl );
71 element =
new TiXmlElement( elementName );
72 this->ToXML( element );
76 rootElem =
new TiXmlElement( ROOT_NAME );
78 rootElem->LinkEndChild(element);
81 if(doc.RootElement() ==
nullptr)
82 doc.LinkEndChild( rootElem );
84 if(!doc.SaveFile( file ))
86 std::ostringstream s; s <<
"File " << file
87 <<
" could not be written. Please check permissions.";
88 throw std::logic_error(s.str());
98 ,
const std::string& elemName)
100 endodebug(
"Trying to read from " << file )
102 TiXmlDocument doc( file.c_str() );
103 bool loadOkay = doc.LoadFile();
106 std::ostringstream s; s <<
"File " << file
107 <<
" could not be loaded!";
108 throw std::logic_error(s.str().c_str());
111 m_XMLFileName = file;
113 TiXmlElement* elem = doc.FirstChildElement();
117 std::string elementName = elemName;
118 if(elementName.empty())
119 elementName = this->GetNameOfClass();
121 if(strcmp(elem->Value(), elementName.c_str()) != 0)
122 elem = elem->FirstChildElement(elementName.c_str());
125 "\" found in " << file );
130 if(elem->QueryStringAttribute(FILE_REFERENCE_ATTRIBUTE_NAME.c_str(), &
filename)
133 if( !itksys::SystemTools::FileIsFullPath(filename.c_str()) )
134 filename = itksys::SystemTools::GetFilenamePath(file) +
"/" +
filename;
135 this->FromXMLFile(filename);
139 this->FromXML( elem );
virtual void FromXMLFile(const std::string &file, const std::string &elemName="")
#define endoAssertMsg(a, msg)
DataCollection - Class to facilitate loading/accessing structured data.
static const std::string ROOT_NAME
std::string GetXMLFileName() const
static const std::string filename
static const std::string FILE_REFERENCE_ATTRIBUTE_NAME
virtual void ToXMLFile(const std::string &file, const std::string &elemName="")