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

  • SEARCH
  • Home
  • 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 8506223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:32:07+00:00 2026-06-11T02:32:07+00:00

I have the following XML that I am trying to recreate with the lxml.objectify

  • 0

I have the following XML that I am trying to recreate with the lxml.objectify package

<file>
  <customers>
    <customer>
        <phone>
            <type>home</type>
            <number>555-555-5555</number>
        </phone>
        <phone>
            <type>cell</type>
            <number>999-999-9999</number>
        </phone>
        <phone>
            <type>home</type>
            <number>111-111-1111</number>
        </phone>
    </customer>
   </customers>
</file>

I can’t figure out how to create the phone element multiple times. Basically, I have the following non-working code:

    # create phone element 1
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE1']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 1']

    # create phone element 2
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE2']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 2']

    # create phone element 3
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE3']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 3']

Which of course only outputs one section of phone information in the resulting XML. Does anyone have any ideas?

  • 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-11T02:32:08+00:00Added an answer on June 11, 2026 at 2:32 am

    You should create objectify.Element objects, and add them as children of root.customers.

    For example, inserting two phone numbers can be done like this:

    phone = objectify.Element('phone')
    phone.type = data_dict['PRIMARY PHONE1']
    phone.number = data_dict['PRIMARY PHONE TYPE 1']
    root.customers.customer.append(phone)
    
    phone = objectify.Element('phone')
    phone.type = data_dict['PRIMARY PHONE2']
    phone.number = data_dict['PRIMARY PHONE TYPE 2']
    root.customers.customer.append(phone)
    

    If you get unnecessary attributes on these elements when transforming the xml back to a string, use objectify.deannotate(root, xsi_nil=True, cleanup_namespaces=True).
    See lxml’s objectify documentation for exact parameters of objectify.deannotate.

    (If you’re using an older version of lxml, which doesn’t include the cleanup_namespaces keyword argument, do this instead:

    from lxml import etree
    # ...
    objectify.deannotate(root, xsi_nil=True)
    etree.cleanup_namespaces(root)
    

    )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following line in my xml file that I am trying to
I have the following type of XML that I'm trying to parse. The code
I have the following line of xml that I'm trying transform using XSLT but
i have the following question: I have a XML file with some elements that
I have the following XML code that I'm trying to parse, but I'm sure
I'm trying to write a schema that will validate the following XML: <category type=rifles>
I have the following xml that I need to get 2 values (see *
I have the following xml format that i want to bind it through a
I have the following XML Document (that can be redesigned if necessary) that stores
I have the following XML and I want to process it so that I

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.