Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse XML using vba

I work in VBA, and want to parse a string eg

<PointN xsi:type='typens:PointN'  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:xs='http://www.w3.org/2001/XMLSchema'>     <X>24.365</X>     <Y>78.63</Y> </PointN> 

and get the X & Y values into two separate integer variables.

I'm a newbie when it comes to XML, since I'm stuck in VB6 and VBA, because of the field I work in.

How do I do this?

like image 252
Devdatta Tengshe Avatar asked Aug 14 '08 16:08

Devdatta Tengshe


People also ask

Can Excel parse XML?

Note: FILTERXML is not available in Excel on the Mac, or in Excel Online. The FILTERXML function can parse XML using XPath expressions. XML is a special text format designed to transport data, with features that allow it to be easily parsed and verified by software.

What is XML in VBA?

Excel VBA XML. The full form of XML is eXtensible Markup Language which is much like an HTML file, is designed to store and transport the data from different programs. XML file stores the data which includes the splits and separators. We can create a VBA code by which we can import the data from the XML file into Excel ...


2 Answers

Thanks for the pointers.

I don't know, whether this is the best approach to the problem or not, but here is how I got it to work. I referenced the Microsoft XML, v2.6 dll in my VBA, and then the following code snippet, gives me the required values

Dim objXML As MSXML2.DOMDocument  Set objXML = New MSXML2.DOMDocument  If Not objXML.loadXML(strXML) Then  'strXML is the string with XML'     Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason End If   Dim point As IXMLDOMNode Set point = objXML.firstChild  Debug.Print point.selectSingleNode("X").Text Debug.Print point.selectSingleNode("Y").Text 
like image 164
Devdatta Tengshe Avatar answered Sep 30 '22 04:09

Devdatta Tengshe


This is a bit of a complicated question, but it seems like the most direct route would be to load the XML document or XML string via MSXML2.DOMDocument which will then allow you to access the XML nodes.

You can find more on MSXML2.DOMDocument at the following sites:

  • Manipulating XML files with Excel VBA & Xpath
  • MSXML - http://msdn.microsoft.com/en-us/library/ms763742(VS.85).aspx
  • An Overview of MSXML 4.0
like image 34
rjzii Avatar answered Sep 30 '22 04:09

rjzii