Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何替换XML字符串中的无效字符?_Java_Xml Parsing - Fatal编程技术网

Java 如何替换XML字符串中的无效字符?

Java 如何替换XML字符串中的无效字符?,java,xml-parsing,Java,Xml Parsing,我有一个由UTF-16编码的字符串。当使用javax.xml.parsers.DocumentBuilder进行解析时,我遇到如下错误: Character reference "&#x0" is an invalid XML character 以下是我用来解析XML的代码: InputSource inputSource = new InputSource(); inputSource.setCharacterStream(new StringReader(xmlString));

我有一个由UTF-16编码的字符串。当使用
javax.xml.parsers.DocumentBuilder
进行解析时,我遇到如下错误:

Character reference "&#x0" is an invalid XML character
以下是我用来解析XML的代码:

InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(xmlString));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
org.w3c.dom.Document document = parser.parse(inputSource);

我的问题是,如何用
(空格)替换无效字符?

您试图解析无效的
xml实体,这就是引发异常的原因。似乎您不必为自己的情况担心UTF-16

找到一些解释和例子

例如,对于
有效xml
,不能使用
&
字符,我们需要使用
&取而代之。这里
&
是xml实体

假设上面的例子应该是自解释的,以理解什么是xml实体

据我所知,有一些xml实体是无效的。但别再担心了。可以声明并添加新的
xml实体
。查看上面的文章以了解更多细节



编辑:假设存在使xml无效的
&
字符。

您试图解析无效的
xml实体
,这就是引发异常的原因。似乎您不必为自己的情况担心UTF-16

找到一些解释和例子

例如,对于
有效xml
,不能使用
&
字符,我们需要使用
&取而代之。这里
&
是xml实体

假设上面的例子应该是自解释的,以理解什么是xml实体

据我所知,有一些xml实体是无效的。但别再担心了。可以声明并添加新的
xml实体
。查看上面的文章以了解更多细节



编辑:假设存在使xml无效的
&
字符。

您只需使用String.replaceAll并传递无效字符的模式。

您只需使用String.replaceAll并传递无效字符的模式。

StringEscapeUtils()

逃逸

public static void escapeXml(java.io.Writer writer,
                             java.lang.String str)
                      throws java.io.IOException

Escapes the characters in a String using XML entities.

For example: "bread" & "butter" => "bread" & "butter".

Supports only the five basic XML entities (gt, lt, quot, amp, apos). 
Does not support DTDs or external entities.

Note that unicode characters greater than 0x7f are currently escaped to their 
numerical \\u equivalent. This may change in future releases.

Parameters:
    writer - the writer receiving the unescaped string, not null
    str - the String to escape, may be null 
Throws:
    java.lang.IllegalArgumentException - if the writer is null 
    java.io.IOException - if there is a problem writing
See Also:
    unescapeXml(java.lang.String)
StringEscapeUtils()

逃逸

public static void escapeXml(java.io.Writer writer,
                             java.lang.String str)
                      throws java.io.IOException

Escapes the characters in a String using XML entities.

For example: "bread" & "butter" => "bread" & "butter".

Supports only the five basic XML entities (gt, lt, quot, amp, apos). 
Does not support DTDs or external entities.

Note that unicode characters greater than 0x7f are currently escaped to their 
numerical \\u equivalent. This may change in future releases.

Parameters:
    writer - the writer receiving the unescaped string, not null
    str - the String to escape, may be null 
Throws:
    java.lang.IllegalArgumentException - if the writer is null 
    java.io.IOException - if there is a problem writing
See Also:
    unescapeXml(java.lang.String)

您必须在解析XML之前执行此操作。我知道我必须在解析之前执行此操作,但问题是如何执行?请检查另一个stackoverflow线程的回答:您必须在解析XML之前执行此操作。我知道我必须在解析之前执行此操作,但问题是怎么做?从另一个stackoverflow线程中检查这个答案:我的xmlString是这样的:这是我的内容���;模式是什么?感谢我的xmlString是这样的:这是我的内容���;模式是什么?感谢此函数已弃用。它被替换为或。请注意,这些函数还过滤无效字符。还要注意,这并不能解决老年退休金问题。DOM API已转义预定义的实体。此函数已弃用。它被替换为或。请注意,这些函数还过滤无效字符。还要注意,这并不能解决老年退休金问题。domapi已经转义了预定义的实体。