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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:37:32+00:00 2026-06-13T05:37:32+00:00

We have an XML document that has a tag we wish to alter: …<version>1.0</version>…

  • 0

We have an XML document that has a tag we wish to alter:

...<version>1.0</version>...

It’s buried deep in the XML file, but we’re successfully able to use Beautiful Soup to replace its contents with a command-line parameter.

The problem is that after modifying the tree, we need to write back to the file we read it from. But, we want to maintain the original formatting of the document. When I use:

fileForWriting = open(myXmlFile, 'w')
fileForWriting.write(soup.prettify())

The prettify() call breaks the formatting, and I end up with:

<version>
 1.0
</version>

Is there any way to maintain the original formatting of the XML document, while replacing that single tag text?

Note: Using simply:

fileForWriting.write(str(soup))

Keeps the text and tags on the same line, but eliminates the indents and extra newlines that had been human-added for readability. Close, but no cigar.

By request, the entire script:

from BeautifulSoup import BeautifulSoup as bs

import sys

xmlFile = sys.argv[1:][0]
version = sys.argv[1:][1]

fileForReading = open(xmlFile, 'r')
xmlString = fileForReading.read()
fileForReading.close()

soup = bs(xmlString)

soup.findAll('version')[1].contents[0].replaceWith(version)

fileForWriting = open(xmlFile, 'w')
fileForWriting.write(str(soup))
fileForWriting.close()

The script is then run using:

python myscript.py someFile.xml 1.2

  • 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-13T05:37:33+00:00Added an answer on June 13, 2026 at 5:37 am

    And if you use xml.elementtree, the tree.write(file) method replaces the CRLF by LF only, which also creates issues when trying to import the XML file into i.e. PyXB.

    The solution I found is to use ElementTree just to find what I have to replace. Then I do source_XML = 'new value'.join(source_XML.split('what you need to replace)) Finally a file.write(source_XML)

    it’s not nice, but it solves the issue. However, I do not mind about the indentations, so on this I can’t really say. I would only use pprint.pprint() whenever I need to print it.

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

Sidebar

Related Questions

I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an xml document that has a structure like so: <?xml version=1.0 encoding=iso-8859-1
I have an xml file I wish to transform using an xsl-document, but I
I have an XML file that has data in it, but one of the
I have an XML document that has a collection of objects. Each object has
I have a WIX XML document that contains 2,000+ file tags. I am trying
I have an XML document that has several <item> elements. Inside each, there might
I have a snippet of an XML file that looks like: <?xml version=1.0 encoding=utf-16?>
I have an XML document that has dates in standard ISO 8601 format. Like
I have a xml document that has a record set like this. <document> <row>

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.