0

I am building the xml format.

I want to do something like this:

<ValidationRequest>
<Username>[email protected]</Username>
<Password>XBR!t6YMeJbmsjqV</Password>
<Email>[email protected]</Email>
<Email>[email protected]</Email>
<Email>[email protected]</Email>
</ValidationRequest>

Now I can append all email into ValidationRequest. But I got the wrong format.

This is my code:

#!/usr/bin/python
import lxml.etree
import lxml.builder    

email = ['[email protected]','[email protected]','[email protected]']

E = lxml.builder.ElementMaker()
ValidationRequest = E.ValidationRequest
Username = E.Username
Password = E.Password
Email = E.Email

op = []
for a in email:
    GG = Email(a)
    op.append(lxml.etree.tostring(GG, pretty_print=True))
    print (lxml.etree.tostring(GG, pretty_print=True))
print(op)
ed = ''.join(map(str, op))
print(ed)
the_doc = ValidationRequest(
            Username('admin'),
            Password('XBR'),
            ed
        )   

print (lxml.etree.tostring(the_doc, pretty_print=True))

The response:

b'<Email>[email protected]</Email>\n'
b'<Email>[email protected]</Email>\n'
b'<Email>[email protected]</Email>\n'
[b'<Email>[email protected]</Email>\n', b'<Email>[email protected]</Email>\n', b'<Email>[email protected]</Email>\n']
b'<Email>[email protected]</Email>\n'b'<Email>[email protected]</Email>\n'b'<Email>[email protected]</Email>\n'
b"<ValidationRequest><Username>admin</Username><Password>XBR</Password>b'&lt;Email&gt;[email protected]&lt;/Email&gt;\\n'b'&lt;Email&gt;[email protected]&lt;/Email&gt;\\n'b'&lt;Email&gt;[email protected]&lt;/Email&gt;\\n'</ValidationRequest>\n"

How can I keep <Email></Email> and post to ValidationRequest?

1 Answer 1

1

You had the right idea, there's just a lot there that's just moving stuff around. You can bring it down to this:

import lxml.etree
import lxml.builder

email = ['[email protected]','[email protected]','[email protected]']

em = lxml.builder.ElementMaker()

the_doc = em.ValidationRequest(
    em.Username('admin'),
    em.Password('XBR'),
    *(em.Email(address) for address in email)
)

print(lxml.etree.tostring(the_doc, pretty_print=True).decode())

The .decode() is only there for it to print nicely. If you need the bytes, you can just leave that off.

Note this line in particular:

    *(em.Email(address) for address in email)

This loops over email, generating an Email element for each address as it goes, and then unpacking the resulting tuple (which is the result of the parentheses surrounding the generator) in to the constructor for the ValidationRequest element.

Output:

<ValidationRequest>
  <Username>admin</Username>
  <Password>XBR</Password>
  <Email>[email protected]</Email>
  <Email>[email protected]</Email>
  <Email>[email protected]</Email>
</ValidationRequest>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! This help me a lot

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.