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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:42:53+00:00 2026-05-20T03:42:53+00:00

Say I have a Java String which has xml data like so: String content

  • 0

Say I have a Java String which has xml data like so:

String content = "<abc> Hello <mark> World </mark> </abc>";

Now, I seek to render this String as text on a web page and hightlight/mark the word “World”. The tag “abc” could change dynamically, so is there a way I can rename the outermost xml tag in a String using Java ?

I would like to convert the above String to the format shown below:

String content = "<i> Hello <mark> World </mark> </i>";

Now, I could use the new String to set html content and display the text in italics and highlight the word World.

Thanks,
Sony

PS: I am using xquery over files in BaseX xml database. The String content is essentially a result of an xquery which uses ft:extract(), a function to extract full text search results.

  • 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-20T03:42:54+00:00Added an answer on May 20, 2026 at 3:42 am

    XML “parsing” with regexes can be cumbersome. If there is a possibility that your XML string can be more complicated than the one used in your example, you should consider processing it as a real XML node.

    String newName = "i";
    // parse String as DOM
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(content)));
    
    // modify DOM
    doc.renameNode(doc.getDocumentElement(), null, newName);
    

    This code assumes that the element to that needs to be renamed is always the outermost element, that is, the root element.

    Now the document is a DOM tree. It can be converted back to String object with a transformer.

    // output DOM as String
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter sw = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    String italicsContent = sw.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java string, which has a variable length. I need to put
Let's say I have an existing application written in Java which I wish to
Let's say I have a java program that makes an HTTP request on a
Let's say I have some Java code: public class SomeClass { static { private
Say I have a binary file (generated with Java) containing a 32 bit int
In Java, say you have a class that wraps an ArrayList (or any collection)
Are there any performance benchmarks for Mono compared to say Java in GNU/Linux? Have
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say we have the following method: private MyObject foo = new MyObject(); // and

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.