2字节UTF-8序列的字节2无效

时间:2015-05-21 08:22:00

标签: java xml

这是SAP PI场景。消息在两个系统之间交换。源系统:IPOS目标系统:ECC ..一旦消息到达PI系统,就会生成与IPOS对应的xml。我必须将IPOS结构映射到ECC结构。这里我使用DOM解析器..我使用main()进行测试。

public static void main (String str[]) throws FileNotFoundException, StreamTransformationException { 

    ZcreateHomeDelivery obj = new ZcreateHomeDelivery();

    try { 


    InputStream inputStream  = new FileInputStream("C:/Users/XYZ/workspace/input.xml");

    FileOutputStream newOut = new FileOutputStream("C:/Users/XYZ/workspace/output.xml");  
    obj.execute(inputStream, newOut);
    }catch (Exception e ) { 
        e.printStackTrace();
    }}

我已导入所有必需的包..

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformation;

我遇到的问题是当我执行代码时,我收到错误

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
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 com.sap.nw.rr.hd.ZcreateHomeDelivery.execute(ZcreateHomeDelivery.java:85)
at com.sap.nw.rr.hd.ZcreateHomeDelivery.main(ZcreateHomeDelivery.java:801)
java.lang.NullPointerException
at com.sap.nw.rr.hd.ZcreateHomeDelivery.execute(ZcreateHomeDelivery.java:101)
at com.sap.nw.rr.hd.ZcreateHomeDelivery.main(ZcreateHomeDelivery.java:801)

我尝试过的解决方案是

try
    {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");// added
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(outdoc);
        StreamResult result = new StreamResult(outputStream);
        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
        throw new StreamTransformationException("Mapping failed:Check input file", e);
    }

*添加(在上面的代码中注释)来解决问题 - 但它提供了帮助。我必须添加UTF-8,但在哪里添加它?

这已解决:解决方案是 1)打开记事本中的xml 2)确保文档的开头和结尾没有多余的空间 3)选择文件 - >另存为 4)选择保存类型 - >所有文件
5)输入文件名为filename.xml
6)选择编码 - UTF-8 - >单击“保存”

1 个答案:

答案 0 :(得分:0)

您好,您可以找到here对问题的引用。问题是xml文件声明了与用于保存文件的编码不同的编码。 如果使用iso-8859-1对文件进行编码,并且在文件中将其声明为UTF-8,则可以使用阅读器读取文件,并将编码强制为正确的编码。链接显示了这样做的api。