int CXmlBase::GetAttrsOfNode(map<CString, CComBSTR>& mapAttrs, MSXML2::IXMLDOMNodePtr pNode) { MSXML2::IXMLDOMNamedNodeMapPtr pAttrs; MSXML2::IXMLDOMNodePtr pAttr; HRESULT hr; CComBSTR bstrText; CString str; long lAttrCount = 0; mapAttrs.clear(); if (NULL == pNode) { //WRITELOG("END, pNode为空"); return 0; } pAttrs = pNode->Getattributes(); if (pAttrs == NULL) { //WRITELOG("END, 取得节点属性集合失败"); return 0; } lAttrCount = pAttrs->Getlength(); for (int i=0; i<lAttrCount; i++) { pAttr = pAttrs->Getitem(i); if (pAttr == NULL) { //WRITELOG("取得第[%d]个属性为空", i); continue; } hr = pAttr->get_nodeName(&bstrText); if (FAILED(hr)) { //WRITELOG("取得第[%d]个属性名为空", i); continue; } if (!ConvertBSTR2CString(str, bstrText.m_str)) { //WRITELOG("转换第[%d]个属性名 失败", i); continue; } hr = pAttr->get_text(&bstrText); if (FAILED(hr)) { //WRITELOG("取得第[%d]个属性值 失败,属性[%s]", i, str); continue; } mapAttrs.insert(make_pair(str, bstrText)); } return (int)mapAttrs.size(); }
//////////////////////////////////////////////////////////////////////////// // Helper function: //////////////////////////////////////////////////////////////////////////// CString CXMLDocument::GetStringValue(LPCSTR szAttrib, MSXML2::IXMLDOMNode* pNode) { CComBSTR bstrValue; MSXML2::IXMLDOMNamedNodeMapPtr pAttributes; MSXML2::IXMLDOMNodePtr pItemNode; if (SUCCEEDED(pNode->get_attributes(&pAttributes)) && (pAttributes != NULL)) pAttributes->getNamedItem(CComBSTR(szAttrib), &pItemNode); if (pItemNode) pItemNode->get_text(&bstrValue); return CString(bstrValue); }
bool XMLElement::getAttributeValue(const char *attributeName, std::string& attributeValue) const { bool found = false; MSXML2::IXMLDOMNamedNodeMapPtr attrs; MSXML2::IXMLDOMNodePtr attr; BSTR bstr; if (nodePtr) { if ((nodePtr->get_attributes(&attrs)) == S_OK) { if ((attrs->raw_getNamedItem(_bstr_t(attributeName), &attr)) == S_OK) { if ((attr->get_text(&bstr)) == S_OK) { attributeValue = bstrToString(bstr).c_str(); found = true; } } } } return found; }
//////////////////////////////////////////////////////////////////////////// // 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); }
// 获取当前结点的指定序号的属性的名称 int CBpXMLNode::GetAttributeByIndex(int nIndex, CString &strAttributeValue) const { if (m_pDOMNode == NULL || nIndex <= 0) return XML_ERROR; MSXML2::IXMLDOMNamedNodeMap *pAttributeMap = NULL; m_pDOMNode->get_attributes(&pAttributeMap); if (pAttributeMap == NULL) return XML_ERROR; MSXML2::IXMLDOMNodePtr pAttNode = pAttributeMap->item[nIndex]; if (pAttNode == NULL) return XML_ERROR; BSTR bstrText = NULL; pAttNode->get_text(&bstrText); strAttributeValue = bstrText; ::SysFreeString(bstrText); return XML_OK; }
// 获取当前结点的指定属性的值 int CBpXMLNode::GetAttributeByName(const wchar_t *szAttributeName, CString &strAttributeValue) const { if (m_pDOMNode == NULL) return XML_ERROR; MSXML2::IXMLDOMNamedNodeMap *pAttributeMap = NULL; m_pDOMNode->get_attributes(&pAttributeMap); if (pAttributeMap == NULL) return XML_ERROR; MSXML2::IXMLDOMNodePtr pAttNode = pAttributeMap->getNamedItem(szAttributeName); if (pAttNode == NULL) return XML_ERROR; BSTR bstrText = NULL; pAttNode->get_text(&bstrText); strAttributeValue = bstrText; ::SysFreeString(bstrText); return XML_OK; }
int CSOAPUtil::ParseSOAPRequest(char* xml, int xmlSize, SOAP_REQUEST_INFO* info) { int res = NO_ERR; MSXML2::IXMLDOMDocumentPtr xmlDoc; HRESULT hr = xmlDoc.CreateInstance(MSXML2::CLSID_DOMDocument); if(FAILED(hr)) { return ERR_FALSE; } xmlDoc->put_async(VARIANT_FALSE); xmlDoc->put_preserveWhiteSpace(VARIANT_TRUE); if( LoadDOMDocument(xml, xmlSize, xmlDoc) != NO_ERR ) { xmlDoc.Release(); xmlDoc = NULL; return ERR_FALSE; } //��� MSXML2::IXMLDOMElementPtr pElemRoot; MSXML2::IXMLDOMNodePtr mediaNode = NULL; xmlDoc->get_documentElement(&pElemRoot); if( pElemRoot == NULL ) { res = ERR_FALSE; goto Err_End; } for( int i=0; i<pElemRoot->childNodes->length; i++ ) { MSXML2::IXMLDOMNodePtr bodyNode = pElemRoot->childNodes->Getitem(i); CComBSTR localName; bodyNode->get_baseName(&localName); if( localName == NULL ) { continue; } if(CompareNoCase(L"Body", localName.m_str) == 0 ) { for( int j=0; j<bodyNode->childNodes->length; j++ ) { MSXML2::IXMLDOMNodePtr actionNode = bodyNode->childNodes->Getitem(j); CComBSTR actionName; actionNode->get_baseName(&actionName); if( actionName == NULL ) { continue; } info->actionName = actionName; for( int k=0; k<actionNode->childNodes->length; k++) { MSXML2::IXMLDOMNodePtr argNode = actionNode->childNodes->Getitem(k); CComBSTR argName; argNode->get_baseName(&argName); if( argName == NULL ) { continue; } CComBSTR argVal; argNode->get_text(&argVal); info->argList.insert(pair<wstring,wstring>(argName.m_str, argVal.m_str)); } } break; } } Err_End: xmlDoc.Release(); xmlDoc = NULL; return res; }
////////////////////////////////////////////////////////////////////////// //简要描述 : 设置扩展属性配置文件 //输入参数 : //返 回 值 : // // //修改日志 : ////////////////////////////////////////////////////////////////////////// STDMETHODIMP CDwgWriter::put_XDataXMLConfigFile(BSTR sXMLFile) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); if (sXMLFile == NULL) return S_OK; CString sXml = sXMLFile; if (sXml.IsEmpty()) return S_OK; m_XDataCfgs.RemoveAll(); try { CXMLFile xmlfile; xmlfile.load(sXml); MSXML2::IXMLDOMNodePtr pNode; MSXML2::IXMLDOMNodePtr pExtAttrNode = NULL; xmlfile.GetNode("LAYERS", pNode); if (pNode == NULL) { //AfxMessageBox("XML配置文件不正确,请检查。"); m_dwgWriter.WriteLog("XML配置文件不正确,请检查。"); return S_FALSE; } pNode = pNode->GetfirstChild(); if (pNode == NULL) { //AfxMessageBox("XML配置文件不正确,请检查。"); m_dwgWriter.WriteLog("XML配置文件不正确,请检查。"); return S_FALSE; } CComBSTR bsNodeName; CComBSTR bsExtAttrs; CString sLayerName; CString sRegAppName; CString sExtAttrs; while (pNode != NULL) { //得到图层名 pNode->get_nodeName(&bsNodeName); sLayerName = bsNodeName; //去掉前面的_前缀,解决数字开头的节点问题 CString sSign = ""; sSign = sLayerName.Mid(0, 1); if (sSign.CompareNoCase("_") == 0) { sLayerName = sLayerName.Mid(1); } XDataAttrLists* pExtAttrs = new XDataAttrLists(); //得到图层下的注册应用名 if (pNode->hasChildNodes()) { pExtAttrNode = pNode->GetfirstChild(); while (pExtAttrNode != NULL) { pExtAttrNode->get_nodeName(&bsNodeName); sRegAppName = bsNodeName; //去掉前面的_前缀,解决数字开头的节点问题 sSign = sRegAppName.Mid(0, 1); if (sSign.CompareNoCase("_") == 0) { sRegAppName = sRegAppName.Mid(1); } pExtAttrNode->get_text(&bsExtAttrs); sExtAttrs = bsExtAttrs; CStringList* pAttrLst = new CStringList(); //解析注册应用名下的属性字段名称 CString sAttr; int iPos = sExtAttrs.Find(','); while (iPos > 0) { sAttr = sExtAttrs.Mid(0, iPos); sExtAttrs = sExtAttrs.Mid(iPos + 1); if (!sAttr.IsEmpty()) { pAttrLst->AddTail(sAttr); } iPos = sExtAttrs.Find(','); } if (iPos == -1) { if (!sExtAttrs.IsEmpty()) { pAttrLst->AddTail(sExtAttrs); } } pExtAttrs->SetAt(sRegAppName, pAttrLst); //得到下一个注册应用名的配置 pExtAttrNode = pExtAttrNode->GetnextSibling(); } } m_XDataCfgs.SetAt(sLayerName, pExtAttrs); //得到下一个图层的扩展属性的配置 pNode = pNode->GetnextSibling(); } } catch (...) { m_dwgWriter.WriteLog("解析XML文件出错,请检查。"); return S_FALSE; } return S_OK; }