Results 1 to 2 of 2

Thread: Write XML File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2002
    Location
    India
    Posts
    105

    Write XML File

    Can anyone tell how to write an xml file through VB.
    Shahab Ahmed

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    This is just a quick example....

    But includes all the basic objects, elements, attributes, comment, and Processing Instruction. This example could be adapted to include a loop to create multiple Person elements using an array or something similar.

    Enjoy!
    VB Code:
    1. Public Sub XMLwriteDemo()
    2.   '=== XML document objects
    3.   Dim objXMLdoc           As Object
    4.   Dim objXMLpi            As Object
    5.   Dim objComment          As Object
    6.   Dim objXMLroot          As Object
    7.   Dim objXMLchild         As Object
    8.   Dim objXMLtext          As Object
    9.   '=== Create document
    10.   Set objXMLdoc = CreateXMLDoc
    11.   '=== Create processing instruction so file can be parsed
    12.   Set objXMLpi = objXMLdoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
    13.   objXMLdoc.appendChild objXMLpi
    14.   '== Add a comment
    15.   Set objComment = objXMLdoc.createComment("Example by Jerry Grant - [url]www.jg-design.net[/url] " & Now)
    16.   objXMLdoc.appendChild objComment
    17.   '=== Create the main Root
    18.   Set objXMLroot = objXMLdoc.createElement("Addresses")
    19.   objXMLdoc.appendChild objXMLroot
    20.   '=== Add an element 'Person'
    21.   Set objXMLchild = objXMLdoc.createNode("element", "Person", "")
    22.   '=== Give it an 'id' attribute
    23.   objXMLchild.setAttribute "id", 1
    24.   objXMLdoc.documentElement.appendChild objXMLchild
    25.   '=== Add a Surname element
    26.   Set objXMLtext = objXMLdoc.createNode("element", "Surname", "")
    27.   objXMLtext.Text = "Grant"
    28.   objXMLchild.appendChild objXMLtext
    29.   '=== Add a Forename element
    30.   Set objXMLtext = objXMLdoc.createNode("element", "Forename", "")
    31.   objXMLtext.Text = "Jerry"
    32.   objXMLchild.appendChild objXMLtext
    33.   '=== Add a Link element
    34.   Set objXMLtext = objXMLdoc.createNode("element", "Link", "")
    35.   objXMLtext.Text = "http://www.jg-design.net"
    36.   objXMLchild.appendChild objXMLtext
    37.   '=== Save as a file
    38.   objXMLdoc.Save App.Path & "\JG-Design.xml"
    39.   '=== Destroy objects
    40.   Set objXMLtext = Nothing
    41.   Set objXMLchild = Nothing
    42.   Set objXMLroot = Nothing
    43.   Set objComment = Nothing
    44.   Set objXMLpi = Nothing
    45.   Set objXMLdoc = Nothing
    46. End Sub
    47.  
    48. Public Function CreateXMLDoc() As Object
    49.   '=== Late bound example
    50.   Set CreateXMLDoc = CreateObject("Msxml2.DOMDocument")
    51.   '=== Uncomment for early bound example
    52.   'Set CreateXMLDoc = New MSXML2.DOMDocument
    53.   With CreateXMLDoc
    54.     .preserveWhiteSpace = False
    55.     .resolveExternals = False
    56.     .validateOnParse = False
    57.     .async = False
    58.   End With
    59. End Function
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width