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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:09:02+00:00 2026-06-18T23:09:02+00:00

My XML file test.xml contains the following tags <?xml version=1.0 encoding=ISO-8859-1?> <AppName> <out>This is

  • 0

My XML file test.xml contains the following tags

<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <out>This is a sample output with <test>default</test> text </out>
<AppName>

I have written a python code which does the following till now:

from xml.dom.minidom import parseString
list = {'test':'example'}
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
if (len(dom.getElementsByTagName('out'))!=0):
    xmlTag = dom.getElementsByTagName('out')[0].toxml()
    out = xmlTag.replace('<out>','').replace('</out>','')
    print out

The output of the following program is This is a sample output with <test>default</test> text

You will also notice i have a list with list = {'test':'example'} defined.

I want to check if in the out there is a tag which is listed in the list, will be replaced with the corresponding value, else the default value.

In this case, the output should be:

This is a sample output with example text

  • 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-18T23:09:03+00:00Added an answer on June 18, 2026 at 11:09 pm

    This will do more or less what you want:

    from xml.dom.minidom import parseString, getDOMImplementation
    
    test_xml = '''<?xml version="1.0" encoding="ISO-8859-1"?>
    <AppName>
        <out>This is a sample output with <test>default</test> text </out>
    </AppName>'''
    
    replacements = {'test':'example'}
    dom = parseString(test_xml)
    if (len(dom.getElementsByTagName('out'))!=0):
        xmlTag = dom.getElementsByTagName('out')[0]
        children =  xmlTag.childNodes
        text = ""
        for c in children:
            if c.nodeType == c.TEXT_NODE:
                text += c.data
            else:
                if c.nodeName in replacements.keys():
                    text += replacements[c.nodeName]
                else: # not text, nor a listed tag
                    text += c.toxml()
        print text
    

    Notice that I used replacements rather than list. In python terms, it’s a dictionary, not a list, so that’s a confusing name. It’s also a builtin function, so you should avoid using it as a name.

    If you want a dom object rather than just text, you’ll need to take a different approach.

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

Sidebar

Related Questions

My XML file test.xml contains the following tags <?xml version=1.0 encoding=ISO-8859-1?> <AppName> <author>Subho Halder</author>
I have the following xml file: <?xml version=1.0 encoding=utf-8?> <root> <phrase id=test><![CDATA[test]]></phrase> <phrase id=test0><![CDATA[test0]]></phrase>
I have the following XML file: <xml version=1.0 encoding=utf-8?> <Data> <Parameter1>1</Parameter1> </Data> I want
I've got the following XML file: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <invoice xmlns=http://www.jemos.co.uk/xsds/experiments> <header date=2012-12-27T13:17:03.652Z
I have the following plist file at ~/Library/LaunchAgents: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC
I have an xml file that contains its element like <ab:test>Str</ab:test> When I am
I have a file test.xml with the following content : <body> <content> <p>Lorem ipsum
I have this test code that just saves an XML file to a folder.
This is an example XML file that would come from the Android Client. <test>
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property

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.