Tech Tip: Where to begin with DOM XML commands
PRODUCT: 4D | VERSION: 2004.2 | PLATFORM: Mac & Win
Published On: July 29, 2005
The following example method demonstrates how to:
- Create a XML root path
- Create or find an element
- Fill the element's value
- Allow modification of the element's value through a user interface
The resulting file created will look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<MyRoot>
<myFirstChild FirstChildAttr="FirstChildAttrValue">FirstChildValue</myFirstChild>
</MyRoot>
``````````````````````````````````````````````````````````````````````
C_STRING(16;$xmlRef;$xmlElementRef)
C_TIME($docRef)
C_TEXT($documentPath;$ChildValue)
` does the XML document already exist?
If (Test path name("mydocument.txt")=1)
$xmlRef:=DOM Parse XML source("mydocument.txt") ` it does so parse its elements and get a reference
$documentPath:="mydocument.txt"
Else
$docRef:=Create document("mydocument.txt") ` it doesn't so create a document for storage later
CLOSE DOCUMENT($docRef)
$xmlRef:=DOM Create XML Ref("MyRoot") ` start a XML root path (within 4D only)
$documentPath:=document
End if
`get a reference to the element within the XML root path
$xmlElementRef:=DOM Find XML element($xmlRef;"/MyRoot/myFirstChild") ` find a predetermined XML element
If (OK=1)
` element exists so get its value
DOM GET XML ELEMENT VALUE($xmlElementRef;$ChildValue)
Else
` element doesn't exist, so create it, and return a reference to it
$xmlElementRef:=DOM Create XML element($xmlRef;"/MyRoot/myFirstChild";"FirstChildAttr";"FirstChildAttrValue")
$childValue:="FirstChildValue"
DOM SET XML ELEMENT VALUE($xmlElementRef;$ChildValue) ` store a value for the element
End if
` change the element's value
CONFIRM("Would you like to change the first child value?\nCurrent value is: "+$childValue;"Yes";"No")
If (OK=1)
$childValue:=Request("Please enter the new child value";$childValue;"OK";"Cancel")
DOM SET XML ELEMENT VALUE($xmlElementRef;$childValue) ` change the element's value
End if
DOM EXPORT TO FILE($xmlRef;$documentPath) ` store into the file
DOM CLOSE XML($xmlRef) ` close the reference
``````````````````````````````````````````````````````````````````````