Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:19:45+00:00 2026-06-11T21:19:45+00:00

I asked a question about adding multiple elements a couple weeks ago and now

  • 0

I asked a question about adding multiple elements a couple weeks ago and now I’ve run into a similar issue. I have to create some XML where I’ll have the following:

<embossed>
   <line>Test Line</line>
   <line>Test Line 2</line>
   <line>Test Line 3</line>
</embossed>

I cannot figure out how to create the same element N times in a row with different text using the LXML objectify.Element() method. I tried this:

embossed = objectify.Element('embossed')
embossed.line = objectify.Element("line")
embossed.line = objectify.Element("line")

But I end up with only one “line” element inside the “embossed” element. Does anyone know how to do this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:19:46+00:00Added an answer on June 11, 2026 at 9:19 pm

    Just append the lines to embossed, instead:

    embossed = objectify.Element('embossed')
    embossed.append(objectify.Element('line'))
    embossed.line[-1] = 'Test Line'
    embossed.append(objectify.Element('line'))
    embossed.line[-1] = 'Test Line 2'
    

    Each lxml tree tag acts like a list, where any children are elements in the list. Simply appending new objectify.Element objects makes them children of the tag you append them to.

    You can then reach each element of that list by using indexing; the -1 index is the last element, allowing us to set it’s text.

    The above code outputs:

    >>> from lxml import objectify, etree
    >>> embossed = objectify.Element('embossed')
    >>> embossed.append(objectify.Element('line'))
    >>> embossed.line[-1] = 'Test Line'
    >>> embossed.append(objectify.Element('line'))
    >>> embossed.line[-1] = 'Test Line 2'
    >>> print etree.tostring(embossed, pretty_print=True)
    <embossed xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE">
      <line py:pytype="str">Test Line</line>
      <line py:pytype="str">Test Line 2</line>
    </embossed>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have asked a question about manytomanyfield here which i solved but now a
A few weeks ago i asked a question about a multilanguage site (see Multilanguage
I have tried looking though several of the already asked question about this topic
I asked a question about building custom Thread Safe Generic List now I am
I asked a question about bigints yesterday which was kindly answered. However, I have
I've just asked a question about an hour ago, while waiting for replies, I've
I have asked this question about using the a Linq method that returns one
People have asked similar questions about the efficiency of various data structures but none
I asked a similar question yesterday that was specific to a technology, but now
I've asked one question about this a month ago, it's here: "post" method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.