Example objects for this post can be downloaded here.
The GDL XML Add-On allows you to access an XML file directly from GDL. For the available operations of the GDL XML Add-On see the GDL Reference Guide.
In the following example a library part will read an other objects’ parameter descriptions and values (from the libpart source xml: block object AC19.xml) and display it in the floorplan, like this:
The XML file has to be added to the active loaded libraries. To understand this example, it is strongly recommended to open the “block object AC19.xml” file in a text editor while reading this post and the GDL script.
The “block object AC19.xml” file has multiple script nodes. In this example we walk through only the ParamSection node, the rest are ignored. The ParamSection node has two child nodes: the ParamSectHeader, which is also ignored, and the Parameters node, which we need for getting the information from the file’s parameters.
This example works on any .xml files, which are reverted from .gsm format. For revert files in xml format see the How to use the LP_XMLConverter tool? article.
Open a file, prepare for walking in the “xml-tree”
First a channel (“_ch”) should be created with the OPEN command to the XML file handling. In this case the file handling is in read-only mode and the file is in the loaded library. Therefore the parameter_string should be “rl”.
_ch = open ("XML", "block object AC19.xml", "rl")
To navigate through the XML file various INPUT commands will be used, their return values have to be initialized:
_dummy = "" _posdesc = "" _name = "" _val = "" _type = "" _numberOfParams = 0
To walk through the nodes of the tree, first we have to request a new position descriptor from the extension.
The fieldID of the “NewPositionDesc” instruction has to be set, but the return value will be ignored. For these ignored values use the _dummy variable.
r = input (_ch, "NewPositionDesc", _dummy, _posdesc)
Finding parameters section
After getting the position descriptor the Parameters node has to be found, which is the second node under the ParamSection node, which is directly under the root. We can search for the first child node of the root which is an ELEMENT type and its name starts with “Param”, then search for its second ELEMENT type child which name starts with “Param”:
! ParamSection r = input (_ch, "MoveToNode FromFirstChild Param* ELEM", _posdesc, _name, _val, _type) ! Parameters r = input (_ch, "MoveToNode FromFirstChild Param* ELEM 2", _posdesc, _name, _val, _type)
However in this case the exact target path is known, therefore it can be given to the MoveToNode instruction directly:
! ParamSection / Parameters r = input (_ch, "MoveToNode Path ParamSection:Parameters", _posdesc, _name, _val, _type)
In this example we want to display all of the parameter descriptions and values of the Parameters node in a loop. For this action the number of the ELEMENT type child nodes need to be known first:
! Count the number of the Parameters r = input (_ch, "NumberofChildNodes ELEM *", _posdesc, _numberOfParams)
Getting all parameters
For the iteration the starting position is the first found parameter node, right under the Parameters node:
! First parameter (Length in this file) r = input (_ch, "MoveToNode FromFirstChild * ELEM", _posdesc, _name, _val, _type)
In each step the value of the Description node and its Value node pair (if it exists) are found and displayed. The Description node should always exist, and no other child nodes names are expected to start with “D”. Therefore the first ELEMENT type child node which name starts with “D” will be the Description node.
The value of the actual parameter description is under the Description node, and it is a CDATA type node. This node’s value should be displayed, but without the quotation marks.
With the STRSUB function only the substring is displayed with the TEXT2 command:
the substring starts from the second character, because the first one is the ‘”‘ character, end the substring’s length is 2 character (the 2 quotation marks) shorter then the _val variable value.
After displaying the Description CDATA value, we have to move back to the Description ELEMENT parent node. The “MoveToNode ToParent” instruction is equivalent with the “MoveToNode Path ..” instruction. The Value node is on the same level as the Description ELEMENT, and no other node’s name in this level is expected to start with “V”, therefore the instruction will be: “MoveToNode FromNextSibling V* ELEM”.
Not every parameter, which has a Description node has Value node as well. If the name of the node is not “Value” (the Value node is not found), further moves inside the not existing node cannot be done. In this case a “-” character is displayed, and we step back to the actual parameter parent node.
If the Value node is found, its child node’s value should be displayed. The child node can be TXT or CDATA type, and its value should be directly used in the TEXT2 command.
For getting the next parameter’s description and value, we navigate back to the parameters node, then move to the next one.
for i = 1 to _numberOfParams ! Description - move to the element r = input (_ch, "MoveToNode FromFirstChild D* ELEM", _posdesc, _name, _val, _type) ! Description - get its content, which can only be CDATA type r = input (_ch, "MoveToNode FromFirstChild * CDATA", _posdesc, _name, _val, _type) ! Display value without quotation marks text2 10, -i, strsub(_val, 2, strlen(_val) - 2) ! Move back to parent: Description element r = input (_ch, "MoveToNode ToParent", _posdesc, _name, _val, _type) ! Move to the Value element of the same parameter r = input (_ch, "MoveToNode FromNextSibling V* ELEM", _posdesc, _name, _val, _type) ! If Value element is not found (Title, Separation parameter types) ! display "-" and do not try to move to its child node if _name <> "Value" then text2 25, -i, "-" else ! Value - get its content, which can be TXT or CDATA type r = input (_ch, "MoveToNode FromFirstChild * TXT+CDATA", \ _posdesc, _name, _val, _type) ! Display the value content text2 25, -i, _val ! Move back to parent: Value element r = input (_ch, "MoveToNode ToParent", _posdesc, _name, _val, _type) endif ! Move back to parent: Parameter element r = input (_ch, "MoveToNode ToParent", _posdesc, _name, _val, _type) ! Move to the next Parameter element r = input (_ch, "MoveToNode FromNextSibling * ELEM", _posdesc, _name, _val, _type) next i
Close the file
At the end of the operation the position descriptor should be returned and the channel should be closed.
r = input (_ch, "ReturnPositionDesc", _posdesc, _dummy) close _ch