XML解析 Invalid byte 2 of 2-byte UTF-8 sequence

String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new ByteArrayInputStream(xmlStr.getBytes())); 

1.以上代码在字符串转Document时碰到以下错误:

   Invalid byte 2 of 2-byte UTF-8 sequence

解决方法:

Document doc = saxReader.read(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

或直接用以下方法

DocumentHelper.parseText(xmlStr);

 

 2.

 

Document doc  = DocumentHelper.parseText(xmlStr);

 

   出现The processing instruction target matching "[xX][mM][lL]" is not allowed. 

 xml前后出现空行导致改成以下即可

 

Document doc  = DocumentHelper.parseText(xmlStr.trim());

 

 

  

 

你可能感兴趣的:(sequence)