package com.quark.aspose.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class StringAndDocument {
public static Document createNewDocument() throws ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
return builder.newDocument();
}
/**
* Create Document Object from string formatted xmlcontent.
*
* @param xmlContent
* , xml content in string format
* @return
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
*/
public static Document getDocumentFromString(final String xmlContent) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
return documentBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlContent)));
}
/**
* Create Document Object from input stream having xmlcontent.
*
* @param xmlContent
* , xml content in input stream
* @return
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
*/
public static Document getDocumentFromInputStream(final InputStream xmlContent) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
return documentBuilderFactory.newDocumentBuilder().parse(new InputSource(xmlContent));
}
/**
* Returns xml content present in document object in string format
*
* @param document
* @return
* @throws TransformerFactoryConfigurationError
* @throws TransformerException
*/
public static String getStringFromDocument(final Document document) throws TransformerFactoryConfigurationError, TransformerException {
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "no");
transformer.setOutputProperty("indent", "no");
transformer.setOutputProperty("encoding", "UTF_8");
transformer.transform(new DOMSource(document), new StreamResult(sw));
return sw.toString();
}
}
Help4J - Jar Search Engine. Easiest way to find jar and its source.
not working
ReplyDeleteWhat is not working. Can you please tell me what problem you are facing..
Delete