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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:17:00+00:00 2026-05-24T20:17:00+00:00

I would like to include the encoding tag in an XML document using BeautifulSoup.BeautifulStoneSoup

  • 0

I would like to include the encoding tag in an XML document using BeautifulSoup.BeautifulStoneSoup, but I’m not sure how!

<?xml version="1.0" encoding="UTF-8"?>
<mytag>stuff</mytag>

It outputs the encoding tag when I read a document that already has it, but I’m making a new soup.

Thanks!

Edit: I’ll give an example of what I’m currently doing.

from BeautifulSoup import BeautifulStoneSoup, Tag
soup = BeautifulStoneSoup()
mytag = Tag(soup, 'mytag')
soup.append(mytag)

str(soup)
# '<mytag></mytag>'

soup.prettify() # No encoding given
# '<mytag>\n</mytag>'

soup.prettify(encoding='UTF-8')
# '<mytag>\n</mytag>' # Where's the encoding?

Even if I create the soup like BeautifulStoneSoup(fromEncoding='UTF-8'), there is still no <?xml?> tag.

Is there another way to get that tag without creating and passing the tag as a string directly, or is that the only way?

  • 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-24T20:17:02+00:00Added an answer on May 24, 2026 at 8:17 pm

    Do you mean something like this?

    from BeautifulSoup import BeautifulStoneSoup
    soup = BeautifulStoneSoup('<?xml version="1.0" encoding="UTF-8"?>')
    # make some more soup
    

    Or,

    soup = BeautifulStoneSoup()
    # make some more soup
    soup.insert(0, '<?xml version="1.0" encoding="UTF-8"?>')
    

    From the BeautifulSoup documentation:

    Beautiful Soup tries the following encodings, in order of priority, to turn your document into Unicode:

    • An encoding you pass in as the fromEncoding argument to the soup constructor.
    • An encoding discovered in the document itself: for instance, in an XML declaration or (for HTML documents) an http-equiv META tag. If Beautiful Soup finds this kind of encoding within the document, it parses the document again from the beginning and gives the new encoding a try. The only exception is if you explicitly specified an encoding, and that encoding actually worked: then it will ignore any encoding it finds in the document.
    • An encoding sniffed by looking at the first few bytes of the file. If an encoding is detected at this stage, it will be one of the UTF-* encodings, EBCDIC, or ASCII.
    • An encoding sniffed by the chardet library, if you have it installed.
    • UTF-8
    • Windows-1252

    Beautiful Soup will almost always guess right if it can make a guess at all. But for documents with no declarations and in strange encodings, it will often not be able to guess.

    N.B. item #2, which I read as: BeautifulSoup will use the encoding in the xml declaration automatically, if you don’t explicitly specify one with the fromEncoding argument. YMMV.

    There are other, potentially useful, unicode related examples in the earlier referenced documentation, as well.


    Edit: @TorelTwiddler, if there is another way to add an xml declaration using BeautifulSoup without passing the tag as a string directly, I am not aware of it.

    That said, consider the following:

    soup = BeautifulStoneSoup('<?xml version="1.0" encoding=""?>') # <- no encoding
    mytag = Tag(soup, 'mytag')
    soup.append(mytag)
    
    print str(soup)
    # "<?xml version='1.0' encoding='utf-8'?><mytag></mytag>" 
    # Wha!? :)
    print soup.prettify(encoding='euc-jp')
    # <?xml version='1.0' encoding='euc-jp'?>
    # <mytag>
    # </mytag>
    

    Perhaps that’ll help you get where you want to go.

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

Sidebar

Related Questions

I would like to include a different file depending on the version of GCC.
Given this XML: <?php $xml = <<<XML <?xml version=1.0 encoding=UTF-8 ?> <root> <project type=residential>
In my XSD, I have something similar to this: <?xml version=1.0 encoding=utf-8 ?> <schema
I would like to include a Google map in a Flash site I am
I would like to include cron tasks in my Capistrano deployment files instead of
I would like to include common resources into two of my applications. Rather than
I would like to include a textbox / richtextbox in which I would like
I would like to include libQtGui.so.4 libQtNetwork.so.4 and libQtCore.so.4 in the same directory as
I recently added CKEditor to my app and I would like to include my
I have some documentation written in OpenOffice, and I would like to include some

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.