Python 2.7 如何将本地磁盘位置URL链接到XML中的标记?

Python 2.7 如何将本地磁盘位置URL链接到XML中的标记?,python-2.7,lxml,lxml.html,Python 2.7,Lxml,Lxml.html,我对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

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

回答标题中的问题:

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

要回答帖子中的问题:

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

file://c\:\\<path to the file>
file://c\:\\
寻找例子和实验。

我在这里找到了解决问题的方法。我的问题是:
    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)
1.生成XML文件。 2.每次运行都要动态编译此文件。 我做了这样的事情: 来自未来进口部 导入操作系统 导入fnmatch 将xml.etree.cElementTree作为ET导入 导入时间 导入csv 从xml.etree.ElementTree导入元素、子元素、注释到字符串 导入日期时间 从lxml导入etree 导入smtplib root=etree.Element(“包括”) comment1=etree.Comment(“========================================================================================”) root.append(注释1) user1=etree.SubElement(根,“完成结果”) param=etree.SubElement(user1,“总计”) 参数集('Success_Percentage',str('%.2f'%((已通过计数/总计数)*100))) param=etree.SubElement(user1,“总计”) 参数集('Failure_Percentage',str('%.2f'%((failed_cnt/total_Count)*100))) param=etree.SubElement(user1,“聚合结果”) 如果通过率=100: res=“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 其他: res=“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu失败” 参数集('Finally',res) user1=etree.SubElement(根,“成功结果”) comment2=etree.Comment(“=====================================================================================================================================”) user1.append(comment2) user1.set('通过测试的案例数',str(通过测试)) params=etree.SubElement(user1,“结果”) param=etree.SubElement(params,“Success_Results”) 对于已通过的i\u TC\u列表: 对于已通过的\u列表中的位置: param=etree.SubElement(params,'TC_Details') 参数集('File_name',str(i)) param=etree.SubElement(参数'ID') 参数集('Path_in_Local_Directory',str(location)) 路径=str(str(位置)+str(“\\”)+str(i)) 参数集('Link_to_file',str(path)) 已通过\u列表。删除(位置)
您的标题包含一个问题。你的身体不会。因此不清楚你问了什么,也不清楚你试过什么。@LennartRegebro:我已经编辑了这个问题。你现在能看一下吗?每个问题你应该有一个问题。第一个问题太笼统了。“如何在每次满足条件时创建XML标记?”回答:当满足条件时,您创建一个标记。。。
    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)