LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Insert XML comment in XML file

HI,
 
Do You know how to insert XML comment in XML file.
I am using the cvixml library wich not permit this.
I know it is possible with the msxmldom.h library, but do you have some simple example ?
(CVI 7.1.1)
 
Thanks..
 
 
0 Kudos
Message 1 of 3
(5,323 Views)
See if the following helps. Pass TRUE (non-zero) in the insertBelow parameter to insert the comment below the element, or FALSE (zero) to insert above the element.

#include <cvixml.h>

CVIXMLStatus CVIXMLInsertElementComment(CVIXMLDocument document,
        CVIXMLElement element, const char *comment, int insertBelow)
{
    HRESULT                      error;
    MSXMLObj_IXMLDOMDocument     documentHandle = 0;
    MSXMLObj_IXMLDOMElement      elementHandle = 0, childHandle = 0, parentHandle = 0;
    MSXMLObj_IXMLDOMNode         commentHandle = 0, newCommentHandle = 0;
    CVIXMLElement                child = 0, parent = 0;
    VARIANT                      vtRefChild;

    errChk(CA_VariantSetEmpty(&vtRefChild));
    errChk(CVIXMLGetDocumentActiveXHandle(document, &documentHandle));
    errChk(CVIXMLGetElementActiveXHandle(element, &elementHandle));
   
    errChk(MSXML_IXMLDOMDocumentcreateComment(documentHandle, NULL, comment, &commentHandle));
    if (insertBelow)
    {
        // Insert below the user's element
        int numChildren;
        errChk(CVIXMLGetNumChildElements(element, &numChildren));
        if (numChildren == 0)
        {
            // No child elements; just append the comment
            errChk(MSXML_IXMLDOMElementappendChild(elementHandle, NULL, commentHandle,
                &newCommentHandle));
        }
        else
        {
            // Get the first child and insert comment before it
            errChk(CVIXMLGetChildElementByIndex(element, 0, &child));
            errChk(CVIXMLGetElementActiveXHandle(child, &childHandle));
            errChk(CA_VariantSetObjHandle(&vtRefChild, childHandle, CAVT_UNKNOWN));
            errChk(MSXML_IXMLDOMElementinsertBefore(elementHandle, NULL, commentHandle,
                vtRefChild, &newCommentHandle));
        }
    }
    else
    {
        // Insert above the user's element
        errChk(CVIXMLGetParentElement(element, &parent));
        if (parent == 0)
        {
            // No parent element => this is the root element
            errChk(CA_VariantSetObjHandle(&vtRefChild, elementHandle, CAVT_UNKNOWN));
            errChk(MSXML_IXMLDOMDocumentinsertBefore(documentHandle, NULL, commentHandle,
                vtRefChild, &newCommentHandle));
        }
        else
        {
            // Get the parent element and use this for the insertion
            errChk(CVIXMLGetElementActiveXHandle(parent, &parentHandle));
            errChk(CA_VariantSetObjHandle(&vtRefChild, elementHandle, CAVT_UNKNOWN));
            errChk(MSXML_IXMLDOMElementinsertBefore(parentHandle, NULL, commentHandle,
                vtRefChild, &newCommentHandle));
        }
    }
   
Error:
    CA_VariantClear(&vtRefChild);
    if (child)
        CVIXMLDiscardElement(child);
    if (childHandle)
        CA_DiscardObjHandle(childHandle);
    if (parent)
        CVIXMLDiscardElement(parent);
    if (parentHandle)
        CA_DiscardObjHandle(parentHandle);
    if (documentHandle)
        CA_DiscardObjHandle(documentHandle);
    if (elementHandle)
        CA_DiscardObjHandle(elementHandle);
    if (commentHandle)
        CA_DiscardObjHandle(commentHandle);
    if (newCommentHandle)
        CA_DiscardObjHandle(newCommentHandle);
    return error;
}

void main(void)
{
    HRESULT        error;
    CVIXMLElement  root = 0, elem = 0, child = 0;
    CVIXMLDocument doc = 0;
    const char *   FILE_PATH = "c:\\temp\\temp.xml";
   
    errChk(CVIXMLLoadDocument(FILE_PATH, &doc));
   
    errChk(CVIXMLGetRootElement(doc, &root));
    errChk(CVIXMLInsertElementComment(doc, root, "comment above root element", 0));
    errChk(CVIXMLInsertElementComment(doc, root, "comment below root element", 1));
   
    errChk(CVIXMLGetChildElementByIndex(root, 1, &elem));
    errChk(CVIXMLInsertElementComment(doc, elem, "comment above second element", 0));
    errChk(CVIXMLInsertElementComment(doc, elem, "comment below second element", 1));

    errChk(CVIXMLGetChildElementByIndex(elem, 0, &child));
    errChk(CVIXMLInsertElementComment(doc, child, "comment above child element", 0));
    errChk(CVIXMLInsertElementComment(doc, child, "comment below child element", 1));

    errChk(CVIXMLSaveDocument(doc, 0, FILE_PATH));
   
Error:
    if (root)
        CVIXMLDiscardElement(root);
    if (elem)
        CVIXMLDiscardElement(elem);
    if (child)
        CVIXMLDiscardElement(child);
    if (doc)
        CVIXMLDiscardDocument(doc);
    if (error < 0)
    {
        char buf[512];
        CVIXMLGetErrorString(error, buf, sizeof(buf));
        MessagePopup("Error", buf);
    }
}

Message 2 of 3
(5,300 Views)

Thanks !!

 

 

0 Kudos
Message 3 of 3
(5,296 Views)