Using StAX to Clone an XML Document

Using StAX to Clone an XML Document

To clone an XML document at runtime, use the StAX XMLEventReader and XMLEventWriter classes like this:

//AirWings_order.xml - the original XML in this example//AirWings_order_clone.xml - the clone of AirWings_order.xmlimport*;import*;public class StAXBasicEventWriter_clone{   public StAXBasicEventWriter_clone(){}   public static void main(String[] args)   {      XMLInputFactory XMLif=null;      XMLOutputFactory XMLof=null;      XMLEventReader XMLer=null;      XMLEventWriter XMLew=null;                   System.setProperty("",         "");           System.setProperty("",         "");      //get the XMLInputFactory and XMLOutputFactory objects      XMLif=XMLInputFactory.newInstance();      XMLof=XMLOutputFactory.newInstance();                                         //get the XMLEventReader and XMLEventWriter objects      try{         XMLer=XMLif.createXMLEventReader                       ("file:///C://Data_Local//xml//docs//",             new FileReader             ("C://Data_local//xml//docs//AirWings_order.xml"));         XMLew=XMLof.createXMLEventWriter(            new FileWriter            ("C://Data_local//xml//docs//AirWings_order_clone.xml"));      }catch( e)         {System.out.println(e.getMessage());              }catch( e)            {System.out.println(e.getMessage());}                                      //generate the XML document      try{         XMLew.add(XMLer);         //clean up         XMLew.close();         XMLer.close();      }catch( e)         {System.out.println(e.getMessage());}              }       }
See also  How to Avoid Money Transfer Scams  

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist