KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to receive all child references of an parent xml element
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: October 21, 2015

Below is an utility method that returnsan array of names, and an array of references to child elements within a parent xml element.

//Gets all child elements of the given parent element
//$1 - parent xml element reference
//$2 - pointer to array to contain child names
//$3 - pointer to array to contain child elements (TODO)

C_TEXT($parentRef;$1)
C_POINTER($2;$arrChildren)
C_POINTER($3;$arrRefs)
C_TEXT($parentName;$firstName;$firstRef;$nextName;$nextRef)
$parentRef:=$1
$arrChildren:=$2
$arrRefs:=$3
If(Count parameters>=3)
  DOM GET XML ELEMENT NAME($parentRef;$parentName)
  $firstRef:=DOM Get first child XML element($parentRef)
  If (ok=1)
     DOM GET XML ELEMENT NAME($firstRef;$firstName)
     APPEND TO ARRAY($arrChildren->;$firstName)
     APPEND TO ARRAY($arrRefs->;$firstRef)
     $nextRef:=$firstRef
     While (ok=1)
        $nextRef:=DOM Get next sibling XML element($nextRef)
        If (ok=1)
           DOM GET XML ELEMENT NAME($nextRef;$nextName)
           APPEND TO ARRAY($arrChildren->;$nextName)
           APPEND TO ARRAY($arrRefs->;$nextRef)
         End if
      End while
   End if
End if


Example:

Assume the following xml structure below:


Running the code below will obtain the parent element's (Parent) children.
ARRAY TEXT($arrChildren;0)
ARRAY TEXT($arrRefs;0)
DOM_GET_CHILDREN($parentRef;->$arrChildren;->$arrRefs)
//$parentRef is reference to the parent xml element

The arrays will contain the following: