add cdata section to the a xml and also read it

hi

i was facing a problem for sometime as to how to add and read CDATA sections to xml using java. Finally did it so thought of sharing it....

-uv

/**

* this will add cdata section to the comments tag in a xml and also read it back

* java 1.5

*/

package misc;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.CDATASection;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.xml.sax.SAXException;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

/**

* @author uddalokv

*

*/

public class TestCDATAXML {

public static String write(String filename1) throws Exception {

// TODO Auto-generated method stub

Document doc = loadXml(filename1);

CDATASection cdas = doc.createCDATASection("THIS CAN CONTAIN SPECIAL CHARACTERS $ \\ / ? * ( ) @ # $ ! < >");

doc.getElementsByTagName("COMMENTS").item(0).appendChild(cdas);

String filename = "d:/test/CDATA" + System.nanoTime() + ".xml";

FileOutputStream fos = new FileOutputStream(filename);

// XERCES 1 or 2 additionnal classes.

OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);

of.setIndent(1);

of.setIndenting(true);

//of.setDoctype(null,"users.dtd");

XMLSerializer serializer = new XMLSerializer(fos, of);

// As a DOM Serializer

serializer.asDOMSerializer();

serializer.serialize(doc.getDocumentElement());

fos.close();

return filename;

}

public static void main(String[] args) throws Exception {

read(write("d:/test/abc.xml"));

}

public static void read(String filename) {

Document doc = loadXml(filename);

Node node = doc.getElementsByTagName("COMMENTS").item(0);

for (int i = 0; i < node.getChildNodes().getLength(); i++)

System.out.println("Node Value:" + node.getChildNodes().item(i).getNodeValue());

}

public static Document loadXml(String argFileName) {

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File(argFileName));

return doc;

}

catch (ParserConfigurationException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

catch (SAXException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

return null;

}

}

[2987 byte] By [uddaloka] at [2007-11-27 9:05:46]
# 1

/**

* this will add cdata section to the comments tag in a xml and also read it back

* java 1.5

*/

package misc;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.CDATASection;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.xml.sax.SAXException;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

/**

* @author uddalokv

*

*/

public class TestCDATAXML {

public static String write(String filename1) throws Exception {

// TODO Auto-generated method stub

Document doc = loadXml(filename1);

CDATASection cdas = doc.createCDATASection("THIS CAN CONTAIN SPECIAL CHARACTERS $ \\ / ? * ( ) @ # $ ! < >");

doc.getElementsByTagName("COMMENTS").item(0).appendChild(cdas);

String filename = "d:/test/CDATA" + System.nanoTime() + ".xml";

FileOutputStream fos = new FileOutputStream(filename);

// XERCES 1 or 2 additionnal classes.

OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);

of.setIndent(1);

of.setIndenting(true);

//of.setDoctype(null,"users.dtd");

XMLSerializer serializer = new XMLSerializer(fos, of);

// As a DOM Serializer

serializer.asDOMSerializer();

serializer.serialize(doc.getDocumentElement());

fos.close();

return filename;

}

public static void main(String[] args) throws Exception {

read(write("d:/test/abc.xml"));

}

public static void read(String filename) {

Document doc = loadXml(filename);

Node node = doc.getElementsByTagName("COMMENTS").item(0);

for (int i = 0; i < node.getChildNodes().getLength(); i++)

System.out.println("Node Value:" + node.getChildNodes().item(i).getNodeValue());

}

public static Document loadXml(String argFileName) {

try {

DocumentBuilderFactory docBuilderFactory =DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File(argFileName));

return doc;

}

catch (ParserConfigurationException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

catch (SAXException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

return null;

}

}

Just to make it more presentable :)

Message was edited by:

lem@phil

lem@phila at 2007-7-12 21:40:22 > top of Java-index,Java Essentials,Java Programming...