dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Advanced
Apr 29, 2008

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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.xml

import javax.xml.stream.*;
import java.io.*;

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("javax.xml.stream.XMLInputFactory",
         "com.sun.xml.stream.ZephyrParserFactory");     
      System.setProperty("javax.xml.stream.XMLOutputFactory",
         "com.sun.xml.stream.ZephyrWriterFactory");

      //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(java.io.IOException e)
         {System.out.println(e.getMessage());        
      }catch(javax.xml.stream.XMLStreamException e)   
         {System.out.println(e.getMessage());}
                                
      //generate the XML document
      try{
         XMLew.add(XMLer);

         //clean up
         XMLew.close();
         XMLer.close();
      }catch(javax.xml.stream.XMLStreamException e)
         {System.out.println(e.getMessage());}           
   }       
}
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date