MsXmlMisc.cpp

/****************************************************************************/

/**                                                                        **/

/** Copyright (c) 2005 Diodia Software          (http://www.diodia.com/)   **/

/**                                                                        **/

/** Use in any form with or without modification for commercial and non-   **/

/** commercial purposes is permitted.                                      **/

/**                                                                        **/

/****************************************************************************/

#include "stdafx.h"

#include "msxml2.h"

 

#include "XmlHelpers.h"

 

/****************************************************************************/

/*

/* DESCRIPTION : Create XML document.

/*

/* RETURN VALUE: New XML document

/*

/****************************************************************************/

IXMLDOMDocument *CreateXmlDocument(bool Async, const WCHAR *SelectNameSpace)

{

   IXMLDOMDocument *XmlDoc = NULL;

   HRESULT      Result;

 

   Result = CoCreateInstance(__uuidof(DOMDocument30),

                             NULL,

                             CLSCTX_INPROC_SERVER,

                             __uuidof(IXMLDOMDocument),

                             (void**) &XmlDoc);

   if (FAILED(Result))

      return NULL;

 

   Result = XmlDoc->put_async(Async ? VARIANT_TRUE : VARIANT_FALSE);

   if (FAILED(Result))

   {

      XmlDoc->Release();

      return NULL;

   }

 

   Result = XmlDoc->put_validateOnParse(VARIANT_FALSE);

   if (FAILED(Result))

   {

      XmlDoc->Release();

      return NULL;

   }

 

   Result = XmlDoc->put_resolveExternals(VARIANT_FALSE);

   if (FAILED(Result))

   {

      XmlDoc->Release();

      return NULL;

   }

 

   SetXmlXPathSelections(XmlDoc, SelectNameSpace);

 

   return XmlDoc;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Read XML document.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool ReadXmlDocument(IXMLDOMDocument *XmlDoc, const char *FileName)

{

   VARIANT      VarFileName;

   VARIANT_BOOL Status;

   HRESULT      Result;

 

   VariantInit(&VarFileName);

   V_BSTR(&VarFileName) = SysAllocString((bstr_t) FileName);

   V_VT(&VarFileName) = VT_BSTR;

 

   Result = XmlDoc->load(VarFileName, &Status);

 

   VariantClear(&VarFileName);

 

   if (FAILED(Result) || Status != VARIANT_TRUE)

      return false;

  

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Get XML tag name, including name space.

/*

/* RETURN VALUE: Name

/*

/****************************************************************************/

std::string GetXmlNodeTagName(IXMLDOMNode *Node)

{

   BSTR         TagName = NULL;

   bstr_t       bTagName;

   HRESULT      Result;

 

   Result = Node->get_nodeName(&TagName);

   if (FAILED(Result) || !TagName)

      return std::string();

 

   bTagName = TagName;

   SysFreeString(TagName);

 

   return std::string(bTagName);

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Get XML tag attribute value.

/*

/* RETURN VALUE: Attribute

/*

/****************************************************************************/

std::string GetXmlNodeAttribute(IXMLDOMNode *Node, const char *Name)

{

   IXMLDOMNamedNodeMap *NodeMap = NULL;

   IXMLDOMNode *Attribute = NULL;

   BSTR         ItemName;

   VARIANT      VarValue;

   std::string  Value;

   HRESULT      Result;

 

   Result = Node->get_attributes(&NodeMap);

   if (FAILED(Result) || !NodeMap)

      return Value;

 

   ItemName = SysAllocString((bstr_t) Name);

   Result = NodeMap->getNamedItem(ItemName , &Attribute);

   SysFreeString(ItemName);

   if (FAILED(Result) || !Attribute)

   {

      NodeMap->Release();

      return Value;

   }

 

   Result = Attribute->get_nodeValue(&VarValue);

   Attribute->Release();

   NodeMap->Release();

   if (FAILED(Result))

      return Value;

 

   Value = (bstr_t) VarValue;

 

   VariantClear(&VarValue);

 

   return Value;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Set XML tag attribute value.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool SetXmlNodeAttribute(IXMLDOMNode *Node, const char *Name, const char *Value)

{

   IXMLDOMElement *Element = NULL;

   VARIANT      VarValue;

   HRESULT      Result;

 

   Result = Node->QueryInterface(IID_IXMLDOMElement, (void **) &Element);

   if (FAILED(Result) || !Element)

      return false;

 

   VariantInit(&VarValue);

   V_BSTR(&VarValue) = SysAllocString((bstr_t) Value);

   V_VT(&VarValue) = VT_BSTR;

 

   Result = Element->setAttribute((bstr_t) Name, VarValue);

 

   VariantClear(&VarValue);

   Element->Release();

 

   if (FAILED(Result))

      return false;

 

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Get XML tag content.

/*

/* RETURN VALUE: Text

/*

/****************************************************************************/

std::string GetXmlNodeText(IXMLDOMNode *Node)

{

   BSTR         Text;

   std::string  Value;

   HRESULT      Result;

 

   Result = Node->get_text(&Text);

   if (FAILED(Result) || !Text)

      return Value;

 

   Value = (bstr_t) Text;

 

   SysFreeString(Text);

 

   return Value;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Set selection language to XPath.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void SetXmlXPathSelections(IXMLDOMDocument *XmlDoc, const WCHAR *SelectNameSpace)

{

   IXMLDOMDocument2 *XmlDoc2 = NULL;

   VARIANT      VarLanguage;

   VARIANT      VarNameSpace;

   HRESULT      Result;

 

   Result = XmlDoc->QueryInterface(IID_IXMLDOMDocument2, (void **) &XmlDoc2);

   if (FAILED(Result))

      return;

 

   VariantInit(&VarLanguage);

   V_BSTR(&VarLanguage) = SysAllocString(L"XPath");

   V_VT(&VarLanguage) = VT_BSTR;

 

   XmlDoc2->setProperty(L"SelectionLanguage", VarLanguage);

 

   VariantClear(&VarLanguage);

 

   VariantInit(&VarNameSpace);

   V_BSTR(&VarNameSpace) = SysAllocString(SelectNameSpace);

   V_VT(&VarNameSpace) = VT_BSTR;

 

   XmlDoc2->setProperty(L"SelectionNamespaces", VarNameSpace);

 

   VariantClear(&VarNameSpace);

 

   XmlDoc2->Release();

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Create a new tag node in specified XML document.

/*

/* RETURN VALUE: New node

/*

/****************************************************************************/

IXMLDOMNode *CreateXmlElementNode(IXMLDOMDocument *XmlDoc, const char *TagName)

{

   IXMLDOMNode *NewNode = NULL;

   VARIANT      VarType;

   HRESULT      Result;

 

   VariantInit(&VarType);

   V_I4(&VarType) = NODE_ELEMENT;

   V_VT(&VarType) = VT_I4;

 

   Result = XmlDoc->createNode(VarType, (bstr_t) TagName, L"http://www.diodia.com/gdf", &NewNode);

   if (FAILED(Result) || NewNode == NULL)

      return NULL;

 

   return NewNode;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Get XML text from node.

/*

/* RETURN VALUE: XML text

/*

/****************************************************************************/

std::string GetXmlNodeXml(IXMLDOMNode *Node)

{

   BSTR         NodeXml = NULL;

   std::string  XmlResult;

   HRESULT      Result;

 

   Result = Node->get_xml(&NodeXml);

   if (FAILED(Result) || NodeXml == NULL)

      return XmlResult;

 

   XmlResult = (bstr_t) NodeXml;

 

   SysFreeString(NodeXml);

 

   return XmlResult;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Set text in node.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool SetXmlNodeText(IXMLDOMNode *Node, const char *Text)

{

   HRESULT      Result;

 

   Result = Node->put_text((bstr_t) Text);

   if (FAILED(Result))

      return false;

 

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Create new simple text node as child of specified node.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool AppendChildXmlTextNode(IXMLDOMDocument *XmlDoc, IXMLDOMNode *Node, const char *Name, const char *Text)

{

   IXMLDOMNode *Child;

   IXMLDOMNode *NewChild;

   HRESULT      Result;

 

   Child = CreateXmlElementNode(XmlDoc, Name);

   if (!Child)

      return false;

 

   if (!SetXmlNodeText(Child, Text))

   {

      Child->Release();

      return false;

   }

 

   Result = Node->appendChild(Child, &NewChild);

   if (FAILED(Result) || !NewChild)

   {

      Child->Release();

      return false;

   }

 

   Child->Release();

   NewChild->Release();

 

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Append Child as child of Node.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool AppendChildXmlNode(IXMLDOMNode *Node, IXMLDOMNode *Child, bool Clone)

{

   IXMLDOMNode *NewChild = NULL;

   HRESULT      Result;

 

   if (Clone)

   {

      Result = Child->cloneNode(VARIANT_TRUE, &Child);

      if (FAILED(Result) || !Child)

         return false;

   }

 

   Result = Node->appendChild(Child, &NewChild);

   if (FAILED(Result) || !NewChild)

      return false;

 

   NewChild->Release();

   if (Clone)

      Child->Release();

 

   return true;

}





Source Files
   Commands.cpp
   Context.cpp
   CreatePath.cpp
   DateParser.cpp
   DlgEditFeedC.cpp
   DlgManageFeedsC.cpp
   Hook.cpp
   Iso8601.cpp
   MsXmlMisc.cpp
   MsXmlSelect.cpp
   RegistryNotify.cpp
   RegistryUtil.cpp
   RSS.cpp
   RSS.def
   RSS.idl
   RssAction.cpp
   RssDownloader.cpp
   RssFeed.cpp
   RssHelpers.cpp
   RssInit.cpp
   RssItem.cpp
   RssMultiFeed.cpp
   RssTimer.cpp
   RssXml.cpp
   StdAfx.cpp

Header Files
   Commands.h
   ContextC.h
   DateParser.h
   DlgEditFeedC.h
   DlgManageFeedsC.h
   Helper.h
   HookC.h
   RegistryNotify.h
   RegistryNotifyC.h
   Resource.h
   RssDownloaderC.h
   RssFeedC.h
   RssHelpers.h
   RssItemC.h
   RssMultiFeedC.h
   StdAfx.h
   ThreadObject.h
   XmlHelpers.h

Resource Files
   about.htm
   DiodiaLogoSmall.gif
   logo.bmp
   manifest.xml
   read.bmp
   RSS.rc
   RSS.rgs
   ToolBandLayout.xml
 

© 2002-2008 Diodia Software