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 8866991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:55:07+00:00 2026-06-14T16:55:07+00:00

Basic question – how do you ‘move’ around in a tree when you are

  • 0

Basic question – how do you ‘move’ around in a tree when you are building a tree.

I can populate the first level:

import lxml.etree as ET

def main():
    root = ET.Element('baseURL')
    root.attrib["URL"]='www.com'
    root.attrib["title"]='Level Title'
    myList = [["www.1.com","site 1 Title"],["www.2.com","site 2 Title"],["www.3.com","site 3 Title"]]   
    for i in xrange(len(myList)):
        ET.SubElement(root, "link_"+str(i), URL=myList[i][0], title=myList[i][1])

This gives me something like:

baseURL:
        link_0
        link_1
        link_2

from there, I want to add a subtree from each of the new nodes so it looks something like:

baseURL:
        link_0:
               link_A
               link_B
               link_C
        link_1
        link_2

I can’t see how to ‘point’ the subElement call to the next node down – I tried:

myList2 = [["www.A.com","site A Title"],["www.B.com","site B Title"],["www.C.com","site C Title"]]
for i in xrange(len(myList2)):
        ET.SubElement('link_0', "link_"+str(i), URL=myList2[i][0], title=myList2[i][1])

But that throws the error:

TypeError: Argument '_parent' has incorrect type (expected lxml.etree._Element, got str)

as I am giving the subElement call a string, not an element reference. I also tried it as a variable, (i.e. link_0' rather than“link_0″`) and that gives a global missing variable, so my reference is obviously incorrect.

How do I ‘point’ my lxml builder to a child as a parent, and write a new child?

  • 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-14T16:55:08+00:00Added an answer on June 14, 2026 at 4:55 pm

    ET.SubElement(parent_node,type) creates a new XML element node as a child of parent_node. It also returns this new node.

    So you could do this:

    import lxml.etree as ET
    
    def main():
      root = ET.Element('baseURL')
      myList = [1,2,3]
      children = []
      for x in myList:
        children.append( ET.SubElement(root, "link_"+str(x)) )
    
      for y in myList:
         ET.SubElement( children[0], "child_"+str(y) )
    

    But keeping track of the children is probably excessive since lxml already provides you with many ways to get to them.

    Here’s a way using lxmls built in children lists:

     node = root[0]
     for y in myList:
       ET.SubElement( node, "child_"+str(y) )
    

    Here’s a way using XPath (possibly better if your XML is getting ugly)

     node = root.xpath("/baseURL/link_0")[0]
     for y in myList:
       ET.SubElement( node, "child_"+str(y) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic question: In R, how can I make a list and later populate it
Basic question.. had to ask. Any help will be appreciated. Q: Why can't I
Pretty basic question ... I can not find the compatibility table (was looking at
Extremely basic question. Are there common actions I can perform using keyboard shortcuts in
My basic question is: How can I force Hibernate to make float NULLable and
Pretty basic question - I can't seem to vertically align an icon inside of
Basic question, but I can't figure it out...I have a label with an accelerator
Very basic question, but I have an error in my code that can only
Basic question - I have a UIWebView with a pdf document loaded. How can
Basic question here, can I put String variables into a position in a string

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.