7

I am getting the below error when i try to read XML file that has some japanese characters.

javax.xml.bind.PropertyException: jaxb.encoding
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.getProperty(AbstractUnmarshallerImpl.java:360)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getProperty(UnmarshallerImpl.java:423)
at com.jaxb.JAXBTest.main(JAXBTest.java:23)
enter code here

package com.jaxb;

import java.io.FileReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JAXBTest 
{
    public static void main(String args[])
    {

        try
        {
            JAXBContext context = JAXBContext.newInstance(com.pain.jaxb.ver2.Document.class);           
            Unmarshaller um = context.createUnmarshaller();         
            um.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");          
            com.pain.jaxb.ver2.Document PainTransferList2 = (com.pain.jaxb.ver2.Document) um.unmarshal(new FileReader("C:/WorkArea/JAXB/src/com/pain/messages/APXSEPAS_510812_1.XML"));         

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

}

Please advice.

Thanks Rafi

Rafi
  • 161
  • 2
  • 5
  • 7
  • 1
    Can you try without setting the JAXB_ENCODING property? It is used for writing (marshalling) XML only, I think. – Thilo Aug 23 '11 at 10:37
  • @Rafi - For more information on JAXB properties see: http://blog.bdoughan.com/2011/08/jaxb-and-java-io-files-streams-readers.html – bdoughan Aug 23 '11 at 17:41

1 Answers1

6

You're setting a Marshaller property on an Umarshaller:

Unmarshaller um = context.createUnmarshaller();         
um.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 

You can only set Unmarshaller properties on an Unmarshaller.

Remove the setProperty and try again.

skaffman
  • 381,978
  • 94
  • 789
  • 754
  • Thanks for the response. I am writing a generic XML parser using JAXB for parsing ISO20022 credit transfers XML files. I have generated all the POJOS by using in built JAXB tool by feeding XSD into it for all the ISO versions. – Rafi Aug 24 '11 at 09:12
  • Thanks for the response. I am writing a generic XML parser using JAXB for parsing ISO20022 credit transfers XML files. I have generated all the POJOS by using in built JAXB tool by feeding XSD into it for all the ISO versions. pain.001.001.01.xsd / pain.001.001.02.xsd / pain.001.001.03.xsd. I have clubbed and created a single package structure so that I can have only unique element to java objects created. Now at the runtime i am giving different Document class to the JAXBContext context = JAXBContext.newInstance(DocumentVer01.class); Document class is different for each of the versions. – Rafi Aug 24 '11 at 09:20
  • Hi I found the answer for this in the same forum. http://stackoverflow.com/q/1871060/907519 – Rafi Aug 24 '11 at 10:04