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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:49:55+00:00 2026-05-26T02:49:55+00:00

How do I replace text in an XML document using Java? Source: <body> <title>Home

  • 0

How do I replace text in an XML document using Java?

Source:

<body>
<title>Home Owners Agreement</title>
<p>The <b>good</b> thing about a Home Owners Agreement is that...</p>
</body>

Desired output:

<body>
<title>Home Owners Agreement</title>
<p>The <b>good</b> thing about a HOA is that...</p>
</body>

I only want text in <p> tags to be replaced. I tried the following:

replaceText(string term, string replaceWith, org.w3c.dom.Node p){
       p.setTextContent(p.getTextContent().replace(term, replaceWith));

}

The problem with the above code is that all the child nodes of p get lost.

  • 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-26T02:49:56+00:00Added an answer on May 26, 2026 at 2:49 am

    Okay, I figured out the solution.

    The key to this is that you don’t want to replace the text of the actual node. There is a actually a child representation of just the text. I was able to accomplish what I needed with this code:

    private static void replace(Node root){
        if (root.getNodeType() == root.TEXT_NODE){
            root.setTextContent(root.getTextContent().replace("Home Owners Agreement", "HMO"));
        }
        for (int i = 0; i < root.getChildNodes().getLength(); i++){ 
            outputTextOfNode(root.getChildNodes().item(i));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a snippet of text from the source xml document: Great Lakes. The
I know how to replace text in a string. But that's using keyboard (ASCII)
I've got the following code: Regex.Replace(text, words.ToString(), <dfn title=\ + this.FindDefinition($0) + \>$0</dfn>, RegexOptions.IgnoreCase
I am using the code below to replace text inside a div. But I
I need to replace some text in a JNLP file using a DOS batch
I have an input XML document something like this: <text> <p> Download the software
Using the Open XML SDK I've been successful in programatically finding bookmarks or text
I'm using the new System.Xml.Linq to create HTML documents (Yes, I know about HtmlDocument,
I am writing a converter from XML&MathML to MS Word document. I'm using MFC
I need to parse 70mb data with Java and I've currently a xml document

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.