c++读xml文件 MSXML
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include <opencv2/opencv.hpp>
#include "math.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))
using namespace std;
struct Node
{
double x1;
double y1;
double x2;
double y2;
};
void getXYbyXML(string fileName, vector<Node>& nodeList) {
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr)) {
try {
MSXML::IXMLDOMDocument2Ptr xmlDoc;
hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60),
NULL, CLSCTX_INPROC_SERVER);
if (xmlDoc->load(fileName.data()) != VARIANT_TRUE) {
printf("Unable to load input.xml\n");
}
else {
printf("XML was successfully loaded\n");
xmlDoc->setProperty("SelectionLanguage", "XPath");
MSXML::IXMLDOMElementPtr node = xmlDoc->GetdocumentElement();
MSXML::IXMLDOMNodePtr pNode;
MSXML::IXMLDOMNodePtr pNode1;
MSXML::IXMLDOMNodePtr pNode3;
MSXML::IXMLDOMNodeListPtr pNodeList = node->GetchildNodes();
VARIANT varVal;
assert(pNodeList != NULL);
int nCount = (int)pNodeList->length;
for (int i = 0; i < nCount; i++)
{
pNode = pNodeList->item[i];
pNode->get_nodeTypedValue(&varVal);
MSXML::IXMLDOMNodeListPtr pNodeList1 = pNode->GetchildNodes();
for (int j = 0; j < (int)pNodeList1->length; j++) {
pNode1 = pNodeList1->item[j];
pNode1->get_nodeTypedValue(&varVal);
if ((string)pNode1->nodeName == "bndbox") {
vector<int> xy;
Node nodeXY;
MSXML::IXMLDOMNodeListPtr pNodeList2 = pNode1->GetchildNodes();
for (int k = 0; k < pNodeList2->length; k++) {
pNode3 = pNodeList2->item[k];
pNode3->get_nodeTypedValue(&varVal);
xy.push_back(atoi((char*)(_bstr_t)varVal));
}
nodeXY.x1 = xy[0];
nodeXY.y1 = xy[1];
nodeXY.x2 = xy[2];
nodeXY.y2 = xy[3];
nodeList.push_back(nodeXY);
}
}
}
}
}
catch (_com_error& e) {
printf("ERROR: %ws\n", e.ErrorMessage());
}
CoUninitialize();
}
}
int main(int argc, char* argv[]) {
vector<Node> nodeList;
getXYbyXML("input2.xml", nodeList);
for (int kk = 0; kk < nodeList.size(); kk++) {
cout << nodeList[kk].x1 << " ," << nodeList[kk].y1 << " ," << nodeList[kk].x2 << " ," << nodeList[kk].y2 << endl;
}
return 0;
}