1

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);

私の質問は、無効な文字を(スペース) に置き換える方法は?

4

3 に答える 3

0

無効なものを解析しようとしていますがxml entity、これが例外を発生させます。UTF-16あなたの状況について心配する必要はないようです。

ここで説明と例を見つけてください。

&例として、文字を aに使用することはできません。代わりvalid xmlに使用する必要があります。&これがxml&エンティティです。

上記の例は、xmlエンティティが何であるかを理解するために自明であると仮定します。

私が理解しているように、無効な xml エンティティがいくつかあります。でももう心配いりません。new の宣言と追加が可能xml entityです。詳しくは上記記事をご覧ください。


編集:& xmlを無効にする文字があると仮定します。

于 2012-08-03T14:30:40.337 に答える
0

StringEscapeUtils()

エスケープXml

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)
于 2012-08-03T15:19:57.457 に答える