hi
actually i am having following requirement
i have to download pdf from server and save it as a xml in local machine
i neeed to run one offline tool to read xml and take i/p from end user
later I upload that file in to server
I changed my code but i am getting following error
[Fatal Error] :1:1: Content is not allowed in prolog.
Exception in
thread "main" org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at parser.Writingxml.bytesToXml(Writingxml.java:27)
at parser.Writingxml.main(Writingxml.java:36)
my code is
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] test=TestingPdf.getRequestBufferAsBytes(request);
byte[]test5=Base64.encodeBase64(test);
org.w3c.dom.Document test4 = null;
try {
test4 = TestingPdf.bytesToXml(test5);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (org.xml.sax.SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = null;
try {
trans = transfac.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(test4);
try {
trans.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xmlString = sw.toString();
//print xml
System.out.println("Here's the xml:\n\n" + xmlString);
}
private static org.w3c.dom.Document bytesToXml(byte[] test5) throws ParserConfigurationException, org.xml.sax.SAXException, IOException {
// TODO Auto-generated method stub
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(test5));
}
public static byte[] getRequestBufferAsBytes(HttpServletRequest request) throws IOException, ServletException
{
ServletInputStream oInput = request.getInputStream();
long nContentLength = request.getContentLength();
@SuppressWarnings("unused")
String contentType = request.getContentType();
if(nContentLength <= 0L)
return null;
byte cContent[] = new byte[(int)nContentLength];
int nRead = 0;
int nToRead = (int)nContentLength;
int nBlkSize = 512;
byte cTemp[] = new byte[512];
do {
int n = 0;
int i = 0;
if(nToRead - nRead < 512)
nBlkSize = nToRead - nRead;
n = oInput.read(cTemp, 0, nBlkSize);
for(i = 0; i < n; i++)
cContent[i + nRead] = cTemp[i];
nRead += i;
} while(nRead < nToRead);
@SuppressWarnings("unused")
Long nBytesRead = new Long(nRead);
return cContent;
}