Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an element to an XML file

Status
Not open for further replies.

cyclegeek

Programmer
Sep 4, 2001
62
US
I have an xml file e.g.:
<guest_list>
<guest>
<name>x</name>
<number>1</number>
</guest>
<guest>
<name>y</name>
<number>2</number>
</guest>
</guest_list>

How can I append an element into my guest_list.xml file.

Currently using asp with vb and javascript to gather and validate data.

Many thanks in advance.

Cheers,
cyclegeek

 
Using VB you can load xml in a msxmlDomDocument.
This object provides you with lots of functions to query and manipulate the xml. For example:
Code:
Public Sub AppendGuest(ByRef strXML As String, ByVal strName As String)

    Dim objDocument As New MSXML2.DOMDocument40
    Dim objNumber As MSXML2.IXMLDOMNode
    Dim objNumbers As MSXML2.IXMLDOMNodeList
    Dim objNewGuest As MSXML2.IXMLDOMNode
    Dim intID As Integer
    
    objDocument.loadXML strXML
    
    intID = 1
    Set objNumbers = objDocument.selectNodes(&quot;//number&quot;)
    For Each objNumber In objNumbers
        If CInt(objNumber.Text) >= intID Then
            intID = objNumber.Text + 1
        End If
    Next
    
    Set objNewGuest = objDocument.createElement(&quot;guest&quot;)
    objNewGuest.appendChild(objDocument.createElement(&quot;number&quot;)).Text = CStr(intID)
    objNewGuest.appendChild(objDocument.createElement(&quot;name&quot;)).Text = strName
    objDocument.selectSingleNode(&quot;guest_list&quot;).appendChild objNewGuest
    
    strXML = objDocument.xml

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top