meta.cpp 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "ismrmrd/meta.h"
#include "pugixml.hpp"


namespace ISMRMRD
{
  void deserialize(const char* xml, MetaContainer& h)
  {
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load(xml);
    
    if (!result) {
      throw std::runtime_error("Unable to load ISMRMRD Meta XML document");
    }

    pugi::xml_node root = doc.child("ismrmrdMeta");

    if (!root) {
      throw std::runtime_error("ismrmrdMeta tag not found in meta data header");
    }
    
    pugi::xml_node meta = root.child("meta");
    while (meta) {
      pugi::xml_node name = meta.child("name");
      pugi::xml_node value = meta.child("value");

      if (!name || !value) {
	std::runtime_error("Malformed metadata value");
      }

      while (value) {
	h.append(name.child_value(),value.child_value());
	value = value.next_sibling("value");
      }

      meta = meta.next_sibling("meta");
    }
  }

  void serialize(MetaContainer& h, std::ostream& o)
  {
    pugi::xml_document doc;
    pugi::xml_node root = doc.append_child("ismrmrdMeta");
    
    MetaContainer::map_t::iterator it = h.map_.begin();
    while (it != h.map_.end()) {
      pugi::xml_node meta = root.append_child("meta");
      pugi::xml_node name = meta.append_child("name");
      name.append_child(pugi::node_pcdata).set_value(it->first.c_str());
      for (unsigned int i = 0; i < it->second.size(); i++) {
	pugi::xml_node name = meta.append_child("value");
	name.append_child(pugi::node_pcdata).set_value(it->second[i].as_str());
      }
      it++;
    }
    doc.save(o);
  }

}