none
Read XML File using SAX parser.

    Question

  •  

    Hi,

    I have large sie XML File.

    i want to read XML data using SAX parser.

     

    I have to do in vc++

    Is any sample application to do the same?

     

    Friday, December 07, 2007 9:07 AM

Answers

All replies

  • I am not familiar with SAX Parser, but it seems it is a Java library. In VC++, you have other options to parser xml file:

    http://msdn2.microsoft.com/en-us/library/ms752838.aspx

    http://www.codeguru.com/cpp/misc/misc/article.php/c3707/

    http://www.developer.com/xml/article.php/629271

     

    hope it helps

    rico

    Wednesday, December 12, 2007 10:56 AM
  •  Bite Qiu - MSFT wrote:

    I am not familiar with SAX Parser, but it seems it is a Java library. In VC++, you have other options to parser xml file:



    A SAX parser has nothing to do with being Java library or not. It is simply a different way of parsing an XML file (opposed to a DOM parser).Both are simple API's for parsing XML file with the following characteristics:


    • SAX (Simple API for XML)

      SAX is an event-driven API meaning that it works off callback functions that are defined by the user and are called when the corresponding events occur during parsing. The events are always come as a pair...for example, an event is triggered when the open tag ('<tag>') is parsed, a second event is triggered when the corresponding close tag ('</tag>') is parsed. SAX only provides unidirectional parsing thus, previously parsed XML tags cannot be parsed again before starting the parsing again.


    • DOM (Document Object Model)

      DOM is definitely the most common method for parsing an XML file. It is a full object model describing how XML data should be represented. It is way too complex to describe in short terms thus, the interested reader may want to read the official specification. Opposed to SAX, DOM actually provides full bidirectional parsing thus, allows modifications and accessing tags in any order even for previously parsed tags.


    There are many free XML parsers out there...some of the ones you have provided the links are able to parse XML data using both DOM and SAX parsers (such as the Microsoft one).
    Wednesday, December 12, 2007 3:40 PM
  •  Andreas Masur wrote:
     Bite Qiu - MSFT wrote:

    I am not familiar with SAX Parser, but it seems it is a Java library. In VC++, you have other options to parser xml file:



    A SAX parser has nothing to do with being Java library or not. It is simply a different way of parsing an XML file (opposed to a DOM parser).Both are simple API's for parsing XML file with the following characteristics:

    Oh, thanks for the clarification, Andreas. My apology for been inaccurate.

     

    best regards,

    rico

    Thursday, December 13, 2007 2:22 AM
  • Hi,

    Does it mean SAX is not compatible for VC++, I am familiar with MSXML DOM parsing and want to know, Does SAX can be used for better performance than DOM or not?

    Please clarify and let me know how to parse using SAX.
     
    I found some interfaces like ISAXContentHandler/ ISAXXMLReader etc.. for SAX in MSDN and I am unable to understand how to use those interfaces for parsing.

    Thanks in advance,


    -Sreedhar
    Knowledge is like light; It spreads only when you have clear and transparent mind. -Sreedhar
    Thursday, February 12, 2009 12:59 PM
  • I have used ExPat which seems fairly good:

    http://expat.sourceforge.net/

    You can use the 3rd party wrappers linked on that page to do either DOM or SAX processing of XML.

    Thanks.
    Thursday, February 12, 2009 1:21 PM
  • Hi Mark,

    Thanks for your reply.

    Right now I am using MSXML DOM that is working fine, I want to know whether using SAX gives better performance than DOM.

    I dont want to go for 3rd party library now, since using MSXML DOM is also simpler.

    My intention is to know how to use SAX for parsing in VC++






    Knowledge is like light; It spreads only when you have clear and transparent mind. -Sreedhar
    Thursday, February 12, 2009 1:30 PM
  •  Hi,

    I came up with a sample code for parsing XML using SAX.


    ....
    ....
    #import "msxml4.dll" raw_interfaces_only
    using namespace MSXML2;
    .....
    .....

    MyClass::LoadUsingSAX()
    {
     
     ::CoInitialize(NULL);
      // create writer
     CComPtr<IMXWriter> pWriter;
     HRESULT hr = pWriter.CoCreateInstance(__uuidof(MXXMLWriter), NULL);
     _ASSERT(SUCCEEDED(hr));
     
     // create reader
     CComPtr<ISAXXMLReader> pReader;
     hr = pReader.CoCreateInstance(__uuidof(SAXXMLReader), NULL);
     _ASSERT(SUCCEEDED(hr));
     
     // set up handlers
     CComQIPtr<ISAXContentHandler> pContentHandler(pWriter);
     CComQIPtr<ISAXDTDHandler> pDTDHandler(pWriter);
     CComQIPtr<ISAXErrorHandler> pErrorHandler(pWriter);
     hr = pReader->putContentHandler(pContentHandler);
     _ASSERT(SUCCEEDED(hr));
     hr = pReader->putDTDHandler(pDTDHandler);
     _ASSERT(SUCCEEDED(hr));
     hr = pReader->putErrorHandler(pErrorHandler);
     _ASSERT(SUCCEEDED(hr));
     
     // put properties
     //CComVariant var;
     _variant_t var;
     var = (LPUNKNOWN)pWriter;
     hr = pReader->putProperty(
      L"http://xml.org/sax/properties/declaration-handler",
      var
      );
     _ASSERT(SUCCEEDED(hr));
     hr = pReader->putProperty(
      L"http://xml.org/sax/properties/lexical-handler",
      var
      );
     _ASSERT(SUCCEEDED(hr));
     
      var = L"";
     hr = pWriter->put_output(var);
     _ASSERT(SUCCEEDED(hr));
     hr = pWriter->put_omitXMLDeclaration(VARIANT_TRUE);
     _ASSERT(SUCCEEDED(hr));
     
     USES_CONVERSION;
      // parse
     TCHAR szTemp[MAX_PATH];
     m_editCtrl.GetWindowText(szTemp, MAX_PATH); //Getting the xml path
     hr = pReader->parseURL(T2W(szTemp));
     
     if (SUCCEEDED(hr))
     {
      // return output
      var.Clear();
      hr = pWriter->get_output(&var);
      _ASSERT(SUCCEEDED(hr));
      CString str;
      str = var.bstrVal;
      AfxMessageBox(str); //Showing in a Message Box
     }
     
    }




    Knowledge is like light; It spreads only when you have clear and transparent mind. -Sreedhar
    • Proposed as answer by D.Sreedhar Friday, February 13, 2009 9:05 AM
    Friday, February 13, 2009 9:04 AM