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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:15:42+00:00 2026-05-27T05:15:42+00:00

So i am trying to do this. Add new root element and wrap old

  • 0

So i am trying to do this. Add new root element and wrap old one in it.

Given this as starting condition

// this part uses SAXParser
org.w3c.com.Document = xmlSrc.parse(is); // *is* is InputStream 

The initial condition is not really negotiable but I am open to hear comments there too

So given this xml file

<?xml version="1.0" encoding="UTF-8"?>
<root1>
   <elem>...</elem>
</root1>

I need in Java to generate an InputStream that will contain xml file in it of this format

<?xml version="1.0" encoding="UTF-8"?>
<newroot>
   <root1>
       <elem>...</elem>
   </root1>
</newroot>

Stored in some InputStream isNewXML

I am curious what is the best way to go about doing this.
I am new to Java and java has billion ways to do the same thing so out in dark which would be the best

  • 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-27T05:15:42+00:00Added an answer on May 27, 2026 at 5:15 am

    Using your example input, this creates the requested output. Ideally, you would handle exceptions and close inputstreams, output streams, and writers properly:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    
    public class XmlTest
    {
    
        public static void main(String[] args) throws Exception
        {
            InputStream is = new FileInputStream("test.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
    
            Document oldDoc = builder.parse(is);
            Node oldRoot = oldDoc.getDocumentElement();
            Document newDoc = builder.newDocument();
            Element newRoot = newDoc.createElement("newroot");
            newDoc.appendChild(newRoot);
            newRoot.appendChild(newDoc.importNode(oldRoot, true));
    
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DOMSource domSource = new DOMSource(newDoc);
            Writer writer = new OutputStreamWriter(out);
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
            writer.flush();
    
            InputStream isNewXML = new ByteArrayInputStream(out.toByteArray());
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a simple answer, but I'm trying to add a dom-created element
Given this code (C#, .NET 3.5 SP1): var doc = new XmlDocument(); doc.LoadXml(<?xml version=\1.0\?><root>
I am trying this in my Form Load Event cmdCancel.Attributes.Add(onClick, document.forms[0].reset();return false;) but it
I'm trying to add some share this javascript in between the head tags of
I'm trying add a tab to my web page that looks like this: Using
I'm trying to add parameters to an objectDataSource at runtime like this: Parameter objCustomerParameter
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
I am trying to add an KeyPress event in a textbox (WinForm) this.textBox1.KeyPress +=
I'm trying to add a publisher policy file to the gac as per this
I'm trying to get the video data from this youtube playlist feed and add

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.