在python中使用Etree解析XML配置文件

发布于 2024-11-30 00:49:43 字数 1956 浏览 1 评论 0原文

请帮我使用 lxml etree 解析以下原型的配置文件。我尝试使用 for event, element 和 tostring。不幸的是,我不需要文本,而是

<template name> 
   <config>
 </template> 

给定属性之间的 XML。

我从这段代码开始,但在搜索属性时出现关键错误,因为它从头开始扫描。

config_tree = etree.iterparse(token_template_file)
for event, element in config_tree:

    if element.attrib['name']=="ad auth":
        print ("attrib reached. get XML before child ends")

由于我是 XML 和 python 的新手,我不知道如何处理它。这是配置文件:

<Templates>

  <template name="config1">

    <request>

      <password>pass</password>

      <userName>username</userName>

      <appID>someapp</appID>

    </request>

  </template>

  <template name="config2">

    <request>

      <password>pass1</password>

      <userName>username1</userName>

      <appID>someapp</appID>

    </request>

  </template>

</Templates>

提前致谢!

预期输出:

假设用户请求 config2- 那么输出应如下所示:(

   <request>

      <password>pass1</password>

      <userName>username1</userName>

      <appID>someapp</appID>

    </request>

我使用 httplib2 将此 XML 发送到服务器进行初始身份验证)

最终代码:

感谢 FC 和 Constantnius。这是最终的代码:

    config_tree = etree.parse(token_template_file)
    for template in config_tree.iterfind("template"):

        if template.get("name") == "config2":
            element = etree.tostring(template.find("request"))

            print (template.get("name"))
            print (element)  

输出:

     config2

    <request>

         <password>pass1</password>

          <userName>username1</userName>

          <appID>someapp</appID>

    </request>

Please help me parse a configuration file of the below prototype using lxml etree. I tried with for event, element with tostring. Unfortunately I don't need the text, but the XML between

<template name> 
   <config>
 </template> 

for a given attribute.

I started with this code, but get a key error while searching for the attribute since it scans from start

config_tree = etree.iterparse(token_template_file)
for event, element in config_tree:

    if element.attrib['name']=="ad auth":
        print ("attrib reached. get XML before child ends")

Since I am a newbie to XML and python, I am not sure how to go about it. Here is the config file:

<Templates>

  <template name="config1">

    <request>

      <password>pass</password>

      <userName>username</userName>

      <appID>someapp</appID>

    </request>

  </template>

  <template name="config2">

    <request>

      <password>pass1</password>

      <userName>username1</userName>

      <appID>someapp</appID>

    </request>

  </template>

</Templates>

Thanks in advance!

Expected Output:

Say the user requests the config2- then the output should look like:

   <request>

      <password>pass1</password>

      <userName>username1</userName>

      <appID>someapp</appID>

    </request>

(I send this XML using httplib2 to a server for initial authentication)

FINAL CODE:

thanks to FC and Constantnius. Here is the final code:

    config_tree = etree.parse(token_template_file)
    for template in config_tree.iterfind("template"):

        if template.get("name") == "config2":
            element = etree.tostring(template.find("request"))

            print (template.get("name"))
            print (element)  

output:

     config2

    <request>

         <password>pass1</password>

          <userName>username1</userName>

          <appID>someapp</appID>

    </request>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-12-07 00:49:43

您可以尝试迭代 XML 中的所有 template 元素并使用以下代码解析它们:

for template in root.iterfind("template"):
    name = template.get("name")
    request = template.find(requst)
    password = template.findtext("request/password")
    username = ...
    ...
    # Do something with the values

You could try to iterate over all template elements in the XML and parse them with the following code:

for template in root.iterfind("template"):
    name = template.get("name")
    request = template.find(requst)
    password = template.findtext("request/password")
    username = ...
    ...
    # Do something with the values
‖放下 2024-12-07 00:49:43

您可以尝试使用 get('name', default='') 而不是 ['name']

要获取标记中的文本,请使用 .text< /代码>

You could try using get('name', default='') instead of ['name']

To get the text in the tag use .text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文