I am using below code to parse a XML file -
package com.kcs.xml; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class ParseXMLOld { public static void main(String[] args) { final String FILE_PATH="C:\\abc.xml"; File file=new File(FILE_PATH); ParseXMLOld pxo=new ParseXMLOld(); pxo.parseUTFXML(file); } public Document parseUTFXML(File file) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder=null; Document doc=null; try { docBuilder = docBuilderFactory.newDocumentBuilder(); InputStream inputStream= new FileInputStream(file); Reader reader = new InputStreamReader(inputStream,"UTF-16"); InputSource is = new InputSource(reader); is.setEncoding("UTF-16"); doc = docBuilder.parse(is); System.out.println("Done"); } catch(Exception e) { e.printStackTrace(); } finally { docBuilderFactory=null; docBuilder=null; } return doc; } }
Below is my XML file -
<?xml version="1.0" encoding="UTF-16"?> <Details> <Content> <id>1234ĄŁ€$˘</id> <Valid_From_Date>2013-01-01</Valid_From_Date> <Valid_To_Date>9999-12-31</Valid_To_Date> <Company>1210</Company> <Description>2nd Life Transaction</Description> </Content> <Totals> <Count>1</Count> </Totals> </Details>
I am getting below error -
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(DOMParser.java:251)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(DocumentBuilderImpl.java:300)
at com.kcs.xml.ParseXMLOld.parseUTFXML(ParseXMLOld.ja va:33)
at com.kcs.xml.ParseXMLOld.main(ParseXMLOld.java:19)
Please help me.
Thanks,
Kartic
--- Update ---
Is there anyone who can help me?