4D v13.4

DOM Create XML element

Home

 
4D v13.4
DOM Create XML element

DOM Create XML element 


 

DOM Create XML element ( elementRef ; xPath {; attribName ; attrValue} {; attribName2 ; attrValue2 ; ... ; attribNameN ; attrValueN} ) -> Function result 
Parameter Type   Description
elementRef  String in Root XML element reference
xPath  Text in XPath path of the XML element to create
attribName  String in Attribute to set
attrValue  String, Boolean, Longint, Real, Time, Date in New attribute value
Function result  String in Reference of the created XML element

The DOM Create XML element command creates a new element in the XML element elementRef in the path set by the xPath parameter and adds attributes to it if necessary.

Pass the root element reference in elementRef (created, for example, using the DOM Create XML Ref command).

In xPath, pass the access path in XPath notation of the element to create (see the “Use of XPath notation” paragraph in the Overview of XML DOM Commands section). If any path elements do not exist, they are created.

It is possible to pass a simple item name directly in the xPath parameter in order to create a sub-item from the current item (see example 3).

Note: If you have defined one or more namespaces for the tree set using elementRef (see the DOM Create XML Ref command), you must precede the xPath parameter with the namespace to be used (for example, “MyNameSpace:MyElement”).

You can pass attribute/attribute value pairs (in the form of variables, fields or literal values) in the optional attrName and attrValue parameters. You can pass as many pairs as you want.

The attrValue parameter can be of the text type or another type (Boolean, integer, real, date or time). If you pass a value other than text, 4D handles its conversion to text, according to the following principles:

TypeExample of converted value
Boolean"true" or "false"
Integer"123456"
Real"12.34" (the decimal separator is always ".")
Date"2006-12-04T00:00:00Z" (RFC 3339 standard)
Time"5233" (number of seconds)

The command returns the XML reference of the element created as a result.

We want to create the following element:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<RootElement>
   <Elem1>
      <Elem2>
         <Elem3> </Elem3>
         <Elem3> </Elem3>
      </Elem2>
   </Elem1>
</RootElement>

To do so, simply write:

 C_TEXT(vRootRef;vElemRef)
 vRootRef:=DOM Create XML Ref("RootElement")
 vxPath:="/RootElement/Elem1/Elem2/Elem3"
 vElemRef:=DOM Create XML element(vRootRef;vxPath)
 vxPath:="/RootElement/Elem1/Elem2/Elem3[2]"
 vElemRef:=DOM Create XML element(vRootRef;vxPath)

We want to create the following element (containing attributes):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<RootElement>
   <Elem1>
      <Elem2>
         <Elem3 Font=Verdana Size=10> </Elem3>
      </Elem2>
   </Elem1>
</RootElement>

To do so, simply write:

 C_TEXT(vRootRef;vElemRef)
 C_TEXT($aAttrName1;$aAttrName2;$aAttrVal1;$aAttrVal2)
 $aAttrName1:="Font"
 $aAttrName2:="Size"
 $aAttrVal1:="Verdana"
 $aAttrVal2:="10"
 
 vRootRef:=DOM Create XML Ref("RootElement")
 vxPath:="/RootElement/Elem1/Elem2/Elem3"
 vElemRef:=DOM Create XML element(vRootRef;vxPath;$aAttrName1;$aAttrVal1;$aAttrName2;$aAttrVal2)

We want to create and export the following structure:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Root>
   <Elem1>Hello</Elem1>
</Root>

We want to use the syntax based on a simple item name. To do this, simply write:

 C_TEXT($root)
 C_TEXT($ref1)
 
 $root:=DOM Create XML Ref("Root")
 $ref1:=DOM Create XML element($root;"Elem1")
 DOM SET XML ELEMENT VALUE($ref1;"Hello")
 DOM EXPORT TO FILE($root;"mydoc.xml")
 DOM CLOSE XML($root)

If the command was executed correctly, the system variable OK is set to 1. Otherwise, it is set to 0 and an error is generated.

An error is generated when:

  • The root element reference is invalid.
  • The name of the element to create is invalid (for example, if it starts with a number).

 
PROPERTIES 

Product: 4D
Theme: XML DOM
Number: 865

The OK variable is changed by the commandThis command modifies the Error system variable

 
INDEX

Alphabetical list of commands

 
HISTORY 

Modified: 4D v11 SQL

 
SEE ALSO 

DOM Create XML element arrays
DOM Get XML element
DOM REMOVE XML ELEMENT