2

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>
2
  • I don't get it, what should be the output you expect? Commented Aug 18, 2011 at 13:48
  • Hey FC, just added requested output. Commented Aug 18, 2011 at 13:52

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

hey, thanks again. I could work on it. have posted the answer along with my question. please check
1

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

To get the text in the tag use .text

1 Comment

hey, the default thingy is helpful, i could handle errors with that. thanks FC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.