Store info in registry, INI or XML

Discussion in 'AutoCAD' started by Dave F., May 17, 2004.

  1. Dave F.

    Dave F. Guest

    Hi

    I've been using the registry to store/retrieve info between Acad sessions.
    But searching this NG there seems to be a movement against this. (Hello, Mr.
    Tuersley).

    Instead, so I read, I should be using ini files via win32api or XML.

    What are the reasons for this? Faster/slower, easier?

    Are there any examples to get me going?
    I've used the API a couple of times, but XML is new to me.

    Any help will be greatfully received

    TIA
    Dave F.
     
    Dave F., May 17, 2004
    #1
  2. Hello Dave :)

    Let's set the record straight, I'm not against using the registry.

    The registry should be used for storing a value that will almost never
    change such as the location of your backend database.

    The registry shoud never be used to store revolving data such as user
    options for your program.

    The reasoning is simple - the registry is already too large, too slow and
    dependent upon the logged in user's rights as to which sections can be
    read/written.

    Of the two INIs are the least favorite but usually the easiest for
    programmers to integrate with. XML is the better choice but not the easiest
    to pickup and run with. I'm out of the office today, but tomorrow I will
    post an XML exmple for you. Until then, there have been some examples
    already posted here.

    Mike
     
    Mike Tuersley, May 17, 2004
    #2
  3. Dave F.

    David Urban Guest

    Dave

    I have slogged through saving my data to an XML it wasn't very easy to
    learn about it but once I figured it out it is very easy to read and
    navigate the file. there was no help in the VBA system so you have to
    go to the MS site to find it and it is not geared for VBA so it was a
    bit of work to figure it out. What helped me is to find the XML notepad
    program which I set up a dummy file to help me figure out how I wanted
    to format my data and then try and program it. here is a small snip it
    of my code. it is used to save and retrieve my UDT which is used in
    multiple drawings.

    Option Explicit
    Dim oDoc As DOMDocument

    Dim oAlg As IXMLDOMElement
    Dim oSta As IXMLDOMElement
    Dim oLat1 As IXMLDOMElement
    Dim oLat2 As IXMLDOMElement
    Dim oRoot As IXMLDOMElement
    Dim oStation As IXMLDOMAttribute
    Public Sub Save_toXML()

    Set oDoc = Nothing

    Set oDoc = New DOMDocument
    oDoc.async = False

    On Error Resume Next
    oDoc.Load (fXMLFile)



    Set oRoot = oDoc.documentElement
    'If Err.Number <> 0 Then
    oDoc.resolveExternals = True
    If oDoc.parseError <> 0 Then
    ' Create processing instruction and document root
    Set oAlg = oDoc.createProcessingInstruction("xml", "version='1.0'")
    Set oAlg = oDoc.insertBefore(oAlg, oDoc.childNodes.Item(0))

    ' Create document root
    Set oRoot = oDoc.createElement("Project_" & CStr(Project))
    Set oDoc.documentElement = oRoot
    ' oRoot.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"

    'Else
    'Set oRoot = oDoc.documentElement
    End If
    Err.Clear
    Dim events As Variant, alignname As String
    Dim x As Integer, name As String, name2 As String
    name = Replace(algname, " ", "_")
    alignname = "//Project_" & CStr(Project) & "//" & name
    Set oAlg = oDoc.selectSingleNode(alignname)
    name2 = oAlg.baseName
    If name2 <> "" Then
    For x = oAlg.childNodes.length - 1 To 0 Step -1
    Set oSta = oAlg.childNodes.Item(x)
    If Left(oSta.baseName, 3) = "Sta" Then
    oAlg.removeChild oSta
    End If
    Next x
    Else
    Set oAlg = oDoc.createElement(name)
    oRoot.appendChild oAlg
    End If
    oAlg.setAttribute "ALGNUMBER", align.Number

    For x = LBound(AlignPt, 1) To UBound(AlignPt, 1)
    Set oSta = oDoc.createElement("Sta" & Format(AlignPt(x).station,
    "0.00"))
    oAlg.appendChild oSta
    Write_XMLStation oSta, oDoc, AlignPt(x)
    Next x



    ' Save xml file
    oDoc.Save fXMLFile
    Set oDoc = Nothing
    End Sub
    Public Function Load_fromXML() As Boolean
    Dim nodes As IXMLDOMNode

    Set oDoc = Nothing

    Set oDoc = New DOMDocument
    oDoc.async = False

    On Error Resume Next
    oDoc.Load (fXMLFile)
    'Set oRoot = oDoc.documentElement
    'If Err.Number <> 0 Then
    oDoc.resolveExternals = True
    If oDoc.parseError = 0 Then
    Dim alignname As String
    Dim x As Integer, name As String, name2 As String, y As Integer
    x = -1
    name = Replace(algname, " ", "_")
    alignname = "//Project_" & CStr(Project) & "//" & name
    Set oAlg = oDoc.selectSingleNode(alignname)
    Debug.Print Err.Number
    name2 = oAlg.baseName
    If name2 <> "" Then
    If oAlg.getAttribute("ALGNUMBER") = align.Number Then
    ReDim AlignPt(100)

    For y = oAlg.childNodes.length - 1 To 0 Step -1
    Set oSta = oAlg.childNodes.Item(y)
    If Left(oSta.baseName, 3) = "Sta" Then
    x = x + 1
    Read_XMLStation oSta, AlignPt(x)
    Load_fromXML = True
    End If
    Next y
    ReDim Preserve AlignPt(x)
    End If
    End If
    End If
    Set oDoc = Nothing

    End Function
    Private Sub Write_XMLStation(oSta As IXMLDOMElement, oDoc As
    DOMDocument, temp As junctions)
    Dim oLat1 As IXMLDOMElement, oLat2 As IXMLDOMElement
    oSta.setAttribute "Station", temp.station
    oSta.setAttribute "Event", temp.Event
    oSta.setAttribute "Direction", temp.direction
    oSta.setAttribute "DirectionUP", temp.DirectionUP
    oSta.setAttribute "Easting", temp.Easting
    oSta.setAttribute "Northing", temp.Northing
    oSta.setAttribute "Pipesize", temp.pipesize
    oSta.setAttribute "PipesizeUP", temp.PipesizeUP
    oSta.setAttribute "Flowline", temp.FlowLine
    Set oLat1 = oDoc.createElement("Lateral1")
    oSta.appendChild oLat1
    oLat1.setAttribute "name", temp.Lateral1.name
    oLat1.setAttribute "Station", temp.Lateral1.INTStation
    oLat1.setAttribute "count", temp.Lateral1.count
    oLat1.setAttribute "Direction", temp.Lateral1.direction
    oLat1.setAttribute "eEast", temp.Lateral1.EndEasting
    oLat1.setAttribute "eNorth", temp.Lateral1.EndNorthing
    oLat1.setAttribute "eSta", temp.Lateral1.EndStation
    Set oLat2 = oDoc.createElement("Lateral2")
    oSta.appendChild oLat2
    oLat2.setAttribute "name", temp.Lateral2.name
    oLat2.setAttribute "Station", temp.Lateral2.INTStation
    oLat2.setAttribute "count", temp.Lateral2.count
    oLat2.setAttribute "Direction", temp.Lateral2.direction
    oLat2.setAttribute "eEast", temp.Lateral2.EndEasting
    oLat2.setAttribute "eNorth", temp.Lateral2.EndNorthing
    oLat2.setAttribute "eSta", temp.Lateral2.EndStation

    End Sub
    Private Sub Read_XMLStation(oSta As IXMLDOMElement, temp As junctions)
    Dim oLat1 As IXMLDOMElement, oLat2 As IXMLDOMElement
    With temp
    .station = oSta.getAttribute("Station")
    .Event = oSta.getAttribute("Event")
    .direction = oSta.getAttribute("Direction")
    .DirectionUP = oSta.getAttribute("DirectionUP")
    .Easting = oSta.getAttribute("Easting")
    .Northing = oSta.getAttribute("Northing")
    .pipesize = oSta.getAttribute("Pipesize")
    .PipesizeUP = oSta.getAttribute("PipesizeUP")
    .FlowLine = oSta.getAttribute("Flowline")
    Set oLat1 = oSta.firstChild
    .Lateral1.name = oLat1.getAttribute("name")
    .Lateral1.INTStation = oLat1.getAttribute("Station")
    .Lateral1.count = oLat1.getAttribute("count")
    .Lateral1.direction = oLat1.getAttribute("Direction")
    .Lateral1.EndEasting = oLat1.getAttribute("eEast")
    .Lateral1.EndNorthing = oLat1.getAttribute("eNorth")
    .Lateral1.EndStation = oLat1.getAttribute("eSta")
    Set oLat2 = oSta.lastChild
    .Lateral2.name = oLat2.getAttribute("name")
    .Lateral2.INTStation = oLat2.getAttribute("Station")
    .Lateral2.count = oLat2.getAttribute("count")
    .Lateral2.direction = oLat2.getAttribute("Direction")
    .Lateral2.EndEasting = oLat2.getAttribute("eEast")
    .Lateral2.EndNorthing = oLat2.getAttribute("eNorth")
    .Lateral2.EndStation = oLat2.getAttribute("eSta")
    End With
    End Sub
     
    David Urban, May 17, 2004
    #3
  4. Dave F.

    akseidel Guest

    I store data between sessions in simple <macroname>.dat text files that
    follow a structure of <keyword:><the data> for each line. The keywords
    are typically three characters long so that a simple left$ can detect
    the data type and a right$ len minus can exctract the data. The method
    has always been instantaneous. A simple texteditor can be used to view
    or edit the file for trouble shooting. The same code can be used from
    project to project. On some occasions where data is lengthy and of
    indeterminate length a simple Begin: and End: works fine.

    AKS
     
    akseidel, May 18, 2004
    #4
  5. Here's a quick down-n-dirty example of using xml. It is pretty much the
    same as an ini at this point, but hopefully you can experiment and/or read
    up on the topic to see the greater value of it. If I get time. I may post
    something more detailed. For now;

    1. In NotePad, paste this text:

    <xml id="MyDefaults">
    <defaults xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <path dt:dt="string">C:\My Files\My Directory</path>
    <username dt:dt="string">Mike Tuersley</username>
    <lastaccess dt:dt="dateTime">2004-03-05</lastaccess>
    </defaults>
    </xml>

    2. Save the file as DEFAULTS.XML

    3. Open a vba module and paste in this code:

    Sub test()
    Dim xDoc As DOMDocument
    Dim xRoot As IXMLDOMElement
    Dim xNode As IXMLDOMElement
    Dim oChild As IXMLDOMElement
    Dim lCnt As Long
    Set xDoc = New DOMDocument
    xDoc.Load "C:\Defaults.xml"
    DoEvents
    Set xRoot = xDoc.documentElement
    For Each xNode In xRoot.childNodes
    If xNode.baseName = "defaults" Then
    For lCnt = 0 To xNode.childNodes.Length - 1
    Set oChild = xNode.childNodes.Item(lCnt)
    MsgBox oChild.baseName & ": " & oChild.text
    Next
    Exit For
    End If
    Next
    End Sub

    4. Add a reference to Microsoft XML [4.0 or better] and change the
    xDoc.Load line to point to the file you saved in step 2.

    In closing, notice how the xml file contains the data type so it is passing
    more than just values. This example does not go into the data types, but
    its not hard to figure out how to do it.

    Good luck,

    Mike
     
    Mike Tuersley, May 18, 2004
    #5
  6. Dave F.

    Dave F. Guest

    Thanks Mike & David, for that. I shall look into it

    Dave F.
     
    Dave F., May 20, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.