Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

generating xml file from a VB app? 1

Status
Not open for further replies.

gabster

Programmer
Oct 23, 2001
192
US
Hello everyone!

I would like to generate from a VB app (at runtime) an xml file containing the values of the form's items (txt fields, check boxes, combo boxes etc.)

Can someone please give me an example please?

Thanks a lot,
gabi.
 
Code:
'Set a reference to Microsoft XML, 4.0
Dim oXml As MSXML2.DOMDocument40
Dim oXMLTempNode As MSXML2.IXMLDOMNode
Dim oChild As MSXML2.IXMLDOMNode
Dim oValue As MSXML2.IXMLDOMNode

Set oXml = New MSXML2.DOMDocument40
   
' First Create the top-level Node
Set oXml.documentElement = oXml.createElement("Root")

'Code to Repeat:
'Now create the first child node
Set oXMLTempNode = oXml.documentElement.appendChild(oXml.createElement("FormElement"))

Set oChild = oXMLTempNode.appendChild(oXMLTempNode.ownerDocument.createElement("MyTextField"))
Set oValue = oChild.appendChild(oChild.ownerDocument.createTextNode("ValueofMyTextField"))

'Repeat the code for the other form fields

Set oChild = Nothing
Set oValue = Nothing
Set oXMLTempNode = Nothing
Set oXml = Nothing

Result will be:
<Root>
<FormElement>
<MyTextField>ValueofMyTextField<\MyTextField>
<\FormElement>
<\Root>

Good luck!

Jordi Reineman
 
Of course you have to save the document at the end... :~/
Code:
oXml.save &quot;C:\Temp.xml&quot;

Jordi Reineman
 
Hi gabster,

As Well as JJR method using XML DOM, you can also create a XML File by concatenating a string and saving this string as a XML File. Simplified example:-

Dim sXML As String

sXML = &quot;<ROOT>&quot;
sXML = sXML & &quot;<TextField>&quot; & XMLFormat(ValueofTextField)& &quot;</TextField>&quot;
sXML = &quot;</ROOT>&quot;

Where XMLFormat is a User Defined Function which removes Illegal and Restricted XML characters which may be typed in by the user.

Then use FileSystemObject to Write sXML to a file or (Create file) with a .xml extension. E.g.

Dim oFSO As FileSystemObject
Dim oTS As TextStream
Dim sFile As String

'File Name
sFile = C:\Temp\Test.xml)

'Instantiate FSO Objects
Set oFSO = New FileSystemObject
Set oTS = oFSO.CreateTextFile(sFile)

'Write to Text File
oTS.WriteLine sXML
oTS.Close

'Release FSO Objects
Set oTS = Nothing
Set oFSO = Nothing


HTH,

Codefish



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top