Extend CU 6224: Read xml node values and attributes

Xml documents can be created/processed via XmlPorts. An other opportunity is the usage of Codeunit 6224. It’s used for complex xml handling. This CU only delivers some create and find functions, but no read/get functions. Following functions extend CU 6224 with read functions.

First a sample for the usage of the new functions. This sample shows, how to load a xml document given as string and read some of the node attributes.

// local variables// XmlMgmtCodeunitXML DOM Management// xmlDocAutomation'Microsoft XML, v3.0'.DOMDocument60// rootNodeAutomation'Microsoft XML, v3.0'.IXMLDOMNode// childNodeAutomation'Microsoft XML, v3.0'.IXMLDOMNode// addressText30// prioInteger// createdAtDateCREATE(xmlDoc);xmlDoc.loadXML('<mail priority="1" createdAt="26/5/2014"><from address="from@test.com"/>' +'<to name="to@test.com"/><state value="1"/></mail>');rootNode := xmlDoc.selectSingleNode('mail');prio := XmlMgmt.GetAttributeValueAsInt(rootNode,'priority');createdAt := XmlMgmt.GetAttributeValueAsDate(rootNode,'createdAt');childNode := rootNode.firstChild;address := XmlMgmt.GetAttributeValueAsText(childNode,'address');MESSAGE('mail: ' + FORMAT(prio) + ',' + FORMAT(createdAt) + ',' + address);CLEAR(rootNode);CLEAR(childNode);CLEAR(xmlDoc);

The new functions:

// Read an attribute value from a given node, e.g. from <data att1="1"/> gets value 1GetAttributeValueAsText(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Text// attributeNodeLoc | Automation | 'Microsoft XML, v3.0'.IXMLDOMNodeSetNormalCase;IF ISCLEAR(xmlNodePar) THEN  EXIT('Param. xmlNodePar must not be null');IF attributeNamePar = '' THEN  EXIT('Param. attributeNamePar must not be empty');attributeNodeLoc := xmlNodePar.attributes.getNamedItem(attributeNamePar);IF ISCLEAR(attributeNodeLoc) THEN  EXIT('Attribute ' + attributeNamePar + ' not found');IF STRLEN(attributeNodeLoc.text) > 0 THEN  EXIT(attributeNodeLoc.text);EXIT('');GetAttributeValueAsInt(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Integer// attrValLoc | Text// returnValueLoc | IntegerattrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);IF attrValLoc <> '' THEN  IF NOT EVALUATE(returnValueLoc, attrValLoc) THENEXIT(-1);EXIT(returnValueLoc);GetAttributeValueAsDec(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Decimal// attrValLoc | Text// returnValueLoc | DecimalattrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);IF attrValLoc <> '' THEN  IF NOT EVALUATE(returnValueLoc, attrValLoc) THENEXIT(-1);EXIT(returnValueLoc);GetAttributeValueAsDate(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Date// attrValLoc | Text// returnValueLoc | DateattrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);returnValueLoc := 0D;IF attrValLoc <> '' THEN  IF NOT EVALUATE(returnValueLoc, attrValLoc) THEN;EXIT(returnValueLoc);GetAttributeValueAsBool(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Boolean// attrValLoc | Text// returnValueLoc | BooleanattrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);returnValueLoc := FALSE;IF attrValLoc <> '' THEN BEGIN  IF NOT EVALUATE(returnValueLoc, attrValLoc) THENERROR('No bool value given');  EXIT(FALSE);END;EXIT(returnValueLoc);// Following is needed to read a node value, e.g. from <data>1</data> gets value 1GetNodeValueAsText(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode") : Text[1024]IF ISCLEAR(xmlNodePar) THEN  EXIT('Param. xmlNode must not be null');IF STRLEN(xmlNodePar.text) > 0 THEN  EXIT(xmlNodePar.text);EXIT('');// Return a node value as Integer valueGetNodeValueAsInt(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode") : Integer// attrValLoc | Text// returnValueLoc | IntegerxmlNodeValueLoc := GetNodeValueAsText(xmlNodePar);returnValueLoc := 0;IF xmlNodeValueLoc <> '' THEN  IF NOT EVALUATE(returnValueLoc, xmlNodeValueLoc) THENERROR('No int value given');EXIT(returnValueLoc);

Instead of Xml vs. 3 you can also use Xml vs. 6, e.g. xmlNode : ‘Microsoft XML, v6.0’.IXMLDOMNode.

One thought on “Extend CU 6224: Read xml node values and attributes

Leave a comment