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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:47:00+00:00 2026-06-09T19:47:00+00:00

Assuming I have a sample configuration XML file that is the following: <?xml version=1.0?>

  • 0

Assuming I have a sample configuration XML file that is the following:

<?xml version="1.0"?>
<note> 
    <to>Tove</to> 
    <infoaboutauthor>
      <nestedprofile>
           <aboutme> 
               <gco:CharacterString>I am a 10th grader who likes to play ball.</gco:CharacterString> 
          </aboutme>
      </nestedprofile>
    </infoaboutauthor>
    <date>
        <info_date>
            <date>
               <gco:Date>2003-06-13</gco:Date>
            </date>
            <datetype>
                <datetype attribute="Value">
                </datetype>
            </datetype>
        </info_date>
    </date>
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
  </note>

In python (tried using ElementTree, not sure if its the best) I would like to get certain values for certain tags. I have tried:

with open('testfile.xml', 'rt') as f:
    tree = ElementTree.parse(f)
print 'Parsing'
root = tree.getroot()
listofelements = root_elem.findall('gco:CharacterString')    
for elementfound in listofelements:
    print elementfound.text

In the code I use above, it appears to not work when I have the colon as I get the following error:

SyntaxError: prefix 'gco' not found in prefix map

My goal is to

  1. get the text in the “2003-06-13” tag
  2. the text in the “aboutme” tag

What is the best way to accomplish this? Is there some way to look up “gco:CharacterString” where parent is equal to “aboutme”? Or is there some convenient way to get it into a dict where I can go mydict['note']['to']['nestedprofile']['aboutme']?

Note: The “gco:” prefix is something that I have to deal with that is part of the xml. If elementtree is not appropriate for this, that is okay.

  • 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-09T19:47:02+00:00Added an answer on June 9, 2026 at 7:47 pm

    Firstly, your XML is broken. the - in line 2 is breaking the parser. Also I don’t think it likes the gco:s. Can you possibly use some other XML configuration? Or is this automatically generated by something out of your control?

    So here’s what the XML needs to look like for this to work with Python:

    <?xml version="1.0"?>
    <note>
        <to>Tove</to>
        <infoaboutauthor>
          <nestedprofile>
               <aboutme>
                   <CharacterString>I am a 10th grader who likes to play ball.</CharacterString>
              </aboutme>
          </nestedprofile>
        </infoaboutauthor>
        <date>
            <info_date>
                <date>
                   <Date>2003-06-13</Date>
                </date>
                <datetype>
                    <datetype attribute="Value">
                    </datetype>
                </datetype>
            </info_date>
        </date>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
      </note>
    

    And here’s the code to accomplish your two goals:

    # Get the element tree from the file name and not a file object
    tree = ElementTree.parse('config.xml')
    
    # Get the root of the tree
    root = tree.getroot()
    
    # To get the 'Date' tag and print its text
    date_tag = root.find('date').find('info_date').find('date').find('Date')
    print date_tag.text
    
    # Get the `aboutme` tag and print its text
    about_me_tag = root.find('infoaboutauthor').find('nestedprofile').find('aboutme').find('CharacterString')
    print about_me_tag.text
    

    UPDATE

    As far as dealing with the “gco:”s goes, you could do something like this:

    def replace_in_config(old, new):
        with open('config.xml', 'r') as f:
            text = f.read()
    
        with open('config.xml', 'w') as f:
            f.write(text.replace(old, new))
    

    Then before you do the above XML operations run:

    replace_in_config('gco:', '_stripped')
    

    Then after the XMl operations are done (of course you will need to account for the fact that the gco:Date tag is now stripped_Date as is the CharacterString tag), run this:

    replace_in_config('_stripped', 'gco:')
    

    This will preserve the original format and allow you to parse it with etree.

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

Sidebar

Related Questions

I have an XML file that I need to transform using an XSL script.
Assuming that I have the following T-SQL code: SELECT * FROM Foo f INNER
Assuming I have a ASP.NET MVC 3 application that runs in a web farm
assuming I have the following array: views = [ { :user_id => 1, :viewed_at
Assuming you have only the URL to a file (hosted on the same server
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I have a table that has multiple duplicate records as the following: ID Title
In my JS I have the following ajax call, that bind the resulting json
Assuming I have an array of doubles, what's a good algorithm to sample this
Assuming I have a project that is: using UINavigationController that has the (a) main

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.