BOOL WXmlParse::readExtendModName(std::vector<std::pair<string, string> > &modNames) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 3 : 扩展模块列表 elementNodes->get_item(1, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr nameNode = elementAttributes->getNamedItem("name"); MSXML2::IXMLDOMNodePtr descNode = elementAttributes->getNamedItem("description"); modNames.push_back(std::make_pair(nameNode->text, descNode->text)); } return TRUE; }
BOOL WXmlParse::readObject(std::vector<ExternalObject *> &objTree) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 3 : 递归获取对象树 elementNodes->get_item(3, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { ExternalObject *objTmp = new ExternalObject(); subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); readObject(objTmp, xmlSubNode); objTree.push_back(objTmp); } return TRUE; }
BOOL WXmlParse::readObject(ExternalObject *objRet, MSXML2::IXMLDOMNodePtr &xmlSubNode) { MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodeListPtr subElementNodes; MSXML2::IXMLDOMNodeListPtr subSubElementNodes; MSXML2::IXMLDOMNodePtr xmlNodeTmp; xmlSubNode->get_attributes(&elementAttributes); LONG attrLength; attrLength = elementAttributes->Getlength(); MSXML2::IXMLDOMNodePtr nameNode = elementAttributes->getNamedItem("name"); MSXML2::IXMLDOMNodePtr clsNode = elementAttributes->getNamedItem("type"); objRet->objName = nameNode->text; objRet->clsName = clsNode->text; LONG length, subLength; xmlSubNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&length); objRet->attri.clear(); for (int i = 0;i < length;i++) { subElementNodes->get_item(i, &xmlNodeTmp); string childName = xmlNodeTmp->nodeName; if (strcmp(xmlNodeTmp->nodeName,"child") != 0) { xmlNodeTmp->get_attributes(&elementAttributes); if (!elementAttributes) return FALSE; MSXML2::IXMLDOMNodePtr typeNode = elementAttributes->getNamedItem("type"); objRet->attri.push_back(std::make_pair(xmlNodeTmp->nodeName, std::make_pair(typeNode->text, xmlNodeTmp->text))); } else { xmlNodeTmp->get_childNodes(&subSubElementNodes); subSubElementNodes->get_length(&subLength); for (int j = 0;j < subLength;j++) { ExternalObject *objSub = new ExternalObject(); subSubElementNodes->get_item(j, &xmlNodeTmp); readObject(objSub, xmlNodeTmp); objRet->children.push_back(objSub); } } } return TRUE; }
BOOL WXmlParse::readCalculateParams(WCalcaulateParam &calParam) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); if (m_strFileName.IsEmpty()) { return FALSE; } doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 0 : elementNodes->get_item(2, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr proNode = elementAttributes->getNamedItem("name"); string proName = proNode->text; MSXML2::IXMLDOMNodeListPtr subAttrNodes; xmlSubNode->get_childNodes(&subAttrNodes); subAttrNodes->get_length(&attrLength); // ASSERT(attrLength == 2); MSXML2::IXMLDOMNodePtr descNode; MSXML2::IXMLDOMNodePtr stateNode; subAttrNodes->get_item(0, &descNode); subAttrNodes->get_item(1, &stateNode); std::vector<string> vecParam; string descText = descNode->text; string stateText = stateNode->text; vecParam.push_back(proName); vecParam.push_back(descText); vecParam.push_back(stateText); calParam.addParam(vecParam); } return TRUE; }
BOOL WXmlParse::readParam(std::vector<WParamDefine *> &outParams) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); if (m_strFileName.IsEmpty()) { return FALSE; } doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 0 : elementNodes->get_item(0, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr proNode = elementAttributes->getNamedItem("name"); string proName = proNode->text; WParamDefine *paramTest = new WParamDefine(proName); MSXML2::IXMLDOMNodeListPtr subAttrNodes; xmlSubNode->get_childNodes(&subAttrNodes); subAttrNodes->get_length(&attrLength); for (int k = 0;k < attrLength;k++) { subAttrNodes->get_item(k, &xmlAttrNode); string attrName = xmlAttrNode->nodeName; string attrText = xmlAttrNode->text; paramTest->addValue(attrName, attrText); } outParams.push_back(paramTest); } return TRUE; }
//static MSXML2::IXMLDOMNodePtr CreateNode(MSXML2::IXMLDOMNodePtr pNode, std::string elementname, std::string elementtext, std::string xmlns="") //{ // MSXML2::IXMLDOMNodePtr pElement = pNode->createNode(NODE_ELEMENT, elementname.c_str(), xmlns.c_str()); // pElement->text = bstr_t(elementtext.c_str()); //} _bstr_t GetAttribute(MSXML2::IXMLDOMNodePtr node, _bstr_t attribute) { _bstr_t text= L"";; CComPtr<MSXML2::IXMLDOMNamedNodeMap> attributes; node->get_attributes( &attributes ); if( attributes ) { MSXML2::IXMLDOMNodePtr attr = attributes->getNamedItem(attribute); if(attr==NULL) return text; if(attr->nodeValue.vt == VT_BSTR) return attr->nodeValue.bstrVal; } return text; }
//////////////////////////////////////////////////////////////////////////// // Helper function: //////////////////////////////////////////////////////////////////////////// CString CXMLDocument::GetStringValue(LPCSTR szNodePattern, LPCSTR szAttrib, MSXML2::IXMLDOMNode* pNode) { CComBSTR strNodePattern(szNodePattern); MSXML2::IXMLDOMNodePtr pPatternNode; CComBSTR strValue; pNode->selectSingleNode(strNodePattern, &pPatternNode); if (pPatternNode) { MSXML2::IXMLDOMNamedNodeMapPtr pAttributes; MSXML2::IXMLDOMNodePtr pItemNode; if (SUCCEEDED(pPatternNode->get_attributes(&pAttributes)) && (pAttributes != NULL)) pAttributes->getNamedItem(CComBSTR(szAttrib), &pItemNode); if (pItemNode) pItemNode->get_text(&strValue); } return CString(strValue); }