【问题标题】:How do I link a local disk location URL to a tag in XML?如何将本地磁盘位置 URL 链接到 XML 中的标记?
【发布时间】:2014-11-14 11:48:24
【问题描述】:

我对 XML 和使用 Python 的 XML 非常陌生。我为此使用 LXML 模块。我的目标是做类似的事情:

<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>

我想动态生成这个。说,我有大约 10 个文件,根据某些搜索条件,我需要对文件进行分类。可以说,分类是真假。 因此,在 True 部分下,我有一些 4 个文件。我想在本地磁盘上使用它们各自的文件位置在 XML 中创建一个条目。当我在浏览器中打开 XML 文件时,XML 文件中的链接可以为我打开目录。

所以我的问题是: 1. 如何在每次满足条件时创建 XML 标签? 2. 如何链接到本地​​磁盘位置?

到目前为止,我已经完成了结果的控制台打印。

        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')

【问题讨论】:

  • 您的标题包含一个问题。你的身体没有。因此,不清楚你问了什么,也不清楚你尝试了什么。
  • @LennartRegebro :我已经编辑了这个问题。你现在可以看看吗?
  • 每个问题应该有一个问题。第一个问题太笼统了。 “每次满足条件时如何创建 XML 标记?”答:当他们的条件满足时,你创建一个标签......

标签: python-2.7 lxml lxml.html


【解决方案1】:

回答标题中的问题:

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

回答帖子中的问题:

您使用file: url 链接到本地​​文件。我不确定它们在 Windows 上应该是什么样子,但我认为它是这样的:

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

寻找例子和实验。

【讨论】:

    【解决方案2】:
        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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多