15 #include <itksys/SystemTools.hxx> 23 ,
const std::string& elemName)
25 TiXmlElement * rootElem=
nullptr;
26 TiXmlElement * element=
nullptr;
29 std::string elementName = elemName;
30 if(elementName.empty())
31 elementName = this->GetNameOfClass();
33 TiXmlDocument doc( file.c_str() );
34 bool loadOkay = doc.LoadFile();
40 rootElem = doc.RootElement();
41 endoAssertMsg( rootElem,
"No root element found in " << file );
44 if( rootElem->ValueStr() == elementName )
46 doc.RemoveChild(rootElem);
52 element = rootElem->FirstChildElement(elementName);
54 rootElem->RemoveChild(element);
60 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"" );
61 doc.LinkEndChild( decl );
67 element =
new TiXmlElement( elementName );
68 this->ToXML( element );
72 rootElem =
new TiXmlElement( ROOT_NAME );
74 rootElem->LinkEndChild(element);
77 if(doc.RootElement() ==
nullptr)
78 doc.LinkEndChild( rootElem );
80 if(!doc.SaveFile( file ))
82 std::ostringstream s; s <<
"File " << file
83 <<
" could not be written. Please check permissions.";
84 throw std::logic_error(s.str());
94 ,
const std::string& elemName)
96 endodebug(
"Trying to read from " << file )
98 TiXmlDocument doc( file.c_str() );
99 bool loadOkay = doc.LoadFile();
102 std::ostringstream s; s <<
"File " << file
103 <<
" could not be loaded!";
104 throw std::logic_error(s.str().c_str());
107 m_XMLFileName = file;
109 TiXmlElement* elem = doc.FirstChildElement();
113 std::string elementName = elemName;
114 if(elementName.empty())
115 elementName = this->GetNameOfClass();
117 if(strcmp(elem->Value(), elementName.c_str()) != 0)
118 elem = elem->FirstChildElement(elementName.c_str());
121 "\" found in " << file );
125 std::string filename;
126 if(elem->QueryStringAttribute(FILE_REFERENCE_ATTRIBUTE_NAME.c_str(), &filename)
129 if( !itksys::SystemTools::FileIsFullPath(filename.c_str()) )
130 filename = itksys::SystemTools::GetFilenamePath(file) +
"/" + filename;
131 this->FromXMLFile(filename);
135 this->FromXML( elem );
std::string GetXMLFileName() const
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
static const std::string FILE_REFERENCE_ATTRIBUTE_NAME
virtual void ToXMLFile(const std::string &file, const std::string &elemName="")