How do I link a local disk location URL to a tag in XML?

1.2k views Asked by At

I am pretty new to XML and XML with Python. I am using LXML module for this. My objective is to do something like:

<include>
  <!--This is the result-->        #This is for naming the result of the file .
  <check run = "1000">
    <params>
      <param name="Name" path="$${path_to_the_file_in_local_disk}"/>
    </params>
    <True>
      <variable name="File1" path=""/>
      <variable name="File2" path="c:\xyz"/>
      <variable name="File3" path="c:\xyz"/>
      <variable name="File4" path="c:\xyz"/>
      <variable name="File5" path="c:\xyz"/>
      <variable name="File6" path="c:\xyz"/>
      <variable name="File7" path="c:\xyz"/>
      <variable name="File8" path="c:\xyz"/>
    </variables>
  </user>
</include>

And this i want to generate dynamically. Say, i have some 10 files and based on certain search criteria, i need to Classify the files. Lets say, classification is True and False. So, under True section, i have some 4 files. I want to make an entry in the XML with their respective file location on the local disk. When i open the XML file in browser, the link in the XML file can open up the directory for me.

So my Questions are: 1. How do i create a XML tag each time a condition is met? 2. How do i link it to the local disk location?

Till far, i have done the Console printing of the result.

        f = open('./script.log', 'r')
        for lines in f.readlines():
            passed      = lines.find("=== Result: PASS ===")
            failed      = lines.find("=== Result: FAIL ===")
            if passed != -1: 
                print "True File"
                passed_cnt = passed_cnt + 1
                passed_list.append(os.getcwd())
                lookup = '* COMMAND:'
                with open('./script.log') as myFile:
                    for num, line in enumerate(myFile, 1):
                        if lookup in line:
                            #print 'found at line:', num
                            tc_id = (line.split('\\')[-1]).split(' ')[-3]
                            print "TRUE FILE Name : ", tc_id
                            variable = etree.SubElement(variables, "variable")
                            variable.set('name', 'path')
                            variable.set('value', '1000')
2

There are 2 answers

0
Lennart Regebro On

To answer the question in the title:

with open("outfile.xml", "wb") as outfile:
    outfile.write(etree.tostring(xmlroot, xml_declaration=True))

To answer the question in the post:

You link to a local file with a file: url. I'm unsure how they should look exactly on Windows, but I think it's like this:

file://c\:\\<path to the file>

Look for examples and experiment.

0
Ai_Nebula On
    I found a way to deal with the problem here. My issues were:
    1. Generating a XML file.
    2. This file was to be be compiled dynamically for each and every run.

    I did something like:


from __future__ import division
import os
import fnmatch
import xml.etree.cElementTree as ET
import time
import csv
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
import datetime
from lxml import etree
import smtplib

    root = etree.Element("include")
    comment1 = etree.Comment("================<Your Text>================")
    root.append(comment1)
    user1 = etree.SubElement(root, "Complete_Results")
    param = etree.SubElement(user1, "Total")
    param.set('Success_Percentage', str('%.2f'%((passed_cnt/total_Count)*100)))
    param = etree.SubElement(user1, "Total")
    param.set('Failure_Percentage', str('%.2f'%((failed_cnt/total_Count)*100)))
    param = etree.SubElement(user1, "Aggregate_Result")
    if pass_percentage == 100:
        res = "_________________Successfully_Passed________________"
    else:
        res = "________________Iteration_Failed________________"
    param.set('Finally', res)
    user1 = etree.SubElement(root, "Success_Results")
    comment2 = etree.Comment("======================= Passed test cases section ==========================")
    user1.append(comment2)
    user1.set('Number_of_Test_cases_passed', str(passed_cnt))
    params = etree.SubElement(user1, "Results")
    param = etree.SubElement(params, "Success_Results")
    for i in passed_TC_list:
        for location in passed_list:
            param = etree.SubElement(params, 'TC_Details')
            param.set('File_name', str(i))
            param = etree.SubElement(params, 'ID' )
            param.set('Path_in_Local_Directory',str(location))
            path = str(str(location) + str("\\") + str(i))
            param.set('Link_to_file', str(path))
            passed_list.remove(location)