Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Parsing error for UTF-16 file.

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing error for UTF-16 file.

    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?
    Last edited by jps; August 6th, 2013 at 12:55 PM. Reason: code tags


  2. #2
    Junior Member ChainDev's Avatar
    Join Date
    Aug 2013
    Location
    Home :)
    Posts
    10
    My Mood
    Inspired
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Parsing error for UTF-16 file.

    Forum > Java Programming Forums Lobby > Member Introductions > Parsing error for UTF-16 file.

    I think that this is really the wrong place to ask questions.

  3. The Following User Says Thank You to ChainDev For This Useful Post:

    jps (August 6th, 2013)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Parsing error for UTF-16 file.

    Welcome kcs87
    Please use code tags when posting code, details can be found on the Announcements page
    Thread moved from Member Introductions

  5. #4
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Parsing error for UTF-16 file.

    He did use code tags.

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Parsing error for UTF-16 file.

    Quote Originally Posted by llowe29 View Post
    He did use code tags.
    I'm not sure what point you wish to make, but jps was nice enough to edit the post for them to add the tags.

  7. The Following User Says Thank You to copeg For This Useful Post:

    jps (August 6th, 2013)

Similar Threads

  1. RDF File parsing
    By informtopradip in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 20th, 2011, 07:05 AM
  2. message box for error in parsing date
    By johnyjj2 in forum Exceptions
    Replies: 1
    Last Post: November 27th, 2011, 02:36 PM
  3. HELP Error Parsing File
    By gerry123 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 23rd, 2011, 04:18 PM
  4. Convert file from any encoding to UTF-8
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 26th, 2011, 01:16 AM
  5. Change file encoding from ANSI to UTF-8
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 19th, 2011, 01:48 PM

Tags for this Thread