开发者

How to determine whether a given string is an .xml file

开发者 https://www.devze.com 2023-02-08 03:21 出处:网络
I have an issue that I get some some response as a S开发者_JAVA技巧tring. This String could be a normal string,number etc.. or an .xml file.

I have an issue that I get some some response as a S开发者_JAVA技巧tring. This String could be a normal string,number etc.. or an .xml file.

Now ,when I get an xml file, I want to treat it differently. I am not able to distinguish between a string or an .xml file. Also, this xml file could have some syntatic error.

Please suggest , how do I go ahead

Code is like this:

Document document = reader.read(new StringReader(xml));

where xml can be a string or an xml file itself. If xml is a string , it is fine but if it is an xml file and with some syntax error then it should throw exception


If it is a proper XML document it should begin with a XML declaration. If that's there, it's intended to be a conforming XML document. If that's not there it cannot be a conforming XML document.


If you are using a coding language like C#, then you can use - XmlDocument.loadxml -

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

This will throw error if the string is not in correct xml format.

0

精彩评论

暂无评论...
验证码 换一张
取 消