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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:07:08+00:00 2026-05-23T05:07:08+00:00

I have a XML document, i want to insert more number of tag. example

  • 0

I have a XML document, i want to insert more number of tag. example
<data>
<tag1>1St tag</tag1>
<tag2>2nd tag</tag2>
<tag3>NewTag</tag3>
<tag4>4th tag</tag4>
</data>

I have tried to insert the data, but whenever i insert the data the inserting element occurred at once

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
            domFactory.setIgnoringComments(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder(); 
            Document doc = builder.parse(new File("File.xml")); 

            NodeList nodes = doc.getElementsByTagName("tag4");

            Text a = doc.createTextNode("value"); 
            Element p = doc.createElement("tag3"); 
            p.appendChild(a); 



             for (int i = 0; i < nodes.getLength(); i++) {
                 nodes.item(i).getParentNode().insertBefore(p, nodes.item(i));
        }
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);

            String xmlOutput = result.getWriter().toString();
            System.out.println(xmlOutput);
        System.out.println("Done");

This code is adding new element but whenever i insert new tag it rewrite the new tag name into existing tag. How to rectify the issue. To be simple i want to insert more tag in XML file.

  • 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-23T05:07:08+00:00Added an answer on May 23, 2026 at 5:07 am
    // Add a text node to the element
    element.appendChild(doc.createTextNode("D"));
    
    // Add a text node to the beginning of the element
    element.insertBefore(doc.createTextNode("A"), element.getFirstChild());
    
    // Add a text node before the last child of the element
    element.insertBefore(doc.createTextNode("C"), element.getLastChild());
    
    // Add another element after the first child of the root element
    Element element2 = doc.createElement("item");
    element.insertBefore(element2, element.getFirstChild().getNextSibling());
    
    // Add a text node in front of the new item element
    element2.getParentNode().insertBefore(doc.createTextNode("B"), element2);
    

    Sample piece of code which will be helpful for you to add

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

Sidebar

Related Questions

I have an XML document, and I want to print the tag names and
I have an XML document and want to insert a new node at a
I have a xml document as string. I need to insert the data into
I have an XML document of the form: <RootElement xmlns=http://mynamespace> <MyElement> <ElementNumber>0</ElementNumber> <Data>mydata</Data> <MoreData>more
I have an XML document with un-namespaced elements, and I want to use XSLT
I have a given XML document (structure can not be changed) and want to
I have an XML document and I want to index it to a Lucene
I have one XML document which I want to store it inside ViewState so
I have a document which uses an XML namespace for which I want to
I have a reasonably complex XML document which I want to flatten down to

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.