|
-
August 14th, 2001, 02:29 PM
#1
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?
-
August 14th, 2001, 07:00 PM
#2
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
-
August 16th, 2001, 08:19 AM
#3
Re: Create XML File with XML Parser
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
|