Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Xml
integer li_BFound
integer li_NumChildren
integer i

li_Success = 0

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Xml.LoadXmlFile("qa_data/xml/sample1.xml")
if li_Success <> 1 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Xml
    return
end if

// Navigate to the "books" element:
li_BFound = loo_Xml.FindChild2("books")
if li_BFound = 0 then
    Write-Debug "No books child found!"
    destroy loo_Xml
    return
end if

li_NumChildren = loo_Xml.NumChildren

i = 0
do while i < li_NumChildren

    // Navigate to the Nth book 
    loo_Xml.GetChild2(i)

    // Display the book title:
    Write-Debug loo_Xml.GetAttrValue("title")

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

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

    // Navigate back up to the parent:
    loo_Xml.GetParent2()

    i = i + 1
loop

// Navigate back to the document root:
loo_Xml.GetRoot2()

// Save the updated document:
li_Success = loo_Xml.SaveXml("modified.xml")
if li_Success <> 1 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Xml
    return
end if

Write-Debug loo_Xml.GetXml()


destroy loo_Xml