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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:22:29+00:00 2026-05-16T22:22:29+00:00

I need to avoid creating double branches in an xml tree when parsing a

  • 0

I need to avoid creating double branches in an xml tree when parsing a text file. Let’s say the textfile is as follows (the order of lines is random):

branch1:branch11:message11
branch1:branch12:message12
branch2:branch21:message21
branch2:branch22:message22

So the resulting xml tree should have a root with two branches. Both of those branches have two subbranches. The Python code I use to parse this textfile is as follows:

import string
fh = open ('xmlbasic.txt', 'r')
allLines = fh.readlines()
fh.close()
import xml.etree.ElementTree as ET
root = ET.Element('root')

for line in allLines:
   tempv = line.split(':')
   branch1 = ET.SubElement(root, tempv[0])
   branch2 = ET.SubElement(branch1, tempv[1])
   branch2.text = tempv[2]

tree = ET.ElementTree(root)
tree.write('xmlbasictree.xml')

The problem with this code is, that a branch in xml tree is created with each line from the textfile.

Any suggestions how to avoid creating another branch in xml tree if a branch with this name exists already?

  • 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-05-16T22:22:29+00:00Added an answer on May 16, 2026 at 10:22 pm
    with open("xmlbasic.txt") as lines_file:
        lines = lines_file.read()
    
    import xml.etree.ElementTree as ET
    
    root = ET.Element('root')
    
    for line in lines:
        head, subhead, tail = line.split(":")
    
        head_branch = root.find(head)
        if not head_branch:
            head_branch = ET.SubElement(root, head)
    
        subhead_branch = head_branch.find(subhead)
        if not subhead_branch:
            subhead_branch = ET.SubElement(branch1, subhead)
    
        subhead_branch.text = tail
    
    tree = ET.ElementTree(root)
    ET.dump(tree)
    

    The logic is simple — you already stated it in your question! You merely need to check whether a branch already exists in the tree before creating it.

    Note that this is likely inefficient, since you are searching up to the entire tree for each line. This is because ElementTree is not designed for uniqueness.


    If you require speed (which you may not, especially for smallish trees!), a more efficient way would be to use a defaultdict to store the tree structure before converting it to an ElementTree.

    import collections
    import xml.etree.ElementTree as ET
    
    with open("xmlbasic.txt") as lines_file:
        lines = lines_file.read()
    
    root_dict = collections.defaultdict( dict )
    for line in lines:
        head, subhead, tail = line.split(":")
        root_dict[head][subhead] = tail
    
    root = ET.Element('root')
    for head, branch in root_dict.items():
        head_element = ET.SubElement(root, head)
        for subhead, tail in branch.items():
            ET.SubElement(head_element,subhead).text = tail
    
    tree = ET.ElementTree(root)
    ET.dump(tree)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.