org.xml.sax.SAXParseException:对实体“T”的引用必须以“;”结尾定界符

新手上路,请多包涵

我正在尝试使用 DOM 解析器解析包含一些特殊字符(如“&”)的 XML 文件。我收到 saxparse 异常“对实体的引用必须以 aa 定界符结尾”。有没有办法克服这个异常,因为我不能修改 XML 文件来删除特殊字符,因为它来自不同的应用程序。请建议一种解析此 XML 文件以获取根元素的方法?

提前致谢

这是我正在解析的 XML 的一部分

<P>EDTA/THAM WASH
</P>

<P>jhc ^ 72. METER SOLVENT: Meter 21 LITERS of R. O. WATER through the add line into
FT-250. Start agitator.
</P>

<P>R. O. WATER &lt;ZLl LITERS </P>

<P>•     NOTE: The following is a tool control operation. The area within 10 feet of any open vessel or container is under tool control. </P>

<P>-af . 73. CHARGE SOLIDS: Remove any unnecessary items from the tool controlled area. Indicate the numbers of each item that will remain in the tool controlled area during the operation in the IN box of the Tool Control Log. </P>

<P>^___y_ a. To minimize the potential for cross contamination, confirm that no other solids are being charged or packaged in adjacent equipment. </P>

<P>kk k WARNING: Wear protective gloves, air jacket and use local exhaust when handling TROMETHAMINE USP (189400) (THAM) (K-l--Irritant!). The THAM may be dusty. </P>

<P>-&lt;&amp;^b .   Charge 2.1 KG of TROMETHAMINE USP (189400) (THAM) into FT-250 through the top. </P>

<P>TROMETHAMINE USP (189400) (THAM) </P>

<P>Scale ID:     / / 7S </P>

<P>LotNo.:   qy/o^yo^ </P>

<P>Gross:    ^ . S </P>

<P>Tare: 10 ,1 </P>

<P>Net:     J^l </P>

<P>Total:   JL'J </P>

<P><Figure ActualText="&T ">

<ImageData src="images/17PT 07009K_img_1.jpg"/>
&amp;T </Figure>
Checked by </P>

原文由 vasumathi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 556
1 个回答

正如其他人所说,您的 XML 绝对无效。但是,如果您不能更改生成应用程序并且可以添加清理步骤,那么以下内容应该清理 XML:

 String clean = xml.replaceAll( "&([^;]+(?!(?:\\w|;)))", "&amp;$1" );

该正则表达式正在做的是寻找任何格式错误的实体引用并转义&符号。

具体来说, (?!(?:\\w|;)) 是一个否定的前瞻,它使匹配停止在任何不是单词字符 (az,0-9) 和分号的地方。所以整个正则表达式从 & 中获取所有不是 ; 的东西。直到第一个非单词、非分号字符。

它将除了 & 符号之外的所有内容都放在第一个捕获组中,以便可以在替换字符串中引用它。那是1美元。

请注意,这不会修复看起来有效但实际上无效的引用。例如,如果你有 &T;除非 XML 实际定义了实体,否则将引发完全不同类型的错误。

原文由 PSpeed 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题