📜  转义Java字符串中的 XML 特殊字符

📅  最后修改于: 2022-05-13 01:54:36.456000             🧑  作者: Mango

转义Java字符串中的 XML 特殊字符

当我们读取一个 XML 文件并尝试写入另一个 XML 文件时,处理 XML 中的特殊字符对我们来说很重要。 Java中有一些保留字符需要转换或转义才能被视为字符串字面量。如果我们不转义这些特殊字符,那么Java中的 DOM 或 SAX 解析器等解析器会将它们视为 XML 标记,特别是在 < 和 > 的情况下。即使我们使用 XSLT 转换,这些解析器也会失败。因此,我们需要在将这些特殊字符读取为Java中的字符串字面量之前对其进行转义或转换。

XML 中的特殊字符

XML 中有 5 个常用的特殊字符在用作Java字符串时需要转义

  • & — &
  • < — <
  • > — >
  • ”——“
  • ' - '

这些特殊字符也称为 XML 元字符。通过转义过程,我们将用替代字符串替换这些字符,以给出特殊字符的字面量结果。

例子:

 Data Structures & Java 

// is an invalid string in java because '&' is a reserved literal 
// in XML that is used to import other XML entity. For converting this 
// to a valid String literal we need to & instead of & here.

 Data Structure & Java 

// now becomes a valid String.

在Java中,我们总是可以编写自己的函数来使用等效的字符串字面量转义 XML 特殊字符,但我们也可以使用 Apache Commons 提供的Java库“StringEscapeUtils”。这个库为我们提供了一个通用的 API,可以为我们进行 XML 转义。

代码:

Java
// Java program to escape all the five characters
// mentioned above using the StringEscapeUtils class
  
import java.io.*;
import org.apache.commons.lang.StringEscapeUtils;
  
class GeeksForGeeks {
    public static void main (String[] args) {
        
      System.out.println("Program to escape XML Special Characters !!");
          
      // Escape & character in XML String 
      String unescapedXMLString = "DataStructures & Java";
        
      System.out.println("Unescaped String: " + unescapedXMLString);
        
      // using StringEscapeUtils
      System.out.println("Escaped String: " 
                         + StringEscapeUtils.escapeXml(unescapedXMLString));
        
      // Escape > character in XML String 
      unescapedXMLString = "DataStructures > Java";
        
      System.out.println("Unescaped String: " + unescapedXMLString);
        
      // using StringEscapeUtils
      System.out.println("Escaped String: " 
                         + StringEscapeUtils.escapeXml(unescapedXMLString));
        
      // Escape < character in XML String 
      unescapedXMLString = "DataStructures < Java";
        
      System.out.println("Unescaped String: " + unescapedXMLString);
        
      // using StringEscapeUtils
      System.out.println("Escaped String: " 
                         + StringEscapeUtils.escapeXml(unescapedXMLString));
        
      // Escape " character in XML String 
      unescapedXMLString = "DataStructures \" Java";
        
      System.out.println("Unescaped String: " + unescapedXMLString);
        
      // using StringEscapeUtils
      System.out.println("Escaped String: " 
                         + StringEscapeUtils.escapeXml(unescapedXMLString));
        
      // Escape ' character in XML String 
      unescapedXMLString = "DataStructures ' Java";
        
      System.out.println("Unescaped String: " + unescapedXMLString);
        
      // using StringEscapeUtils
      System.out.println("Escaped String: " 
                         + StringEscapeUtils.escapeXml(unescapedXMLString));
             
    }
}


输出:

Program to escape XML Special Characters !!
Unescaped String: DataStructures & Java
Escaped String: DataStructures & Java
Unescaped String: DataStructures > Java
Escaped String: DataStructures > Java
Unescaped String: DataStructures < Java
Escaped String: DataStructures < Java
Unescaped String: DataStructures " Java
Escaped String: DataStructures " Java
Unescaped String: DataStructures ' Java
Escaped String: DataStructures ' Java