tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 XML > XOM > Serialize CDATA

Serialize CDATA 

XOM (XML Object Model) is a tree based java API for processing XML by taking the best ideas from SAX and DOM. It is simple, fast and easy to use. This requires the library xom-1.2.7.jar to be in classpath. The following example shows serializing an XML document with complex character data.

File Name  :  
com/bethecoder/tutorials/xom/tests/SerializeCDATA.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.xom.tests;

import java.io.IOException;

import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
import nu.xom.Text;

public class SerializeCDATA {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {
    Document doc = new Document(new Element("scripts"));
    Element script = new Element("script");
    script.addAttribute(new Attribute("id""function_signum"));
    doc.getRootElement().appendChild(script);
    
    String jsFunction = "function signum(a) {" +
                "if (a > 0) {" 
                "  return 1;" 
                "} else if (a < 0) {" +
                "  return -1;" +
                "}" 
                "return 0;" +  
              "}";  
    
    Text cdata = new Text(jsFunction);
    script.appendChild(cdata);
    
    //Pretty print
    Serializer serializer = new Serializer(System.out, "UTF-8");
    serializer.setIndent(4);
    serializer.write(doc);
    serializer.flush();
  }

}
   

It gives the following output,
File Name  :  OUTPUT


                    Print

   



Books Recommendation

 
 
bl  br