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 <boost/algorithm/string/trim.hpp>
11//#include <Poco/DOM/Element.h>
12#include <Poco/DOM/NamedNodeMap.h>
13//#include <Poco/DOM/Node.h>
14#include <Poco/DOM/NodeFilter.h>
15#include <Poco/DOM/NodeIterator.h>
16#include <Poco/DOM/NodeList.h>
17//#include <Poco/DOM/Text.h>
18#include <Poco/SAX/InputSource.h>
19
20#include <fstream>
21//#include <iterator>
22
23namespace Mantid::DataHandling {
24
25XmlHandler::XmlHandler(const std::string &filename) {
26 std::ifstream in(filename);
27 Poco::XML::InputSource src(in);
28 Poco::XML::DOMParser parser;
29 pDoc = parser.parse(&src);
30}
31
37std::map<std::string, std::string> XmlHandler::get_metadata(const std::vector<std::string> &tags_to_ignore) {
38 std::map<std::string, std::string> metadata;
39
40 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
41 Poco::XML::Node *pNode = it.nextNode();
42
43 while (pNode) {
44 Poco::AutoPtr<Poco::XML::NodeList> children = pNode->childNodes();
45 if (children->length() == 1 && std::find(std::begin(tags_to_ignore), std::end(tags_to_ignore), pNode->nodeName()) ==
46 std::end(tags_to_ignore)) {
47 std::string key = pNode->parentNode()->nodeName() + "/" + pNode->nodeName();
48 std::string value = pNode->innerText();
49 boost::algorithm::trim(value);
50 metadata.emplace(key, value);
51 }
52 pNode = it.nextNode();
53 }
54 return metadata;
55}
56
57std::string XmlHandler::get_text_from_tag(const std::string &xpath) {
58 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
59 Poco::XML::Node *pNode = it.nextNode();
60 Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath);
61 std::string value;
62 if (detectorNode)
63 value = detectorNode->innerText();
64 return value;
65}
66
67std::map<std::string, std::string> XmlHandler::get_attributes_from_tag(const std::string &xpath) {
68 std::map<std::string, std::string> attributes_map;
69 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
70 Poco::XML::Node *pNode = it.nextNode();
71 Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath);
72 if (detectorNode) {
73 Poco::AutoPtr<Poco::XML::NamedNodeMap> attributes = detectorNode->attributes();
74 for (unsigned int i = 0; i < attributes->length(); i++) {
75 Poco::XML::Node *attribute = attributes->item(i);
76 attributes_map.emplace(attribute->nodeName(), attribute->nodeValue());
77 }
78 }
79 return attributes_map;
80}
81
87std::vector<std::string> XmlHandler::get_subnodes(const std::string &xpath) {
88
89 std::vector<std::string> subnodes;
90 Poco::XML::Node *xpathNode = pDoc->getNodeByPath(xpath);
91
92 Poco::XML::NodeIterator it(xpathNode, Poco::XML::NodeFilter::SHOW_ELEMENT);
93 Poco::XML::Node *pNode = it.nextNode();
94
95 while (pNode) {
96 Poco::AutoPtr<Poco::XML::NodeList> children = pNode->childNodes();
97 if (children->length() == 1) {
98 subnodes.emplace_back(pNode->nodeName());
99 }
100 pNode = it.nextNode();
101 }
102 return subnodes;
103}
104
105} // 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 &)
Definition: XmlHandler.cpp:57
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 _.
Definition: XmlHandler.cpp:37
std::map< std::string, std::string > get_attributes_from_tag(const std::string &)
Definition: XmlHandler.cpp:67
std::vector< std::string > get_subnodes(const std::string &)
Returns list of sub-nodes for a xpath node For: xpath = //Data/ Returns: Detector ,...
Definition: XmlHandler.cpp:87