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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:49:31+00:00 2026-06-08T07:49:31+00:00

I have a file that contains the following type and structure of data: <data>

  • 0

I have a file that contains the following type and structure of data:

<data>
    <from>A</from>
    <to>B</to>
    <data>
        <name>EXAMPLE ONE</name>
        <info>
            <some_data>1</some_data>
            <more_data>2</more_data>
        </info>
        <random>
            <some_tag>
            </foobar>
            <foo>
                <bar />
           </foo>
        </random>
    </data>
    <data>
        <name>EXAMPLE TWO</name>
        <info>
            <some_data>3</some_data>
            <more_data>4</more_data>
        </info>
        <random>
            <some_tag>
            </foobar>
            <foo>
                <bar />
           </foo>
        </random>
   </data>
</data>
<data>
    <from>C</from>
    <to>D</to>
    <data>
        <name>EXAMPLE</name>
        <info>
            <some_data>1</some_data>
            <more_data>2</more_data>
        </info>
        <random>
            <some_tag>
            </foobar>
            <foo>
                <bar />
           </foo>
        </random>
    </data>
 </data>

The data continues in this exact structure in the file with the exception of the inner most <data>...</data> tags that can and is repeated n times, the data structure always starts with a <data> tag and then continues with the <from>...</from> and <to>...</to> tags.

What i want to do is to extract all the data between the outer most <data> tags with the <to> and <from> as a description of the data blocks. I of course also want to seperate the inner most <data> tags from each other and save this data in a way so that it’s clear that the outer most data is related to the parent data.

I don’t have a exact idea of how i want to save the data so any examples is appreciated!

I’m testing this with the Python module BeautifulSoup and have searched and read a lot of examples here but haven’t found anything that can point me into the correct direction.

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-08T07:49:34+00:00Added an answer on June 8, 2026 at 7:49 am

    The fact that you are doubling the tag name <data> as the container of your records as well as an element inside creates problems. BeautifulSoup is forgiving of such issues and here is a way you may want to use in case you cannot go back and change the XML structure.

    Assign the data to a variable. This may be read in from text file, of course:

    data = '''<data>
        <from>A</from>
        <to>B</to>
        <data>
            <name>EXAMPLE ONE</name>
            <info>
                <some_data>1</some_data>
                <more_data>2</more_data>
            </info>
            <random>
                <some_tag>
                </foobar>
                <foo>
                    <bar />
               </foo>
            </random>
        </data>
        <data>
            <name>EXAMPLE TWO</name>
            <info>
                <some_data>3</some_data>
                <more_data>4</more_data>
            </info>
            <random>
                <some_tag>
                </foobar>
                <foo>
                    <bar />
               </foo>
            </random>
       </data>
    </data>
    <data>
        <from>C</from>
        <to>D</to>
        <data>
            <name>EXAMPLE</name>
            <info>
                <some_data>1</some_data>
                <more_data>2</more_data>
            </info>
            <random>
                <some_tag>
                </foobar>
                <foo>
                    <bar />
               </foo>
            </random>
        </data>
     </data>'''
    

    Process the data:

    from BeautifulSoup import BeautifulSoup
    from pprint import pprint
    
    store = {}
    key = ()
    
    soup = BeautifulSoup(data)
    
    recs = soup.findAll('data')
    
    for rec in recs:
        if rec.find('from'):
            key = (rec.find('from').text, 
                   rec.find('to').text)
        else:
            item = {}
            item['name'] = rec.find('name').text
            item['some_data'] = rec.find('info').find('some_data').text
            item['more_data'] = rec.find('info').find('more_data').text
            if store.has_key(key):
                store[key].append(item)
            else:
                store[key] = [ item ]
    
    pprint(store)
    

    And the result with this dummy data:

    {(u'A', u'B'): [{'more_data': u'2',
                     'name': u'EXAMPLE ONE',
                     'some_data': u'1'},
                    {'more_data': u'4',
                     'name': u'EXAMPLE TWO',
                     'some_data': u'3'}],
     (u'C', u'D'): [{'more_data': u'2', 'name': u'EXAMPLE', 'some_data': u'1'}]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that contains the following in a csv file; country,region,city,postalCode,metroCode,areaCode I
I currently have a .command file for Mac that contains the following: for f
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have Text file that contains data separated with a comma , . How
I have a class header file called Grid.h that contains the following 2 private
I have following array that contains information on each file.but it is in single
I have an XSD file that contains the following... <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema elementFormDefault=qualified id=OTA2003A targetNamespace=http://www.opentravel.org/OTA/2003/05
I have following type of file contains in xml format <?xml version=1.0 encoding=UTF-8?> <root>
I have a cpp file that contains the following: char const* types[] = {
I have a File that COntains Strings in This Format: ACHMU)][2:s,161,(ACH Payment Sys Menus

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.