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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:51+00:00 2026-05-23T15:24:51+00:00

i would like modify text of xml tag value i have used an xml

  • 0

i would like modify text of xml tag value i have used an xml as follows

  <note>
       <to>Tove</to> 
       <from>Jani</from> 
       <heading>Reminder</heading> 
       <body>Don't forget me this weekend!</body> 
 </note>

here i would like to change the from name Jani as prasad.how can i chage that by using java code

i have written a java code as follows

 try{
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
     Document doc = docBuilder.parse(new File("/mnt/sdcard/one.xml"));

    //Get the staff element by tag name directly
     Node nodes = doc.getElementsByTagName("note").item(0);
    //loop the staff child node
     NodeList list = nodes.getChildNodes();

     for (int i =0; i<list.getLength();i++){
         Node node = list.item(i);

         //get the salary element, and update the value
         if("from".equals(nodes.getNodeName())){
             node.setNodeValue("prasad");

             TransformerFactory factory = TransformerFactory.newInstance();
             Transformer transformer = factory.newTransformer();

             DOMSource source = new DOMSource(doc);
             StreamResult result = new StreamResult(new File("/mnt/sdcard/one.xml"));
             transformer.transform(source, result);


         }
     }

}
    catch (Exception e) {
        e.printStackTrace();
    }
  • 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-23T15:24:52+00:00Added an answer on May 23, 2026 at 3:24 pm

    There is problem in your code: “Jani” is a text node with parent element node “from”. So you should change value of the text node.

    ...
         for (int i =0; i<list.getLength();i++) {
             Node node = list.item(i);
    
             //get the salary element, and update the value
             if("from".equals(node.getNodeName())){
                 Text text = (Text) ((Element) node).getChildNodes().item(0);
                 text.setNodeValue("prasad");
             }
         }
    
         TransformerFactory factory = TransformerFactory.newInstance();
         Transformer transformer = factory.newTransformer();
    
         DOMSource source = new DOMSource(doc);
         StreamResult result = new StreamResult(new File("/mnt/sdcard/one.xml"));
         transformer.transform(source, result);
    ...
    

    And I think RegExp would be much easier for this task

    "your xml as string".replaceAll("<from>.*?</from>", "<from>prasad</from>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a String which I would like to modify in some way. For
I would like to have a GtkTextView in my (Python) program which shows text
I would like to have the visual representation of an HTML5 <textarea> tag indicate
I would like to modify a Spotlight metadata attribute of a file within my
I would like to modify the string in a .h file with NAnt before
I would like to know how to modify the below code to strip =20
I would like to have a reference for the pros and cons of using
Let's say I have an empty XML file like so: <root></root> And I want
I would like to have a html file with JavaScript, which (file) is able
I would like to know how I can update a part of the text

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.