xmlbeans.xmlexception。非法XML字符0x0

时间:2011-09-30 17:49:11

标签: java parsing bytearray xmlbeans xmlexception

我通过System.Text.UTF8Encoding()将字符串转换为字节,在C#中创建一个xml文档。然后我将它发送到我的java程序,xmlbeans通过TCP连接进行解析。无论我尝试什么,我都会收到此错误: org.apache.xmlbeans.XmlException:错误:非法XML字符:0x0 org.apache.xmlbeans.impl.piccolo.io.IllegalCharException:非法XML字符:0x0

我试图清理C#端的字符串,但它没有找到任何0x0的实例。我已经循环并输出我在java端接收的byte []中的每个字节,并且绝对没有任何0x0。

这是我的java端代码:

    public void parseBytes(byte[] bytes, int length, String source)
{
    System.out.println("***************BmsDrawingGatewayParser - ParseBytes  " + length);        

    String foundData = null;
    try
    {
        foundData = new String(bytes, 0, length, "UTF-8");
    }
    catch (UnsupportedEncodingException e1)
    {
        e1.printStackTrace();
    }
    switch (readState)
    {             
    case STATE_NEW_MSG:
        // if contains the 
        if (foundData.contains(startMessageTag))
        {
            if (foundData.contains(endMessageTag))
            {   
                byteStream.write(bytes, 0, length);                   
                parseXml(byteStream.toByteArray());
                if (byteStream.size() > 0)
                {
                    byteStream.reset();
                }
            }
            else
            {                    
                readState = DrawingDeviceParserState.STATE_READING_MSG;
            }                
        }
        else
        {
            System.out.println("Couldn't find start tag");
            System.out.println(foundData);
        }
        break;

    case STATE_READING_MSG:          
        byteStream.write(bytes, byteStream.size(), length);
        if (foundData.contains(endMessageTag))
        {
            System.out.println("Now going to parse");
            //parseXml(xmlString.toString());
            parseXml(byteStream.toByteArray());
            byteStream.reset();
            readState = DrawingDeviceParserState.STATE_NEW_MSG;
        }
        else
        {
            System.out.println("Couldn't find end tag");
            System.out.println(foundData);
        }
        break;
    }                        
}

    private void parseXml(byte[] xmlData)
    {
        System.out.println(xmlData);

        //EventDocument.Factory.parse
        ByteArrayInputStream sid = new ByteArrayInputStream(xmlData);     
        try
        {
            EventDocument eventDoc = EventDocument.Factory.parse(sid);
            if (eventDoc.validate())
            {
                System.out.println("Document is valid");
            }
            else
            {
                System.out.println("Document is INVALID");
            }
            EventDocument.Event myEvent = eventDoc.getEvent();
            EventDocument.Event.Detail[] myDetailArray = myEvent.getDetailArray();

            //myDetailArray[0].

            //BmsDrawingDocument drawingDoc = myEvent.getDetail();
            System.out.println("MY UID: " + myEvent.getUid());
        }
        catch(Exception xmlException)
        {
            System.out.println(xmlException.toString());
            xmlException.printStackTrace();
        }
}

有谁知道我可能做错了什么?我可以提供更多信息吗?

2 个答案:

答案 0 :(得分:0)

它发生在我身上,发现lib文件已损坏,因此请用未损坏或旧版本替换lib。它解决了我的问题。

答案 1 :(得分:-1)

public void parseBytes(byte[] bytes, int length, String source)
{               
    String foundData = null;
    try
    {
        foundData = new String(bytes, 0, length, "UTF-8");
    }
    catch (UnsupportedEncodingException e1)
    {
        e1.printStackTrace();
    }
    switch (readState)
    {             
    case STATE_NEW_MSG:
        // if contains the 
        if (foundData.contains(startMessageTag))
        {
            if (foundData.contains(endMessageTag))
            {   
                byteStream.write(bytes, 0, length);                   
                parseXml(byteStream.toByteArray());
                if (byteStream.size() > 0)
                {
                    byteStream.reset();
                }
            }
            else
            {                    
                readState = DrawingDeviceParserState.STATE_READING_MSG;
            }                
        }
        else
        {
            System.out.println("Couldn't find start tag");
            System.out.println(foundData);
        }
        break;

    case STATE_READING_MSG:          
        byteStream.write(bytes, byteStream.size(), length);
        if (foundData.contains(endMessageTag))
        {
            System.out.println("Now going to parse");
            //parseXml(xmlString.toString());
            parseXml(byteStream.toByteArray());
            byteStream.reset();
            readState = DrawingDeviceParserState.STATE_NEW_MSG;
        }
        else
        {
            System.out.println("Couldn't find end tag");
            System.out.println(foundData);
        }
        break;
    }                        
}

    private void parseXml(byte[] xmlData)
    {
        System.out.println(xmlData);

        //EventDocument.Factory.parse
        ByteArrayInputStream sid = new ByteArrayInputStream(xmlData);     
        try
        {
            EventDocument eventDoc = EventDocument.Factory.parse(sid);
            if (eventDoc.validate())
            {
                System.out.println("Document is valid");
            }
            else
            {
                System.out.println("Document is INVALID");
            }
            EventDocument.Event myEvent = eventDoc.getEvent();
            EventDocument.Event.Detail[] myDetailArray = myEvent.getDetailArray();

            //myDetailArray[0].

            //BmsDrawingDocument drawingDoc = myEvent.getDetail();
            System.out.println("MY UID: " + myEvent.getUid());
        }
        catch(Exception xmlException)
        {
            System.out.println(xmlException.toString());
            xmlException.printStackTrace();
        }
}