-
Nov 24th, 2021, 02:02 PM
#1
Thread Starter
Member
Create xml file in vb6.0 like the following
I am new in xml
Can someone help me to write the code in vb6.0 to create an xml file like the following?
<alfa>
<a1>101</a1>
<a2>102</a2>
<Beta>
<b1>201</b1>
<b2>202</b2>
<Gamma>
<g1>301</g1>
<g2>302</g2>
<g3>303</g3>
</Gamma>
</Beta>
</alfa>
-
Nov 24th, 2021, 02:40 PM
#2
Re: Create xml file in vb6.0 like the following
Shooting from the hip here... this should get you going in the right direction:
Add the appropriate references...
Code:
Dim xDoc as XMLDocument
Dim aNode as XMLNode
Dim anotherNode as XMLNode
seet xDoc = new XMLDocument
set aNode = new XMLNode("Node1")
set anotherNode = new XMLNode("Node2")
anotherNode.value = "Some value"
aNode.Children.Add(anotherNode)
xmlDoc.RootNode - aNode
xmlDoc.WriteFile("C:/myXML.xml")
I think that's right... don't have VB6 handy... and it's been a while... but that shouild be close at the very least.
-
Nov 24th, 2021, 03:36 PM
#3
Re: Create xml file in vb6.0 like the following
This
Code:
Option Explicit
Private Sub Form_Load()
Dim oDoc As Object
Set oDoc = CreateObject("MSXML2.DOMDocument")
With oDoc.appendChild(oDoc.createElement("alfa"))
.appendChild(oDoc.createElement("a1")).nodeTypedValue = 101
.appendChild(oDoc.createElement("a2")).nodeTypedValue = 102
With .appendChild(oDoc.createElement("Beta"))
.appendChild(oDoc.createElement("b1")).nodeTypedValue = 201
.appendChild(oDoc.createElement("b2")).nodeTypedValue = 202
With .appendChild(oDoc.createElement("Gamma"))
.setAttribute "id", "somethingDifferent"
.appendChild(oDoc.createElement("g1")).nodeTypedValue = 301
.appendChild(oDoc.createElement("g2")).nodeTypedValue = 301
.appendChild(oDoc.createElement("g2")).nodeTypedValue = 301
End With
End With
End With
Debug.Print oDoc.xml
Debug.Print FormatXmlIndent(oDoc)
End Sub
Private Function FormatXmlIndent(vDomOrString As Variant) As String
Dim oWriter As Object ' MSXML2.MXXMLWriter
On Error GoTo QH
Set oWriter = CreateObject("MSXML2.MXXMLWriter")
oWriter.omitXMLDeclaration = True
oWriter.indent = True
With CreateObject("MSXML2.SAXXMLReader")
Set .contentHandler = oWriter
'--- keep CDATA elements
.putProperty "http://xml.org/sax/properties/lexical-handler", oWriter
.putProperty "http://xml.org/sax/properties/declaration-handler", oWriter
.parse vDomOrString
End With
FormatXmlIndent = oWriter.output
QH:
End Function
. . . prints this
Code:
<alfa>
<a1>101</a1>
<a2>102</a2>
<Beta>
<b1>201</b1>
<b2>202</b2>
<Gamma id="somethingDifferent">
<g1>301</g1>
<g2>301</g2>
<g2>301</g2>
</Gamma>
</Beta>
</alfa>
cheers,
</wqw>
-
Nov 25th, 2021, 12:32 AM
#4
Re: Create xml file in vb6.0 like the following
Why not just write the stuff as ANSI text and call it good enough? If you must create UTF-8 then use an ADO.Stream object. This looks like some simple-minded XML. Why use the high-overhead scripting crutch of the MSXML DOM for this?
There are numerous possible approaches, but without knowing a lot more about your requirements recommending one of them is difficult. For example when the XML documents to create are really huge you'll be way ahead to use MSXML SAX2 instead.
Don't just say "How do I get to Virginny?" because people will suggest anything from a clown tricycle to a Mack truck.
-
Nov 25th, 2021, 01:37 AM
#5
Re: Create xml file in vb6.0 like the following
IMO these forums have way too little sample code on MSXML2.DOMDocument which uses basic createElement + appendChild with sporadic setAttribute calls to create 99.99% of the XML ever needed in the wild.
There is more SAX talking than nested With appendChild ... End With samples. Go figure!
cheers,
</wqw>
-
Nov 25th, 2021, 10:06 AM
#6
Re: Create xml file in vb6.0 like the following
Really? To me it feels like few are even aware of SAX, let alone have a clue how to use it in a real program for either parsing or generating XML. DOM code is littered everywhere, so discussions of SAX use are rarer and more valuable.
But generating XML using DOM methods may well be fairly rare too. We more often see passive consumption code using DOM with XPath queries, probably because (A.) historically there were far more VBScript snippets to copy/paste for that than anything else, and (B.) most programmers only deal with XML when it is imposed from outside as in a REST response.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|