• No results found

XML files

In document Ansa Scripting (Page 60-64)

4. HANDLING ASCII, XML AND BINARY FILES

4.3 XML files

The most important functions for handling xml files are listed in the following table:

# Function Description

1 XmlOpenBase("/home/script.xml", "scripted"); Opens a XML data base 2 XmlGetNode(base, "VTAElements/");XmlGetNode(base, root,"VTAElements/"); Fetces a node

3 XmlGetSubNodes(base,node); Fetches the subnodes of a node 4 XmlNodeName(base, sub_param); Gets the name of a node 5 XmlGetStringValue(base,sub_param,"Name",""); Gets the string value of a node

6 XmlGetNodeData(base,node); Gets node's data

7 XmlNewNode(base,"Connection Elements",1);XmlNewNode(base,node_connection,"Spotwelds",1); Creates new node

8 XmlSetStringValue(base, sub_node, "val","str"); Set a string value to a node 9 XmlSetNodeData(base, sub_node, "some_data"); Sets node's data

For the clarification of the above functions the following xml scheme is going to be used for reading and writing:

<Data>

<Connection_Elements> <SpotweldPoints>

<Parameter Code = "Connection Name">Spot1</Parameter> <Parameter Code = "xcoord">1.2</Parameter>

<Parameter Code = "ycoord">5.0</Parameter> <Parameter Code = "zcoord">3.5</Parameter>

<Parameter Code = "comment">"Connect 2 flanges"</Parameter> </SpotweldPoints>

<Parts>

<PartInfo Code = "Part Name 1">"upper"</PartInfo> <PartInfo Code = "Part Name 2">"lower"</PartInfo> <PartInfo Code = "Prop Id 1">10</PartInfo>

<PartInfo Code = "Prop Id 2">20</PartInfo> </Parts>

</Connection_Elements> </Data>

4.3.1 Opening xml files

For opening an xml file the XmlOpenBasefunction must be used.

def main() {

base = XmlOpenBase("/home/work/connections.xml", "internal_name"); ...

}

The key point in this function is the output value which is an element (pointer) and indicates the xml database. Almost all the XML functions that are going to be used later will need this element(pointer). The second argument is necessary only when writing XML files where it is used as base node. In case of reading, it can take an arbitrary internal name that will not be used further. When this function points to a nonexistent file then a file is automatically created.

4.3.2 Getting nodes and sub-nodes

For getting the element (pointer) of a node the XmlGetNode must be used. In our example nodes are considered the tags <Data>, <Connection_Elements>, <SpotweldPoints> and <Parts>. The root node in this structure is considered the <Data> and this is the node that must be fetched first. The syntax for getting it is:

data = XmlGetNode(base,"Data/"); //For getting the <Data>

Since "Data" is the root we can set second argument as blank:

node = XmlGetNode(base,""); //This will get the <Data>

The output data is a matrix that contains the element (pointer) of ,"Data"

The nodes that comes hierarchically just after the root, like the <Connection_Elements>, can be taken if we write:

//For getting the <Connection_Elements>

connection_elements = XmlGetNode (base,data[0],"Connection_Elements");

Alternatively, after getting a node it is easy to get its sub-nodes using the XmlGetSubNodes. Attention must be given in the function's second argument since it must be taken from the output matrix of the

XmlGetNode that was called before. In our example the node <Connection_Elements> is the subnode of

<Data>, while the nodes <SpotweldPoints> and <Parts> are the subnodes of <Connection_Elements>. The code that must be used is:

data = XmlGetNode(base,"Data/");

//For getting the <Connection_Elements>

connection_elements = XmlGetNode (base,data[0],"Connection_Elements"); //For getting the <SpotweldPoints>, <Parts>

subnodes = XmlGetSubNodes(base, connection_elements[0]);

The output is always a matrix that contains the elements (pointers) of the sub-nodes.

4.3.3 Getting the name of the node

In an XML file each node holds a name, which describes most of the times an area of interest. The name can be extracted using the XmlNodeName function. Its syntax is very simple and it needs the base and the node element (pointer).

connection_elements = XmlGetNode (base,data[0],"Connection_Elements"); //For getting the <SpotweldPoints>, <Parts>

subnodes = XmlGetSubNodes(base, connection_elements[0]); foreach subnode in subnodes

{

node_name = XmlNodeName(base,subnode); Print(node_name);

In the above example the names 'SpotweldPoints' and 'Parts' are going to be displayed on the screen.

4.3.4 Getting attributes and node data

Apart from its name, a node can also have attributes and data that are also important for the process. Let's focus on node <SpotweldPoints> that has the sub-nodes <Parameter> which in turn have the attribute 'Code'. This attribute takes five parameter names ('Connection Name', 'xcoord', 'ycoord', 'zcoord', 'comment'), while each node holds data that is associated with each of these parameters ('Spot1, 1.2, 5, 3.5, Connect 2 flanges'). Two new functions will be introduced for extracting all these information: These are the XmlGetStringValue and the XmlGetNodeData. Their use is demonstrated together with the complete example:

def main() {

base = XmlOpenBase("/home/work/connections.xml", "internal_name"); data = XmlGetNode(base,"Data/");

connection_elements = XmlGetNode(base,data[0],"Connection_Elements"); sub_nodes = XmlGetSubNodes(base,connection_elements[0]);

foreach sub_node in sub_nodes {

node_name = XmlNodeName(base,sub_node); if(node_name=="SpotweldPoints")

{

subs_of_spots = XmlGetSubNodes(base,sub_node); foreach sub_of_spots in subs_of_spots

{ param_name = XmlGetStringValue(base,sub_of_spots,"Code",""); if(param_name=="Connection Name") connection_name = XmlGetNodeData(base,sub_of_spots); if(param_name=="xcoord") xcoord = XmlGetNodeData(base,sub_of_spots); if(param_name=="ycoord") ycoord = XmlGetNodeData(base,sub_of_spots); if(param_name=="zcoord") zcoord = XmlGetNodeData(base,sub_of_spots); if(param_name=="comment") comment = XmlGetNodeData(base,sub_of_spots); } Print("connection_name:"+connection_name); Print("xcoord:"+xcoord); Print("ycoord:"+ycoord); Print("zcoord:"+zcoord); Print("comment:"+comment); } } }

Important Note1: The name of an attribute is necessary for retrieving the attribute values.

Important Note2: If the attribute hasn't got a value then a default can be assigned through the forth

argument of XmlGetStringValue

4.3.5 Writing XML files and setting the base node

In order to create a new XML file, the function XmlOpenBase must be used. The second argument is important since it defines the base node, which in our case is the <Data>.

base = XmlOpenBase("/home/jharal/tmp/test.xml","Data"); 4.3.6 Creating nodes

A new node can be created with the function XmlNewNode. Its syntax accepts either 3 or 4 arguments. Depending on the syntax a node can be created just after the base node (3 arguments) or in any other place (4 arguments). In the latter case an element (pointer) indicating the parent node will be needed. In the following example:

the node 'Connection Elements' will be placed after the node 'Data' while if you write:

node_spot = XmlNewNode(base,node_connection,"SpotweldPoints",1);

the node 'SpotweldPoints' will be created after the 'Connection Elements'. It is clear that in this case the output of the previous call was used. Last argument indicates whether the node appears once (1) or more (0). In our example it is 1 since both nodes are written once. On the other hand if we create the node 'Parameter' which appears more than once, then we should write:

node_param = XmlNewNode(base,node_spot,"Parameters",0); 4.3.7 Setting attributes and node data

Attribute names and attribute parameters are given through the XmlSetStringValue.

XmlSetStringValue(base,node_param,"Code","Connection name");

or

XmlSetStringValue(base,node_param,"Code","xcoord");

In a similar way the function XmlSetNodeData sets data to a node.

XmlSetNodeData(base,node_param,"Spot1");

or

XmlSetNodeData(base,node_param,"1.2");

Recapitulating, the final code for writing the whole xml file (See Section 4.3) is the following:

def main() {

base = XmlOpenBase("/home/jharal/tmp/test.xml","Data"); node_connection = XmlNewNode(base,"Connection Elements",1); node_spot = XmlNewNode(base,node_connection,"SpotweldPoints",1);

parameter_names_spots = {"Connection name","xcoord","ycoord","zcoord","comment"}; node_data_spots = {"Spot1","1.2","5.0","3.5","Connect 2 flanges"};

for(i=0;i<5;i++) { node_param = XmlNewNode(base,node_spot,"Parameters",0); XmlSetStringValue(base,node_param,"Code",parameter_names_spots[i]); XmlSetNodeData(base,node_param,node_data_spots[i]); } node_parts = XmlNewNode(base,node_connection,"Parts",1);

parameter_names_parts = {"Part Name 1","Part Name 2","Prop Id 1","Prop Id 2"}; node_data_parts = {"upper","lower","10","20"}; for(i=0;i<4;i++) { node_param = XmlNewNode(base,node_parts,"Parameters",0); XmlSetStringValue(base,node_param,"Code",parameter_names_parts[i]); XmlSetNodeData(base,node_param,node_data_parts[i]); } XmlCloseBase(base,1); }

In document Ansa Scripting (Page 60-64)

Related documents