[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-12
6
Medium Priority
?
836 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:Mobinex
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 20071716
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
 
LVL 1

Author Comment

by:Mobinex
ID: 20075604
Can you explain about:

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

What SelectionLanguage and XPath properties? these properties defined by MSXML engine?
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 20077100
Yes.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:OnegaZhang
ID: 20077142
SelectionLanguage is a private property of MSXML, not part of XML standard.
0
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 1500 total points
ID: 20077158
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
 
LVL 1

Author Comment

by:Mobinex
ID: 20082766
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question