preload

XML output with WinRunner – 2

Posted by Albert Gareev on Aug 18, 2007 | Categories: Source codeXML Data

Root page: Service Functions – XML

Parent page: Service Functions – XML / WinRunner

Create XML Node


public function xml_node_create(inout dtNode[], in tag, in text) {
 delete dtNode[];
 dtNode["tag"] = tag;
 dtNode["text"] = xml_str_norm(text);
}

Add Attributes to XML Node


public function xml_node_attr_add(inout dtNode[], in attr_tag1, in attr_value1, in attr_tag2, in attr_value2, in attr_tag3, in attr_value3, in attr_tag4, in attr_value4, in attr_tag5, in attr_value5) {

 if ((nargs()-1)<=0) return;
 
 if (attr_tag1 != "") {
  dtNode[1,"attr_tag"] = attr_tag1;   
  dtNode[1,"attr_value"] = xml_str_norm(attr_value1);   
  dtNode[0,"attr_count"] = 1;
  }
 else
  return; 
 
 if (attr_tag2 != "") {
  dtNode[2,"attr_tag"] = attr_tag2;   
  dtNode[2,"attr_value"] = xml_str_norm(attr_value2);   
  dtNode[0,"attr_count"] = 2;
  }
 else
  return; 
 
 if (attr_tag3 != "") {
  dtNode[3,"attr_tag"] = attr_tag3;   
  dtNode[3,"attr_value"] = xml_str_norm(attr_value3);   
  dtNode[0,"attr_count"] = 3;
  }
 else
  return; 
 
 if (attr_tag4 != "") {
  dtNode[4,"attr_tag"] = attr_tag4;   
  dtNode[4,"attr_value"] = xml_str_norm(attr_value4);   
  dtNode[0,"attr_count"] = 4;
  }
 else
  return; 
 
 if (attr_tag5 != "") {
  dtNode[5,"attr_tag"] = attr_tag5;   
  dtNode[5,"attr_value"] = xml_str_norm(attr_value5);   
  dtNode[0,"attr_count"] = 5;
  }
 else
  return; 
}

Append XML Node to file


public function xml_node_open(in xml_file, inout dtNode[]) {
 auto rc;
 auto s_line;
 
 if (xml_write_mode) {
  rc = file_open(xml_file, FO_MODE_APPEND);
  if (rc != E_OK) return(rc);
  
  file_printf(xml_file, "%s\r\n", "");
  s_line = node_indent & xml_str_node_tag(dtNode);
  file_printf(xml_file, "%s\r\n", s_line); 
  if (dtNode["text"] != "")
   file_printf(xml_file, "%s\r\n", node_indent & "  " & dtNode["text"]);
  file_printf(xml_file, "%s\r\n", "");
 
  file_close(xml_file);
  }
 else {
  dump_printf("%s\r\n", "");
  s_line = node_indent & xml_str_node_tag(dtNode);
  dump_printf("%s\r\n", s_line); 
  if (dtNode["text"] != "")
   dump_printf("%s\r\n", node_indent & "  " & dtNode["text"]);
  dump_printf("%s\r\n", "");
  } 

### Indent!!! 
 node_indent = node_indent & "\t";
 
 return(E_OK);
}

static function xml_str_node_tag(inout dtNode[]) {
 auto res, i,c;
 
 res = "<" & dtNode["tag"];

 c = dtNode[0,"attr_count"];
 if (c=="") c=0;
 for (i=1;i<=c;i++)
  res = res & " " & dtNode[i,"attr_tag"] & "=" & "\""& dtNode[i,"attr_value"] & "\"";  

 res = res & ">";
 return(res); 
}

Finalize XML Node to file


public function xml_node_close(in xml_file, inout dtNode[]) {
 auto rc;
 auto s_line;

### Indent!!! 
 node_indent = substr(node_indent,2);

 if (xml_write_mode) {
  rc = file_open(xml_file, FO_MODE_APPEND);
  if (rc != E_OK) return(rc);
 
  file_printf(xml_file, "%s\r\n", "");
  s_line = node_indent & "</" & dtNode["tag"] & ">";
  file_printf(xml_file, "%s\r\n", s_line); 
  file_printf(xml_file, "%s\r\n", "");
 
  file_close(xml_file);
  }
 else {
  dump_printf("%s\r\n", "");
  s_line = node_indent & "</" & dtNode["tag"] & ">";
  dump_printf("%s\r\n", s_line); 
  dump_printf("%s\r\n", "");
  } 
 
 return(E_OK);
}


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.