CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2000
    Posts
    149

    Create XML File with XML Parser

    Hello,

    I'm having trouble adding elements to my XML file when it doesn't already exist. If I load an existing XML file, I can access the nodes and their values, but I want to create the nodes and values...

    Can someone show me some code please?


  2. #2
    Join Date
    Mar 2000
    Posts
    145

    Re: Create XML File with XML Parser


    option Explicit

    'create some nodes
    private Sub Command1_Click()
    Dim objDom as DOMDocument
    Dim nodeRoot as IXMLDOMNode
    Dim nodeWorking as IXMLDOMNode
    Dim i as Integer

    set objDom = new DOMDocument

    set nodeRoot = objDom.createNode(NODE_ELEMENT, "Form1", "")
    objDom.appendChild nodeRoot

    set nodeRoot = objDom.selectSingleNode("/Form1")
    set nodeWorking = objDom.createNode(NODE_ELEMENT, "Controls", "")
    nodeRoot.appendChild nodeWorking

    'add info for 2 controls
    for i = 1 to 2
    set nodeRoot = objDom.selectSingleNode("/Form1/Controls")
    'create and add a Control node
    CreateChild nodeRoot, "Control" & i
    'select node just added
    set nodeRoot = objDom.selectSingleNode("/Form1/Controls/Control" & i)
    'and add some elements
    CreateChild nodeRoot, "caption", false
    CreateChild nodeRoot, "name", false
    CreateChild nodeRoot, "tag", false
    next i

    MsgBox objDom.xml
    End Sub

    'use the AddAttribute function
    private Sub Command2_Click()
    Dim dom as DOMDocument
    Dim node as IXMLDOMNode

    set dom = new DOMDocument
    dom.loadXML "<xml><node>Attribute me</node><node color=""red"">Select 2</node></xml>"
    set node = dom.selectSingleNode("//node")
    AddAttribute node, "color", "blue"
    Debug.print dom.xml
    set node = nothing
    set dom = nothing
    End Sub

    private Sub CreateChild(byref rNode as IXMLDOMNode, _
    byval NodeName as string, optional byval MoveChild as Boolean = false)

    Dim nodeWorking as IXMLDOMNode

    set nodeWorking = rNode.ownerDocument.createNode(NODE_ELEMENT, NodeName, "")
    rNode.appendChild nodeWorking
    If MoveChild = true then
    set rNode = rNode.selectSingleNode("./" & NodeName)
    End If
    set nodeWorking = nothing
    End Sub

    public Function AddAttribute(byref INode as IXMLDOMNode, _
    byval aName as string, byval aValue as string) as IXMLDOMNode

    Dim attrCreate as IXMLDOMAttribute

    set attrCreate = INode.ownerDocument.createAttribute(aName)
    ' Create a node using the parent document.
    attrCreate.Value = aValue
    'Assign the value
    Call INode.Attributes.setNamedItem(attrCreate)
    'Add it to the INode
    'return the Node
    set AddAttribute = INode
    'clear our reference
    set attrCreate = nothing
    End Function






  3. #3
    Join Date
    Feb 2000
    Posts
    149

    Re: Create XML File with XML Parser

    Thanks for the reply!


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

Featured