Mantid
Loading...
Searching...
No Matches
XmlHandler.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <Poco/DOM/DOMParser.h>
10#include <Poco/DOM/NamedNodeMap.h>
11#include <Poco/DOM/NodeFilter.h>
12#include <Poco/DOM/NodeIterator.h>
13#include <Poco/DOM/NodeList.h>
14#include <Poco/SAX/InputSource.h>
15#include <boost/algorithm/string/trim.hpp>
16
17#include <fstream>
18
19namespace Mantid::DataHandling {
20
21XmlHandler::XmlHandler(const std::string &filename) {
22 std::ifstream in(filename);
23 Poco::XML::InputSource src(in);
24 Poco::XML::DOMParser parser;
25 pDoc = parser.parse(&src);
26}
27
33std::map<std::string, std::string> XmlHandler::get_metadata(const std::vector<std::string> &tags_to_ignore) {
34 std::map<std::string, std::string> metadata;
35
36 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
37 Poco::XML::Node *pNode = it.nextNode();
38
39 while (pNode) {
40 Poco::AutoPtr<Poco::XML::NodeList> children = pNode->childNodes();
41 if (children->length() == 1 && std::find(std::begin(tags_to_ignore), std::end(tags_to_ignore), pNode->nodeName()) ==
42 std::end(tags_to_ignore)) {
43 std::string key = pNode->parentNode()->nodeName() + "/" + pNode->nodeName();
44 std::string value = pNode->innerText();
45 boost::algorithm::trim(value);
46 metadata.emplace(key, value);
47 }
48 pNode = it.nextNode();
49 }
50 return metadata;
51}
52
53std::string XmlHandler::get_text_from_tag(const std::string &xpath) {
54 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
55 Poco::XML::Node *pNode = it.nextNode();
56 Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath);
57 std::string value;
58 if (detectorNode)
59 value = detectorNode->innerText();
60 return value;
61}
62
63std::map<std::string, std::string> XmlHandler::get_attributes_from_tag(const std::string &xpath) {
64 std::map<std::string, std::string> attributes_map;
65 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
66 Poco::XML::Node *pNode = it.nextNode();
67 Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath);
68 if (detectorNode) {
69 Poco::AutoPtr<Poco::XML::NamedNodeMap> attributes = detectorNode->attributes();
70 for (unsigned int i = 0; i < attributes->length(); i++) {
71 Poco::XML::Node *attribute = attributes->item(i);
72 attributes_map.emplace(attribute->nodeName(), attribute->nodeValue());
73 }
74 }
75 return attributes_map;
76}
77
83std::vector<std::string> XmlHandler::get_subnodes(const std::string &xpath) {
84
85 std::vector<std::string> subnodes;
86 Poco::XML::Node *xpathNode = pDoc->getNodeByPath(xpath);
87
88 Poco::XML::NodeIterator it(xpathNode, Poco::XML::NodeFilter::SHOW_ELEMENT);
89 Poco::XML::Node *pNode = it.nextNode();
90
91 while (pNode) {
92 Poco::AutoPtr<Poco::XML::NodeList> children = pNode->childNodes();
93 if (children->length() == 1) {
94 subnodes.emplace_back(pNode->nodeName());
95 }
96 pNode = it.nextNode();
97 }
98 return subnodes;
99}
100
101} // namespace Mantid::DataHandling
double value
The value of the point.
Definition FitMW.cpp:51
Poco::AutoPtr< Poco::XML::Document > pDoc
Definition XmlHandler.h:32
std::string get_text_from_tag(const std::string &)
std::map< std::string, std::string > get_metadata(const std::vector< std::string > &tags_to_ignore)
Build dictionary {string : string } of all tags in the dictionary Composed tags: / replaced by _.
std::map< std::string, std::string > get_attributes_from_tag(const std::string &)
std::vector< std::string > get_subnodes(const std::string &)
Returns list of sub-nodes for a xpath node For: xpath = //Data/ Returns: Detector ,...