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
// XmlMgmt	Codeunit	XML DOM Management	
// xmlDoc	Automation	'Microsoft XML, v3.0'.DOMDocument60	
// rootNode	Automation	'Microsoft XML, v3.0'.IXMLDOMNode	
// childNode	Automation	'Microsoft XML, v3.0'.IXMLDOMNode	
// address	Text		30
// prio	Integer		
// createdAt	Date		
CREATE(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 1
GetAttributeValueAsText(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Text
// attributeNodeLoc | Automation | 'Microsoft XML, v3.0'.IXMLDOMNode
SetNormalCase;
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 | Integer
attrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);
IF attrValLoc <> '' THEN
  IF NOT EVALUATE(returnValueLoc, attrValLoc) THEN
    EXIT(-1);
EXIT(returnValueLoc);

GetAttributeValueAsDec(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Decimal
// attrValLoc | Text
// returnValueLoc | Decimal
attrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);
IF attrValLoc <> '' THEN
  IF NOT EVALUATE(returnValueLoc, attrValLoc) THEN
    EXIT(-1);
EXIT(returnValueLoc);

GetAttributeValueAsDate(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode";attributeNamePar : Text[100]) : Date
// attrValLoc | Text
// returnValueLoc | Date
attrValLoc := 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 | Boolean
attrValLoc := GetAttributeValueAsText(xmlNodePar,attributeNamePar);
returnValueLoc := FALSE;
IF attrValLoc <> '' THEN BEGIN
  IF NOT EVALUATE(returnValueLoc, attrValLoc) THEN
    ERROR('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 1
GetNodeValueAsText(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 value
GetNodeValueAsInt(xmlNodePar : Automation "'Microsoft XML, v3.0'.IXMLDOMNode") : Integer
// attrValLoc | Text
// returnValueLoc | Integer
xmlNodeValueLoc := GetNodeValueAsText(xmlNodePar);
returnValueLoc := 0;
IF xmlNodeValueLoc <> '' THEN
  IF NOT EVALUATE(returnValueLoc, xmlNodeValueLoc) THEN
    ERROR('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.

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s