2011-09-27 141 views
-6

我正在查看java代碼來檢查字符串是否是有效的XML。正則表達式來檢查字符串是否有效XML

+7

不要嘗試它!閱讀:http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-rege –

回答

7

用正則表達式驗證XML是不可能的。 XML不是regular language

使用XML解析器嘗試將字符串解析爲XML,或根據模式驗證XML文檔,例如DTDXSD文件。

-3

您可以使用解析器檢查文檔或文本是否格式正確。 我認爲這個鏈接將幫助您:

http://www.roseindia.net/xml/dom/DOMParserCheck.shtml

字符串中的鏈接替換該行

InputSource is = new InputSource(xmlFile);

ByteArrayInputStream的字符串流=新ByteArrayInputStream進行( 「XML文本」 .getBytes());

InputSource is = new InputSource(stringStream);

相關問題