Tech Tip: Utility method to get all attributes in an xml element
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: November 5, 2015
Below is an utility method to get all the attributes of an xml element. The method returns two arrays. One for the names of the attributes, and the other for the values of the attributes.
//Method: XML_GET_ATTR //Returns attributes of xml reference in an object //$1 - xml reference //$2 - pointer to text array to contain attribute names //$3 - pointer to text array to contain attribute values C_TEXT($1;$xmlRef) C_POINTER($2;$arrNames) C_POINTER($3;$arrValues) C_TEXT($attrName;$attrVal) C_LONGINT($i) If (Count parameters>=3) $xmlRef:=$1 $arrNames:=$2 $arrValues:=$3 $numAttr:=DOM Count XML attributes($xmlRef) If ($numAttr>0) For ($i;1;$numAttr) DOM GET XML ATTRIBUTE BY INDEX($xmlRef;$i;$attrName;$attrVal) APPEND TO ARRAY($arrNames->;$attrName) APPEND TO ARRAY($arrValues->;$attrVal) End for End if End if |
Example:
To get the attributes of the following xml element below:
Run the following code; assuming $elemRef is the reference to the xml element:
ARRAY TEXT($arrNames;0) ARRAY TEXT($arrVals;0) XML_GET_ATTR ($elemRef;->$arrNames;->$arrVals) |
Once ran, the two $arrNames and $arrVals will contain the following values: