2

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?

Jason
  • 2,147
  • 6
  • 32
  • 40
  • can u change String foundData = new String(bytes); to String foundData = new String(bytes, Charset.UTF-8); – Aravind Yarram Sep 30 '11 at 17:55
  • I can, but what difference would that make? I am only referencing it so i can look for a string? I am never actually passing foundData to my parseXml function. – Jason Sep 30 '11 at 17:57
  • can you write the xml to a file in c# and see if you are having additional character at the start or end of the file. or you can sniff the network bytes to see where the null character is – Aravind Yarram Sep 30 '11 at 18:19
  • I wrote it the data out using system.io.file.writealltext. I did it once without specifying encoding and once with. They both matched. I also wrote out hte bytes using a binaryWriter. None of these showed a leading or trailing null char. I'll see if i can find wireshark somewhere and look. I doubt that there is really a null in the data, at least coming from my C# side. – Jason Sep 30 '11 at 18:31
  • you can write the received bytes to a file and check too – Aravind Yarram Sep 30 '11 at 18:37
  • I believe i have found my issue. I was being passed in a byte[], and when i sent it to parseXml, i didn't check the size. the byte[] was 1024, though there was only 384 bytes of data. I'm hoping that was the issue. – Jason Sep 30 '11 at 19:04
  • Yes that was the issue. Code has been corrected above. – Jason Sep 30 '11 at 19:07
  • If you found the answer to your problem, add it as an answer, then click the green checkmark. – Tyler Sep 30 '11 at 21:27

2 Answers2

0

It happened to me and found out to be corrupted lib files, so replace the libs with uncorrupted or old copy. It solved my issue.

Rajesh
  • 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();
        }
}
Jason
  • 2,147
  • 6
  • 32
  • 40