Why Replace the DOM Interface

September 23, 2009 by datapig Leave a reply »

Here's a question for you super-geeks out there. Why would Microsoft create its own object designed to traverse XML documents?

I don't normally get into the high-minded technical side of Excel and Access. But I do use XML quite a bit in my work with Xcelsius. Last year, I noticed that Microsoft introduced new objects with Office 2007: The CustomXMLPart object, and the CustomXMLNode object.

 

The CustomXMLPart object allows you to programmatically add and work with custom XML parts. The CustomXMLNode object is designed to provide functionality similar to that found in the IXMLDOMNode interface when working with custom XML parts. Together, the CustomXMLPart and CustomXMLNode objects enable you to traverse an XML document without relying on the DOM interface found in MSXML.

 

As far as I can tell, these objects are actually designed to work with the custom XML parts in an office document. But you can use these new objects as a way to read any XML document without the DOM interface. All you have to do is add the XML file as a custom XML part to the active document.

In this example, I first add a standard XML file to the XML parts collection of the active workbook. Then I traverse the XML document using the new CustomXMLNodes collection.

Visual Basic:
  1. Sub Add_Traverse_CustomXMLPart()
  2.  
  3. Dim oCustomPart As CustomXMLPart
  4. Dim oCustomNode As CustomXMLNode
  5. Dim oCustomNodes As CustomXMLNodes
  6.  
  7. 'Add a Custom XML Part from a file and then load
  8. Set oCustomPart = ActiveWorkbook.CustomXMLParts.Add
  9. oCustomPart.Load "C:\EmployeeSales.xml"
  10.  
  11. 'Return all nodes for the employee who has invoice amount over 3000
  12. Set oCustomNodes= oCustomPart.SelectNodes("//Employee[InvoiceAmount>3000]")
  13. For Each oCustomNode In oCustomNodes
  14. Debug.Print oCustomNode.Text
  15. Next
  16.  
  17. 'Delete the Custom XML Part
  18. oCustomPart.Delete
  19.  
  20. End Sub

To test out this code, enter this XML into notepad and save as C:\ EmployeeSales.xml .

Visual Basic:
  1. <!--l version="1.0-->
  2. 2312
  3. Mike
  4. Alexander
  5. 100
  6. 2300
  7. 24601
  8. Ethan
  9. Alexander
  10. 200
  11. 3211
  12.  

 

So if you're automating XML documents (whether they are custom XML parts or not), it looks as though you can skip the DOM interface. The obvious benefit is that you don't have to point to an external reference library when coding XML processes using these new objects. The obvious drawback is that your code won't work with versions before Office 2007.

Any guesses as to why Microsoft would choose to create new objects specifically designed to work with XML?

RELATED STUFF

  1. Zoom into Spreadsheet on Double-Click
  2. Mocking the ‘Merge & Center’ Icon
  3. VBE Tips I Wish I Knew 5 Years Ago
  4. Exploding a Dataset using a PivotTable
  5. Highlighting the Active Row and Column
  6. ActiveX Controls and Intellisense
  7. Sending Data to Excel using Access SQL
  8. Transposing a Dataset in Access
  9. Using the Switch Function in Excel
  10. Why is Excel so Negative about Hiding Subtotals
Advertisement

6 Responses

  1. Jon Peltier says:

    I think it's part of their patent infringement strategy:

    Microsoft Penalized $200M for Word XML Patent
    http://www.tomshardware.com/news/microsoft-word-xml-patent-infringement,7861.html

  2. jeff weir says:

    Crikey Mike...with in-depth posts like these, you're constantly reminding me that you're smarter than I think you are!

  3. jeff weir says:

    (...which was plenty smart to start with)

  4. Stray__Cat says:

    I think that having nore than one choice is always a plus.
    I target only 2007+ versions and not having to rely on an external library may be worthwile.

    If we go deeper, though, I'm pretty sure we'll find a few subtle differences between the two.

  5. No joke you know what you are talking about , thank you

  6. johny why says:

    hi, can you please adjust your xml so that it's readable? it seems that the xml tags are not coming through. thanks for the code!

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>