DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoXml
Boolean iBFound
Integer iNumChildren
Integer i
String sTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Get ComLoadXmlFile Of hoXml "qa_data/xml/sample1.xml" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoXml To sTemp1
Showln sTemp1
Procedure_Return
End
// Navigate to the "books" element:
Get ComFindChild2 Of hoXml "books" To iBFound
If (iBFound = False) Begin
Showln "No books child found!"
Procedure_Return
End
Get ComNumChildren Of hoXml To iNumChildren
Move 0 To i
While (i < iNumChildren)
// Navigate to the Nth book
Get ComGetChild2 Of hoXml i To iSuccess
// Display the book title:
Get ComGetAttrValue Of hoXml "title" To sTemp1
Showln sTemp1
// Add a new integer attribute:
// Should never fail..
Get ComAddAttributeInt Of hoXml "bookId" i To iSuccess
// Add a new unread="yes" attribute:
Get ComAddAttribute Of hoXml "unread" "yes" To iSuccess
// Navigate back up to the parent:
Get ComGetParent2 Of hoXml To iSuccess
Move (i + 1) To i
Loop
// Navigate back to the document root:
Send ComGetRoot2 To hoXml
// Save the updated document:
Get ComSaveXml Of hoXml "modified.xml" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoXml To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetXml Of hoXml To sTemp1
Showln sTemp1
End_Procedure