/***********************************************************************//** * @brief Write observations into XML document * * @param[in] xml XML document. * * Write observations into the first observation library that is found in the * XML document. In case that no observation library exists, one is added to * the document. ***************************************************************************/ void GObservations::write(GXml& xml) const { // If there is no observation library then append one if (xml.elements("observation_list") == 0) { xml.append(new GXmlElement("observation_list title=\"observation list\"")); } // Get pointer on observation library GXmlElement* lib = xml.element("observation_list", 0); // Write all observations into library for (int i = 0; i < size(); ++i) { // Initialise pointer on observation GXmlElement* obs = NULL; // Search corresponding observation int n = xml.elements("observation"); for (int k = 0; k < n; ++k) { GXmlElement* element = static_cast<GXmlElement*>(xml.element("observation", k)); if (element->attribute("name") == m_obs[i]->name() && element->attribute("id") == m_obs[i]->id() && element->attribute("instrument") == m_obs[i]->instrument()) { obs = element; break; } } // If no observation with corresponding name, ID and instrument was found // then append one now if (obs == NULL) { obs = new GXmlElement("observation"); obs->attribute("name", m_obs[i]->name()); obs->attribute("id", m_obs[i]->id()); obs->attribute("instrument", m_obs[i]->instrument()); lib->append(obs); } // Write now observation m_obs[i]->write(*obs); } // endfor: looped over all observaitons // Return return; }
/***********************************************************************//** * @brief Test XML element access **************************************************************************/ void TestGXml::test_GXml_access(void) { // Test root document access GXml xml; xml.load(m_xml_file); test_value(xml.size(), 3, "Check number of child elements"); // Test node access for (int i = 0; i < xml.size(); ++i) { GXmlNode* ptr = xml[i]; test_assert(ptr != 0, "Check XML node access"); } test_value(xml.elements(), 1, "Check number of child elements"); // Test node access for (int i = 0; i < xml.elements(); ++i) { GXmlNode* ptr = xml.element(i); test_assert(ptr != 0, "Check XML element access"); } test_value(xml.elements("source_library"), 1, "Check number of child elements"); // Test element access for (int i = 0; i < xml.elements("source_library"); ++i) { GXmlElement* ptr = xml.element("source_library", i); test_value(ptr->name(), "source_library", "Check element name"); } // Test hierarchy access GXmlElement* ptr = NULL; ptr = xml.element("source_library"); test_value(ptr->name(), "source_library", "Check hierarchy level 1"); ptr = xml.element("source_library > source"); test_value(ptr->name(), "source", "Check hierarchy level 2"); ptr = xml.element("source_library > source > spectrum"); test_value(ptr->name(), "spectrum", "Check hierarchy level 3"); ptr = xml.element("source_library > source > spectrum > parameter[2]"); test_value(ptr->name(), "parameter", "Check hierarchy level 4"); test_value(ptr->attribute("name"), "PivotEnergy", "Check for attribute"); // Return return; }