How to load/save xml attributes with xml:lang with DOM in C++?

Hi Experts,
I have xml file as below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<employer id="100">
      <name xml:lang="en">Tri, Tran</name>
      <description xml:lang="en">Deve</description>
      <name xml:lang="cn"> ø      À÷</name>
      <description xml:lang="cn"> b‚s</description>
      <name xml:lang="vn"> Tr§n Hïu Trí</name>
      <description xml:lang="vn">MÙt cái ·c t£ Ã kiÃm tra</description>
      <age>25</age>
</employer>

I used msxml to load and parser xml document.

My question is how to work with xml:lang with msxml (DOM) in C++, it means how I can get the attributes base on language or location,
If now, I just want to get english  text (xml:lang="en")  then how to do?.

Pls help me for this soon.
Thank in advance!
LVL 1
MobinexAsked:
Who is Participating?
 
OnegaZhangConnect With a Mentor Commented:
http://msdn2.microsoft.com/en-us/library/ms754679.aspx
HRESULT setProperty(BSTR strProp, VARIANT strVal);
HRESULT getProperty(BSTR strProp, VARIANT* strVal);

Parameters

strProp

    A BSTR string whose value is "SelectionLanguage".

strVal

    A VARIANT string, "XPath" or "XSLPattern".

Remarks

In MSXML 3.0 only, the default for this property is "XSLPattern"; however, you can set this property to "XPath" to switch the query language from XSLPattern to XPath. Once the property is set, there is no way to switch back without creating an instance of a new DOM object.

For MSXML 4.0 and later, only a value of "XPath" is recognized as XPath is the only query language supported and therefore, this property can be ignored.
0
 
OnegaZhangCommented:
if you want to get all nodes that have attribute xml:lang="en", XPath is a good method.

#include "stdafx.h"
#include <TCHAR.H>
#include <stdio.h>
#include <time.h>
#import "msxml4.dll"
using namespace MSXML2;

inline void EVAL_HR( HRESULT _hr )
   { if FAILED(_hr) throw(_hr); }

void printMSXMLError(const IXMLDOMDocument2Ptr& pXMLDoc)
{
    try
    {
        IXMLDOMParseErrorPtr pError = NULL;
        EVAL_HR(pXMLDoc->get_parseError(&pError));
        _ftprintf(stderr, pError->reason);
    }
    catch(...)
    {//Exception handling
    }
}

int main(int argc, char* argv[])
{
    try
    {
        EVAL_HR(CoInitialize(NULL));
       
        //      Make sure MSXML 4.0 is installed
       
        IXMLDOMDocument2Ptr pXMLDoc = NULL;
        EVAL_HR(pXMLDoc.CreateInstance("Msxml2.DOMDocument.4.0"));
       
        //      Load the document synchronously
        pXMLDoc->async = false;
       
        VARIANT_BOOL varLoadResult = pXMLDoc->load(_T("C:\\onega\\Untitled1.xml"));
        //      Load the XML document
        if (varLoadResult)
        {
            pXMLDoc->setProperty("SelectionLanguage", "XPath");
            IXMLDOMNodeListPtr pEnglishNodes = pXMLDoc->selectNodes("/employer/*[@xml:lang='en']");
            for (long i=0; i<pEnglishNodes->Getlength(); i++)
            {
                IXMLDOMNodePtr pEnglishNode = pEnglishNodes->Getitem(i);
                pEnglishNode->Gettext();
                pEnglishNode->GetnodeName();
                printf("node name:%s, text:%s\n",
                    (LPCSTR)pEnglishNode->GetnodeName(),
                    (LPCSTR)pEnglishNode->Gettext());
            }
        }
        else
        {//      Load Failed
            printMSXMLError(pXMLDoc);
        }
       
    }
    catch(...)
    {//      Exception handling
    }
   
    CoUninitialize();
    return 0;
}
0
 
MobinexAuthor Commented:
Can you explain about:

pXMLDoc->setProperty("SelectionLanguage", "XPath");

What SelectionLanguage and XPath properties? these properties defined by MSXML engine?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
OnegaZhangCommented:
Yes.
0
 
OnegaZhangCommented:
SelectionLanguage is a private property of MSXML, not part of XML standard.
0
 
MobinexAuthor Commented:
oh, Hi OnegaZhang
I have more question about setPropeties and MSXML library.
I see setPropeties is supported for msxml4.dll and IXMLDOMDocument2Ptr
I am using msxml.dll and IXMLDOMDocumentPtr, using namespace MSXML not MSXML2.
So how I can know what msxml is supported by system? I mean how we sure our parser xml can work on any system that has msxml.dll or msxml3.dll or msxml.4,....
How to load xml:lang with  IXMLDOMDocumentPtr?
My loading function is:
      hRes = document.CreateInstance(L"Msxml2.DOMDocument.4.0");
      if (FAILED(hRes))
      {
            hRes = document.CreateInstance(L"Msxml2.DOMDocument.3.0");
            if (FAILED(hRes))
            {
                  hRes = document.CreateInstance(L"Msxml2.DOMDocument.2.6");
                  if (FAILED(hRes))
                  {
                        hRes = document.CreateInstance(L"Msxml2.DOMDocument");
                        
                        if (FAILED(hRes))
                        {
                              hRes = document.CreateInstance(L"Msxml.DOMDocument");
                              if(FAILED(hRes)){
                                    throw DOMException(L"Can not get Instance of Msxml DOMDocument");
                              }
                        }
                  }
            }
      }

so, it suported MSXML2?
Pls help me again to clear what need to do and how to sure our parser can work on any msxml library that is in any system.

Thank again!


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.