c++读xml文件 MSXML

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);
            // TODO: if (FAILED(hr))...

            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") {
                            //cout << "okk" << endl;
                            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));
                                //cout << "node name: " << (char*)pNode1->nodeName << ", "
                                //    << "node value: " << (char*)(_bstr_t)varVal << endl;
                            }
                            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;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值