Skip to main content

Hi
I'm trying to process an XML file, straight forward I would think, if only!
Here is the xml file.
I get "Type Mismatch" when setting str
I have tried a myriad of variations but just cannot get hold of the value.
Any ideas?
Thanks
Adrian

<?xml version="1.0" encoding="ISO-8859-1"?>
  <Data>
    <Field name="First"></Field>
    <Field name="Second"></Field>
  </Data>

Emulation "TN5250" Main

Sub Main Dim objXMLDoc, Root, NodeList, str as String, colNodes  
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 objXMLDoc.async = False
 objXMLDoc.load("u:\\rumba\\scripts\\testxml.xml")

 Set Root = objXMLDoc.documentElement
 Set NodeList = Root.getElementsByTagName("Field")
 For Each Elem In NodeList
  set str = Elem.getAttribute("name")
  msgbox str
 Next
end sub


#Rumba

Hi
I'm trying to process an XML file, straight forward I would think, if only!
Here is the xml file.
I get "Type Mismatch" when setting str
I have tried a myriad of variations but just cannot get hold of the value.
Any ideas?
Thanks
Adrian

<?xml version="1.0" encoding="ISO-8859-1"?>
  <Data>
    <Field name="First"></Field>
    <Field name="Second"></Field>
  </Data>

Emulation "TN5250" Main

Sub Main Dim objXMLDoc, Root, NodeList, str as String, colNodes  
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 objXMLDoc.async = False
 objXMLDoc.load("u:\\rumba\\scripts\\testxml.xml")

 Set Root = objXMLDoc.documentElement
 Set NodeList = Root.getElementsByTagName("Field")
 For Each Elem In NodeList
  set str = Elem.getAttribute("name")
  msgbox str
 Next
end sub


#Rumba

Try this ...

Sub Main


XML_Load("C:\\temp\\xml\\xmlfile.xml")

End Sub


'--------------------------------
' LOAD XML DOCUMENT
'-------------------------------

Function XML_Load(path)

Dim objXML As Object
Set objXML = CreateObject("Microsoft.XMLDOM")


objXML.validateOnParse = True               ' Check document structure
objXML.setProperty "SelectionLanguage", "XPath"            'use  XPath to select specific Node


' load document
If Not objXML.Load(path) Then
' Error when loading
MsgBox "ERROR LOADING DOCUMENT." & vbCrLf & vbCrLf _
& "REASON: " & objXML.parseError.reason & vbCrLf _
& "LINE: " & objXML.parseError.Line, vbOKOnly Or vbExclamation, "ERROR"
Set objXML = Nothing
End If

'just  for debugging and see results

Print objXML.xml
Print objXML.selectSingleNode("Data/Field").text

End Function


Hi
I'm trying to process an XML file, straight forward I would think, if only!
Here is the xml file.
I get "Type Mismatch" when setting str
I have tried a myriad of variations but just cannot get hold of the value.
Any ideas?
Thanks
Adrian

<?xml version="1.0" encoding="ISO-8859-1"?>
  <Data>
    <Field name="First"></Field>
    <Field name="Second"></Field>
  </Data>

Emulation "TN5250" Main

Sub Main Dim objXMLDoc, Root, NodeList, str as String, colNodes  
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 objXMLDoc.async = False
 objXMLDoc.load("u:\\rumba\\scripts\\testxml.xml")

 Set Root = objXMLDoc.documentElement
 Set NodeList = Root.getElementsByTagName("Field")
 For Each Elem In NodeList
  set str = Elem.getAttribute("name")
  msgbox str
 Next
end sub


#Rumba

thanks aescudero, that seems to work (well it doesn't crash) but the final Print outputs blanks (I guess because I'm after the attribute)


Hi
I'm trying to process an XML file, straight forward I would think, if only!
Here is the xml file.
I get "Type Mismatch" when setting str
I have tried a myriad of variations but just cannot get hold of the value.
Any ideas?
Thanks
Adrian

<?xml version="1.0" encoding="ISO-8859-1"?>
  <Data>
    <Field name="First"></Field>
    <Field name="Second"></Field>
  </Data>

Emulation "TN5250" Main

Sub Main Dim objXMLDoc, Root, NodeList, str as String, colNodes  
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
 objXMLDoc.async = False
 objXMLDoc.load("u:\\rumba\\scripts\\testxml.xml")

 Set Root = objXMLDoc.documentElement
 Set NodeList = Root.getElementsByTagName("Field")
 For Each Elem In NodeList
  set str = Elem.getAttribute("name")
  msgbox str
 Next
end sub


#Rumba

correct! Modify your XML and see you will get results.

Anyhow you can have Access to the Attributes :

Print objXml.selectSingleNode("/Data/Field]").attributes(0).text
Print objXml.selectSingleNode("/Data/Field]").attributes.length

You should further extend the code a bit  to check if an XML Elements exists

e.g.

Dim oNode As IXMLDOMNode

Set oNode = objXML.SelectSingleNode("/Address/Contact[Name='Peter']/Street")

If Not oNode Is Nothing Then

Debug.Print oNode.Text

Else

MsgBox "Eelemet not found!", vbOKOnly Or vbExclamation, "Error"