Text/Zhu Jiqian
In the development process of a Java parsing xml file, when using SAX parsing, such an exception message appeared:
Error on line 60 of document : References to the entity "xxx" must end with a ';' separator;
After I opened the xml file, I found that the “xxx” symbol was followed by an “&” symbol. Later, I learned that this type of symbol is a special symbol in xml, and if the special symbol is not represented by an escape character, it is used directly In the xml file, there will be strange exceptions when using SAX and other methods for parsing.
In fact, this is all caused by these special characters.
Special symbols in XML include <> & ‘”, etc. They are not allowed as PCDATA in xml files. If you want to use them, you need to use escape characters instead:
< <
> >
& &
" "
' '
So, if you want to read the xml file data normally, how should you use the escape character to replace it?
At the beginning, I was thinking about how to solve Baidu, but found that many posts were several years ago, and there was no clear way to solve it. Most of them mentioned that the analysis abnormality caused by special symbols, but how to filter it out, it seems It’s vague, so I can only make a fool of myself and come up with a more appropriate solution for filtering special characters.
The realization idea is actually very simple. We can read the xml file through the Reader before reading the xml file and use SAX to parse it, and then read it out by line and concatenate it into a String string, and then use the string replacement method replaceAll() After replacing the special symbols, you can directly convert the xml in the form of a string into a Document object for xml parsing:
String xmlStr=s.replaceAll("&","&");
The conversion method code is as follows:
StringBuffer buffer = new StringBuffer();
BufferedReader bf= new BufferedReader(new FileReader("D:\\测试.xml"));
String s = null;
while((s = bf.readLine())!=null){
buffer.append(s.trim());
}
String str = buffer.toString();
//In this step character replacement is performed, replacing them with legal escaped characters
String xml=str.replaceAll("&","&");
//Here the processed xml file can be read and parsed
Document document = DocumentHelper.parseText(xml);
So far, you can solve the problem of special symbols & abnormalities encountered in Java parsing xml files.
Read More:
- Eclipse: How to Close XML validation (xml file Error)
- How to Solve Error: Error parsing mapper XML
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] Kafka in Windows error:java. nio. file. Filesystemexception: this file is in use by another program and cannot be accessed by the process
- [Maven] maven filtering OTS parsing error incorrect file size in WOFF head [Two Methods to Solve]
- [Solved] Spring set xml Component Scanning error: Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException…
- [Solved] nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
- Error resolution: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/Datatype
- Error:Abnormal build process termination [How to Solve]
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- JAVA: Random access file is always garbled
- o.s.boot.SpringApplication, Error creating bean with name ‘paymentImpl‘, xml Configuration file error
- Gateway Error: allowedOrigins cannot contain the special value “*“
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- [Solved] Java.util.linkedhashmap cannot be cast to entity class
- Java error prompt….. cannot be resolved
- [Solved] Mybatis insert Error: Cause: java.sql.SQLException: SQL String cannot be empty
- [Solved] No validator could be found for constraint ‘javax.validation.constraints.NotBlank’ validating type ‘java.lang.String’
- Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber