Skip to main content

How to create new XML file using retreived XML content by using SAX API?

No replies
jimranjava
Offline
Joined: 2008-11-07
Points: 0

hi all,

* How to create new XML file using retreived XML content by using SAX ?

* I have tried my level best, but output is coming invalid format, my code is follows,

XMLFileParser.java class :-

import java.io.StringReader;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMResult;

import javax.xml.transform.sax.SAXSource;

import javax.xml.transform.sax.SAXTransformerFactory;

import javax.xml.transform.sax.TransformerHandler;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.xml.sax.Attributes;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.XMLFilterImpl;

public class PdfParser extends XMLFilterImpl {

private TransformerHandler handler;

Document meta_data;

private StringWriter meta_data_text = new StringWriter();

public void startDocument() throws SAXException {

}

void startValidation() throws SAXException {

StreamResult streamResult = new StreamResult(meta_data_text);

SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

try

{

handler = factory.newTransformerHandler();

Transformer transformer = handler.getTransformer();

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

handler.setResult(streamResult);

handler.startDocument();

}

catch (TransformerConfigurationException tce)

{

System.out.println("Error during the parse :"+ tce.getMessageAndLocation());

}

super.startDocument();

}

public void startElement(String namespaceURI, String localName,

String qualifiedName, Attributes atts) throws SAXException {

handler.startElement(namespaceURI, localName, qualifiedName, atts);

super.startElement(namespaceURI, localName, qualifiedName, atts);

}

public void characters(char[] text, int start, int length)

throws SAXException {

handler.characters(text, start, length);

super.characters(text, start, length);

}

public void endElement(String namespaceURI, String localName,

String qualifiedName) throws SAXException {

super.endElement("", localName, qualifiedName);

handler.endElement("", localName, qualifiedName);

}

public void endDocument() throws SAXException {

}

void endValidation() throws SAXException {

handler.endDocument();

try {

TransformerFactory transfactory = TransformerFactory.newInstance();

Transformer trans = transfactory.newTransformer();

SAXSource sax_source = new SAXSource(new InputSource(new StringReader(meta_data_text.toString())));

DOMResult dom_result = new DOMResult();

trans.transform(sax_source, dom_result);

meta_data = (Document) dom_result.getNode();

System.out.println(meta_data_text);

}

catch (TransformerConfigurationException tce) {

System.out.println("Error occurs during the parse :"+ tce.getMessageAndLocation());

}

catch (TransformerException te) {

System.out.println("Error in result transformation :"+ te.getMessageAndLocation());

}

}

}

CreateXMLFile.java class :-

Sax.startDocument();
Sax.startValidation();
Sax.startElement("", "pdf", "pdf", new AttributesImpl());
Sax.startElement("", "basic-metadata", "basic-metadata", new AttributesImpl());
String xmp_str = new String(meta_data.getByteArray(),"UTF8");
char[] xmp_arr = xmp_str.toCharArray();
Sax.characters(xmp_arr, 0, xmp_arr.length);
Sax.endElement("", "pdf", "pdf");
Sax.endValidation();
Sax.endDocument();
Sax.endElement("", "basic-metadata", "basic-metadata");

* In CreateXMLFile.java
class, I have retreived the xml content in the meta_data object, after that i have converted into character array and this will be sends to SAX

* In this case , the XML file created successfully but the retreived XML content added as an text in between basic-metadata Element, that is, retreived XML content
is not an XML type text, it just an Normal text Why that ?

* Please help me what is the problem in my code?

Cheers,
JavaImran