XML in VBA

Shingles

New member
Local time
Yesterday, 19:12
Joined
Mar 9, 2012
Messages
3
Hi All,
I need to use VBA (in Access) to manipulate XML nodes. I'm having a crazy difficult time. I found some code online to help me out and this is what I pulled together. I keep getting an error saying that the 'XML document must have a top level element'. The error code is -1072896680 (c00ce558). I tried refencing Microsoft XML 3.0, 4.0 and 6.0 but always the same error.

Public Sub ModifyXML()

Dim myxml As MSXML2.DOMDocument
Dim myxmlstr As String


Set myxml = New MSXML2.DOMDocument


myxml.Load ("C:\Temp\xmldoc.xml")


If Not myxml.loadXML(myxmlstr) Then
Err.Raise myxml.parseError.errorCode, , myxml.parseError.reason
End If


'and now i'm supposed to be able to manipulate the code, but the error pops up instead


End Sub

The XML fine, I pasted it below and I tried a whole bunch of other variations of it too.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


I found someone post a message about a forum that is supposed to be helpful for such things. But seems like this is for Visual Basic 6... so I should be able to use it with VBA right (I'm using MS Office 2003 at work... I know... but I have no choice)? I can't post the link 'cause I'm not at 10 posts yet... I get it... its for the robot spammers. But you can try searching ChilKatXML.
 
Read this: http://msdn.microsoft.com/en-us/library/aa468547.aspx

My bet is that the document hasn't finished loading before your code moves on.

Insert

myxml.async=false

before calling .Load

Also, the myxmlstr is not set to anything. Why do you want to reload from string, when you have just loaded from file? This is the likely cause of your error message - you have fed the beast an empty string.
 
Last edited:
in your code

myxmlstr

is not set to anything. it is probably trying to load "nothing" so you get an error.
 
Thanks folks. I was copying code online assuming it was right, but apparently not.

I have another question about the validate function (a member of DOMDocument). Does it 'validate' or check if the document is 'well formed'? It seems to me that it does the latter. I went ahead and put in some data that would invalidate the document according the the XSD file that is associated with the XML (I put in text where numbers were expected), but it did not catch it. If I messed up tags so that they were not matching however it would catch it.

I guess I answered my question, it only checks for well formed XML. So how can I test if it is valid against an XSD? I do have the XSD referenced in the root node in the XML.

I didn't want to start a whole new thread, so I thought I'd add it here since it is related. Please let me know if this is not cool.
 
Check out the Ribbon Builder I just posted in the sample database. I don't think the mods approved it yet, as I just posted it yesterday. It will be about the only reference you will find for manipulating xml via Access online.
 
Check out the Ribbon Builder I just posted in the sample database. I don't think the mods approved it yet, as I just posted it yesterday. It will be about the only reference you will find for manipulating xml via Access online.
I;ll definitely check it out, but I'm guessing it is for Office 2010 right? I am still using 2003 at work. They are in the process of migrating to 2010 this year, but ya I'm not holding my breath either..
 
It's in the 2000 format. So you can check out the code, and even build a ribbon in xml. Though I believe custom ribbons were not available until 2007, so you just won't be able to see the effect the ribbon has.
 

Users who are viewing this thread

Back
Top Bottom