Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.9 KiB

  1. /*****************************************************************************\
  2. FILE: xml.cpp
  3. DESCRIPTION:
  4. These are XML DOM wrappers that make it easy to pull information out
  5. of an XML file or object.
  6. BryanSt 10/12/2000
  7. Copyright (C) Microsoft Corp 2000-2000. All rights reserved.
  8. \*****************************************************************************/
  9. #include "stock.h"
  10. #pragma hdrstop
  11. /////////////////////////////////////////////////////////////////////
  12. // XML Related Helpers
  13. /////////////////////////////////////////////////////////////////////
  14. #define SZ_VALID_XML L"<?xml"
  15. STDAPI XMLDOMFromBStr(BSTR bstrXML, IXMLDOMDocument ** ppXMLDoc)
  16. {
  17. HRESULT hr = E_FAIL;
  18. // We don't even want to
  19. // bother passing it to the XML DOM because they throw exceptions. These
  20. // are caught and handled but we still don't want this to happen. We try
  21. // to get XML from the web server, but we get HTML instead if the web server
  22. // fails or the web proxy returns HTML if the site isn't found.
  23. if (!StrCmpNIW(SZ_VALID_XML, bstrXML, (ARRAYSIZE(SZ_VALID_XML) - 1)))
  24. {
  25. hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IXMLDOMDocument, ppXMLDoc));
  26. if (SUCCEEDED(hr))
  27. {
  28. VARIANT_BOOL fIsSuccessful;
  29. // NOTE: This will throw an 0xE0000001 exception in MSXML if the XML is invalid.
  30. // This is not good but there isn't much we can do about it. The problem is
  31. // that web proxies give back HTML which fails to parse.
  32. hr = (*ppXMLDoc)->loadXML(bstrXML, &fIsSuccessful);
  33. if (SUCCEEDED(hr))
  34. {
  35. if (VARIANT_TRUE != fIsSuccessful)
  36. {
  37. hr = E_FAIL;
  38. }
  39. }
  40. }
  41. if (FAILED(hr))
  42. {
  43. (*ppXMLDoc)->Release();
  44. *ppXMLDoc = NULL;
  45. }
  46. }
  47. return hr;
  48. }
  49. STDAPI XMLElem_VerifyTagName(IN IXMLDOMElement * pXMLElementMessage, IN LPCWSTR pwszTagName)
  50. {
  51. BSTR bstrTagName;
  52. HRESULT hr = pXMLElementMessage->get_tagName(&bstrTagName);
  53. if (S_FALSE == hr)
  54. {
  55. hr = E_FAIL;
  56. }
  57. else if (SUCCEEDED(hr))
  58. {
  59. if (!bstrTagName || !pwszTagName || StrCmpIW(bstrTagName, pwszTagName))
  60. {
  61. hr = E_FAIL;
  62. }
  63. SysFreeString(bstrTagName);
  64. }
  65. return hr;
  66. }
  67. STDAPI XMLNodeList_GetChild(IN IXMLDOMNodeList * pNodeList, IN DWORD dwIndex, OUT IXMLDOMNode ** ppXMLChildNode)
  68. {
  69. HRESULT hr = pNodeList->get_item(dwIndex, ppXMLChildNode);
  70. if (S_FALSE == hr)
  71. {
  72. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  73. }
  74. return hr;
  75. }
  76. STDAPI XMLElem_GetElementsByTagName(IN IXMLDOMElement * pXMLElementMessage, IN LPCWSTR pwszTagName, OUT IXMLDOMNodeList ** ppNodeList)
  77. {
  78. BSTR bstrTagName = SysAllocString(pwszTagName);
  79. HRESULT hr = E_OUTOFMEMORY;
  80. *ppNodeList = NULL;
  81. if (bstrTagName)
  82. {
  83. hr = pXMLElementMessage->getElementsByTagName(bstrTagName, ppNodeList);
  84. if (S_FALSE == hr)
  85. {
  86. hr = E_FAIL;
  87. }
  88. SysFreeString(bstrTagName);
  89. }
  90. return hr;
  91. }
  92. STDAPI XMLNode_GetChildTag(IN IXMLDOMNode * pXMLNode, IN LPCWSTR pwszTagName, OUT IXMLDOMNode ** ppChildNode)
  93. {
  94. HRESULT hr = E_INVALIDARG;
  95. *ppChildNode = NULL;
  96. if (pXMLNode)
  97. {
  98. IXMLDOMElement * pXMLElement;
  99. hr = pXMLNode->QueryInterface(IID_PPV_ARG(IXMLDOMElement, &pXMLElement));
  100. if (SUCCEEDED(hr))
  101. {
  102. IXMLDOMNodeList * pNodeList;
  103. hr = XMLElem_GetElementsByTagName(pXMLElement, pwszTagName, &pNodeList);
  104. if (SUCCEEDED(hr))
  105. {
  106. hr = XMLNodeList_GetChild(pNodeList, 0, ppChildNode);
  107. pNodeList->Release();
  108. }
  109. pXMLElement->Release();
  110. }
  111. }
  112. return hr;
  113. }
  114. STDAPI XMLNode_GetTagText(IN IXMLDOMNode * pXMLNode, OUT BSTR * pbstrValue)
  115. {
  116. DOMNodeType nodeType = NODE_TEXT;
  117. HRESULT hr = pXMLNode->get_nodeType(&nodeType);
  118. *pbstrValue = NULL;
  119. if (S_FALSE == hr) hr = E_FAIL;
  120. if (SUCCEEDED(hr))
  121. {
  122. if (NODE_TEXT == nodeType)
  123. {
  124. VARIANT varAtribValue = {0};
  125. hr = pXMLNode->get_nodeValue(&varAtribValue);
  126. if (S_FALSE == hr) hr = E_FAIL;
  127. if (SUCCEEDED(hr) && (VT_BSTR == varAtribValue.vt))
  128. {
  129. *pbstrValue = SysAllocString(varAtribValue.bstrVal);
  130. }
  131. VariantClear(&varAtribValue);
  132. }
  133. else
  134. {
  135. hr = pXMLNode->get_text(pbstrValue);
  136. }
  137. }
  138. return hr;
  139. }
  140. STDAPI XMLNode_GetChildTagTextValue(IN IXMLDOMNode * pXMLNode, IN BSTR bstrChildTag, OUT BSTR * pbstrValue)
  141. {
  142. IXMLDOMNode * pNodeType;
  143. HRESULT hr = XMLNode_GetChildTag(pXMLNode, bstrChildTag, &pNodeType);
  144. if (SUCCEEDED(hr))
  145. {
  146. hr = XMLNode_GetTagText(pNodeType, pbstrValue);
  147. pNodeType->Release();
  148. }
  149. return hr;
  150. }