Sample code for 30+ languages & platforms
Xbase++

AddAttribute - Insert New Attribute in an XML Node

Demonstrates adding an name=value attribute to an XML element.

This example uses the XML sample file here: sample1.xml. The sample1.xml file contains this content:

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXml
LOCAL nBFound
LOCAL nNumChildren
LOCAL i

nSuccess := 0

oXml := CreateObject("Chilkat.Xml")

nSuccess := oXml:LoadXmlFile("qa_data/xml/sample1.xml")
IF (nSuccess != 1)
    ? oXml:LastErrorText
    oXml:destroy()
    RETURN
ENDIF

//  Navigate to the "books" element:
nBFound := oXml:FindChild2("books")
IF (nBFound == 0)
    ? "No books child found!"
    oXml:destroy()
    RETURN
ENDIF

nNumChildren := oXml:NumChildren

i := 0
DO WHILE i < nNumChildren

    //  Navigate to the Nth book 
    oXml:GetChild2(i)

    //  Display the book title:
    ? oXml:GetAttrValue("title")

    //  Add a new integer attribute:
    //  Should never fail..
    oXml:AddAttributeInt("bookId", i)

    //  Add a new unread="yes" attribute:
    oXml:AddAttribute("unread", "yes")

    //  Navigate back up to the parent:
    oXml:GetParent2()

    i := i + 1
ENDDO

//  Navigate back to the document root:
oXml:GetRoot2()

//  Save the updated document:
nSuccess := oXml:SaveXml("modified.xml")
IF (nSuccess != 1)
    ? oXml:LastErrorText
    oXml:destroy()
    RETURN
ENDIF

? oXml:GetXml()

oXml:destroy()