Use LSOutput and LSSerializer to Serialize a DOM Tree

Use LSOutput and LSSerializer to Serialize a DOM Tree

This tip shows you how to use the LSOutput/LSSerializer interfaces (from the DOM Load and Save API) for serialize a DOM tree. Extract the DOM tree with the following basic steps:

//AirWings_xml.xml - the XML document from this example//LSOutput_1.xml - the result of serializationimport javax.xml.parsers.*;import org.w3c.dom.*;import*;import*;class DOM{  DocumentBuilderFactory DBF=null;  DocumentBuilder DB=null;  Document D=null;  DOMImplementationLS DOMiLS=null;  FileOutputStream FOS=null;  public DOM(){}   public void StartSerialization(String XMLdoc)     {     try{        //create a DocumentBuilderFactory object        DBF=DocumentBuilderFactory.newInstance();               //create a DocumentBuilder object        DB=DBF.newDocumentBuilder();               //get the DOM tree        D=DB.parse(new File(XMLdoc));                      }catch(javax.xml.parsers.ParserConfigurationException e)           {System.out.println(e.getMessage());        }catch(org.xml.sax.SAXException e)           {System.out.println(e.getMessage());        }catch( e)           {System.out.println(e.getMessage());}               //testing the support for DOM Load and Save        if((D.getFeature("Core","3.0")!=null)&&             (D.getFeature("LS","3.0")!=null))           {                  DOMiLS=(DOMImplementationLS)(D.getImplementation()).                    getFeature("LS","3.0");           System.out.println("[Using DOM Load and Save]");           }else             {             System.out.println("[DOM Load and Save unsupported]");                         System.exit(0);             }         //get a LSOutput object        LSOutput LSO=DOMiLS.createLSOutput();            //setting the location for storing the result of serialization     try{        FOS=new FileOutputStream(                 "C://Data_Local//xml//docs//LS_output_1.xml");        LSO.setByteStream((OutputStream)FOS);                  }catch( e)          {System.err.println(e.getMessage());}            //get a LSSerializer object        LSSerializer LSS=DOMiLS.createLSSerializer();            //do the serialization        boolean ser=LSS.write(D,LSO);            //publish the result        if(ser)           System.out.println("[Serialization done!]");             else System.out.println("[Serialization failed!]");     try{        FOS.close();        }catch( e)          {System.out.println(e.getMessage());}     } }public class serializationExample{ public static void main(String[] args)  {  DOM t=new DOM();    t.StartSerialization("C://Data_Local//xml//docs//AirWings_xml.xml");  }}
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of