当前位置:网站首页>DOM parsing XML error: content is not allowed in Prolog

DOM parsing XML error: content is not allowed in Prolog

2022-07-07 12:08:00 Programmer community


The content of the error report is :

Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.

 

The summary on the Internet is to analyze the content that contains BOM. This mark is invisible , There is only this mark in the stream .
BOM:Byte Order Mark, Chinese name byte order mark .UCS The specification recommends that before transferring a byte stream , Transmit first BOM To determine the byte order .
Actually UTF-8 You don't need to use BOM To indicate the byte order , But it can. use BOM To show how to code .BOM Of UTF-8 Encoding is EF BB BF, So , If the recipient receives EF BB BF The first byte stream , It means it is UTF-8 It's encoded .

 

terms of settlement :
If you are parsing a file :
It can be used UltraEdit perhaps EmEditor open XML, Then save as , When saving, the option is none BOM Of UTF-8 Save or have BOM Of UTF-8 preservation .

 

If it is the content requested from a remote :
Then you return the stream New For the string , I can't see BOM Of , But you have to intercept what you need :

if(null != result && !"".equals(result)){ if(result.indexOf("<") != -1 && result.lastIndexOf(">") != -1 && result.lastIndexOf(">") > result.indexOf("<"))  result = result.substring(result.indexOf("<"), result.lastIndexOf(">") + 1);}

 

It is also said that DOM4J Caused by the lower version , But I looked at the version I used 1.6.1, So exclude such possibility , But in practice, I still recommend using the latest stable version for development .

 

to update
Read the log today , Finding my abnormality is not because BOM It's caused by the head , Fortunately, I also printed the content I received , I have a look at , I went to , The server returns directly after its own processing failure error character string ,mian For a moment , Sure enough, something went wrong , This pit father's play .

public static void main(String[] args) throws DocumentException { String str = "error"; Document doc = DocumentHelper.parseText(str); System.out.println(doc.asXML());}

 

I recommend you to read more about “ DOM xml dom4j prolog bom ” The article

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130618401396.html