Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Template inheritance question

Back to Forum: Tech Off
  • User profile image
    Matthew van Eerde

    So I'm writing an XML document tree-walker that goes node-by-node, starting at the root, depth-first.
    I'm writing it in C++/COM.

    I have the algorithm all figured out, including a function
    HRESULT HandleTree(CComPtr<IXMLDOMNode> &pNode)
    which handles that node, its children, and siblings.

    I also have the document itself in a CComPtr<IXMLDOMDocument> pointer.

    but... I'm running into trouble trying to do this:

    HRESULT HandleDocument() {
       CComPtr<IXMLDOMDocument> pDoc;
       // set pDoc...
       hr = HandleTree(pDoc); // PROBLEM LINE
       // deal with hr...
       return hr;
    }

    That HandleTree call doesn't work because...
    although IXMLDOMDocument inherits from IXMLDOMNode...
    CComPtr<IXMLDOMDocument> does not inherit from CComPtr<IXMLDOMNode> (of course.)

    So far the best idea I've come up with is this:
    Given the root, call .firstChild to get the first node; then call .parentNode on that to get the root back again.

    I'm not particularly impressed with this idea, and I have this nagging feeling I'm missing something obvious.

    If there was a .rootNode property of XMLDOMDocument objects, that would solve my problem.

    Any ideas?

  • User profile image
    Matthew van Eerde

    Wait a minute, I think I figured it out.  More in a minute if it works...

    EDIT: yup, this works.

    CComPtr<IXMLDOMDocument> pDoc;
    // set pDoc...
    CComQIPtr<IXMLDOMNode> pNode(pDoc);
    if (pNode) {
       // do something with pNode...
    } else {
       // error handling...
    }

    Now I'm trying to do the same thing to convert IXMLDOMNode to IXMLDOMElement (so I can extract the tagName property) but it's not working Sad

    EDIT: ah, it was working.  I forgot about the non-element "#document" and "xml" nodes Smiley  So all is well.
    (#document is the root node; xml is the <?xml ... ?> declaration.)

  • User profile image
    Sven Groot

    The best way to get the first real node is to use documentElement.

    Man, I haven't done MSXML in C++ for years...

  • User profile image
    Matthew van Eerde

    What are your thoughts on DOM vs. SAX?

  • User profile image
    Sven Groot

    SAX is more performant but an absolute PITA to work with just because of all the methods you need to implement, and keeping state between their calls. Plus if your access pattern is not linear you can't use SAX. I've also found it a disadvantage that you can't pause or stop parsing when you've got the data you need.

    Unless you're just linearly going through the file to build strongly typed object or the file is too large to efficiently load in the DOM or you memory efficiency is absolutely paramount, I'd go with the DOM.

    In the end, I greatly prefer a pull-model parser such as .Net's XmlReader. Much more flexible than SAX's push-model.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.