Search code examples
javaparsingarraysxmlbeansxmlexception

xmlbeans.xmlexception. illegal XML character 0x0


I create an xml document in C# by converting a string to bytes via System.Text.UTF8Encoding(). I then send this to my java program for xmlbeans to parse via a TCP connection. No matter what i try, i am getting this error: org.apache.xmlbeans.XmlException: error: Illegal XML character: 0x0 org.apache.xmlbeans.impl.piccolo.io.IllegalCharException: Illegal XML character: 0x0

I have attempted to sanitize the the string on the C# side, yet it does not find any instance of 0x0. I have looped through and output each byte in the byte[] that i receive on the java side, and there is absolutely nothing that has a 0x0.

This is my java side code:

    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();
        }
}

Does anyone know what i might be doing wrong? Is there more information that i can provide?


Solution

  • 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();
            }
    }