2011-09-27 143 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);

相关问题