18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to Read XML file attributes using Sahi

wasimhbrwasimhbr Members
edited November -1 in Sahi - Open Source
Hi,

How can we read xml file attributes into sahi script, are there any API to support this?

I went through "http://blog.sahi.co.in/2009/06/parsing-xml-in-sahi-scripts.html" but dint help much. I do not want to copy and paste my entire xml as code, I want to keep a separare xml file and read its attibutes as per my needs.

I want to iterate through XML file using a for loop and fetch the attributes. can any one please help me as how to achieve this.

Example XML file :

<studylist>
<study>
<uid> 4aabb264-4b5f-4c21-8b5f-8c214b5f8c21 </uid>
<status> 0 </status>
</study>
<study>
<uid> e8fcb21b-c03f-49cb-803f-39cbc03f39cb </uid>
<status> 1 </status>
</study>
</studylist>

Comments

  • narayannarayan Administrators
    Read the file using _readFile() and then pass it into new XML()
    var xmlStr = _readFile("yourfile.xml");
    var $x = new XML(xmlStr);
    
  • wasimhbrwasimhbr Members
    edited June 2010
    Hi Narayan,

    I used the same code you have shared above. Its not working.

    var xmlStr = _readFile("C:\\Sahi\\userdata\\scripts\\StudyList.xml");
    var $x = new XML(xmlStr);

    These above two lines seem to work, but when i use a alert message

    _alert($x);

    It displayes an empty object, If i ignore this and go further down.

    _alert($x.study2.status.toString()); //changed above xml a bit

    It throws up an empty value. I believe "new XML(xmlStr);" function used above is over writing the contents read from xml file or The code used to read the xml attributes is incorrect?

    can u please provide a sample code hear on continuation steps as how to proceed reading XML attributs.
  • narayannarayan Administrators
    Hi Wasim,

    I would suggest running the example exactly as it is. Once you get it working, you can modify it and check what is failing.

    Regards,
    Narayan
  • wasimhbrwasimhbr Members
    edited June 2010
    Thanks Narayan, It works now.

    As you mentioned the trick hear is, we need to stick to the same example and try out initially. later we can modify based on our needs.

    For other Sahi users, Sample code to read XML file:

    var xmlStr = _readFile("C:\\Sahi\\userdata\\scripts\\XML_auth.xml");
    var $x = new XML(xmlStr);

    [Note: create a XML file similar to the one mentioned in the example of the above link and follow the code to fetch attributes from it]
  • narayannarayan Administrators
    :) Wasim, This is a standard practice followed by all good developers. Make changes to an application in steps, and check after each step. Makes it very easy to debug problems.

    Regards,
    Narayan
Sign In or Register to comment.