2011-09-30 3 views
2

Je crée un document XML en C# en convertissant une chaîne en octets via System.Text.UTF8Encoding(). Je l'envoie ensuite à mon programme java pour xmlbeans pour analyser via une connexion TCP. Peu importe ce que j'essaie, je reçois cette erreur: org.apache.xmlbeans.XmlException: Erreur: caractère XML illégal: 0x0 org.apache.xmlbeans.impl.piccolo.io.IllegalCharException: caractère XML illégal: 0x0xmlbeans.xmlexception. caractère XML illégal 0x0

J'ai tenté d'assainir la chaîne du côté C#, mais elle ne trouve aucune instance de 0x0. J'ai bouclé et sorti chaque octet dans l'octet [] que je reçois du côté java, et il n'y a absolument rien qui ait un 0x0.

Ceci est mon code côté 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(); 
     } 
} 

Est-ce que quelqu'un sait ce que je pourrais faire mal? Y a-t-il plus d'informations que je peux fournir?

+0

pouvez-vous modifier String foundData = new Chaîne (octets); en String foundData = new String (octets, Charset.UTF-8); –

+0

Je peux, mais quelle différence cela ferait-il? Je ne fais que le référencer pour que je puisse chercher une corde? Je ne passe jamais réellement foundData à ma fonction parseXml. – Jason

+0

pouvez-vous écrire le fichier XML dans un fichier en C# et voir si vous avez des caractères supplémentaires au début ou à la fin du fichier. ou vous pouvez renifler les octets du réseau pour voir où le caractère nul est –

Répondre

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

Il me est arrivé et a trouvé être corrompu fichiers lib, remplacez donc les libs avec copie non corrompue ou vieux. Cela a résolu mon problème.

Questions connexes