Forum Discussion

Irina_1's avatar
Irina_1
New Contributor
14 years ago

A problem with format when I create an xml file



Hello! I use TestComplete v.6 and save some information in an xml file using Sys.OleObject("DOMDocument...").






 function AddTestRun() 





 var Doc, Nodes, i, Node;


  Doc = Sys.OleObject("Msxml2.DOMDocument.6.0");


  Doc.async = false;                              


  Doc.load(path_result_log);    






  var test_run = Doc.createElement("TestRun");


  var attID = Doc.createAttribute("testTool");


  attID.Text = "TestComplete";


  test_run.setAttributeNode(attID);


  


  var rootElement = Doc.selectSingleNode("//TestToolRun");  


  rootElement.AppendChild(test_run);


  Doc.Save(path_result_log);


}






But this file doesn't have an applicable format. If I open the file in Notepad I will see that all xml tags will be joined in one string.









How can I make my script to create an xml file in an applicable format - every tag on a new line?






Thank you.
  • Irina,


    This is how Microsoft DOM objects work. They don't insert line breaks or indentation when saving XML elements to a file.

    This format does not affect the way .xml files are loaded by script code or applications. That is, if you work with the file programmatically, perhaps, you can keep the situation as it is now. However, if the file is intended for reviewing or editing by humans, you have to create code that will insert line breaks and indents.


    If you have Visual Studio, then to format the file contents, select Edit | Advanced | Format Document from Visual Studio's menu.


    If you want to format the document from script, you can create an .xsl template and apply it to your .xml file when it is ready.

    But an easier solution, I think, would be to call the createTextNode method to insert needed text data into the xml tree.

    The following code snippet demonstrates how to use this method. The code is based on the procedure you use but differs a little. I think you will get the idea:




    // Insert a line break

    function AddCRLFNode(ADoc, parentNode)

    {

      var node = ADoc.createTextNode("\n");

      parentNode.appendChild(node);

    }


    // Insert an indent

    function AddIndentNode(ADoc, parentNode)

    {

      var node = ADoc.createTextNode("\t");

      parentNode.appendChild(node);

    }


    function AddTestRun() 



      var Doc, Nodes, i, Node;


      // File name

      var path_result_log = "C:\\Temp\\MyTest.xml";

      

      Doc = Sys.OleObject("Msxml2.DOMDocument.6.0");

      Doc.async = false;                              

      // Doc.load(path_result_log);   // My code always creates a new file


      // Create the root element

      var test_run = Doc.createElement("TestRun");

      var attID = Doc.createAttribute("testTool");

      attID.Text = "TestComplete";

      test_run.setAttributeNode(attID);


      // Add the root element to the doc

      Doc.AppendChild(test_run);

      AddCRLFNode(Doc, test_run); // Add a line break 

     

      // Create another element

      var elem2 = Doc.createElement("childElement1");

      attID = Doc.createAttribute("someAttr");

      attID.Text = "Attribute_Value";

      elem2.setAttributeNode(attID);


      // Add the element to the doc

      AddIndentNode(Doc, test_run); // Add an indent

      test_run.AppendChild(elem2);  // Add the element

      AddCRLFNode(Doc, test_run);   // Add a line break


      // Create the third element

      var elem3 = Doc.createElement("childElement2");

      attID = Doc.createAttribute("someAttr");

      attID.Text = "Attribute_Value";

      elem3.setAttributeNode(attID);


      // Add the element to the doc

      AddIndentNode(Doc, test_run); // Add an indent

      test_run.AppendChild(elem3);  // Add the element

      AddCRLFNode(Doc, test_run);   // Add a line break


      // Save changes

      Doc.Save(path_result_log);

    }