KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Dom Create XML element creation from root vs child reference
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: March 16, 2016

When creating an XML element using the DOM create XML element command, it is important to understand where the reference will be created from. If the reference is choosen to be at the root of the XML, 4D will start at the top of the root and scan to the designated location that will create the XML element. As the XML size increases, scanning from the root will take some time especially if the XML structure is complex.

Here is an example of creating XML element from the root in code as well as a depiction:

C_TEXT($ElemRoot;$elem1;$elem2;$path)
C_LONGINT($i;$j)

$ElemRoot:=DOM Create XML Ref("RootElement")

For ($j;1;5)
   $path:="/RootElement/Set"+String($j)
   $elem1:=DOM Create XML element($ElemRoot;$path)
   For ($i;1;5)
      $elem2:=DOM Create XML element($elem1;"A"+String($i))
      DOM SET XML ELEMENT VALUE($elem2;"data")
   End for
End for

DOM EXPORT TO FILE($ElemRoot;"test.xml")
DOM CLOSE XML($ElemRoot)





When another "Set" is added (Green and Red arrows pointing), is starts from the root.


An efficient way to reference is by using the tag "Set" with the command DOM Append XML child node. Instead of starting from the root, using a reference of a known location will save time from scanning. Here is an example of creating XML element from the "Set" reference in code as well as a depiction:

C_TEXT($ElemRoot;$elem1;$elem2;$path)
C_LONGINT($i;$j)

$ElemRoot:=DOM Create XML Ref("RootElement")

For ($j;1;5)
   $path:="Set"+String($j)
   $elem1:=DOM Append XML child node($ElemRoot;XML ELEMENT;$path)

   For ($i;1;5)
      $elem2:=DOM Create XML element($elem1;"A"+String($i))
      DOM SET XML ELEMENT VALUE($elem2;"data")
   End for
End for

DOM EXPORT TO FILE($ElemRoot;"test.xml")
DOM CLOSE XML($ElemRoot)