Objective: given the file, determine whether it is of a given type (XML, JSON, Properties etc) Consider the case of XML - Up until we ran into this issue, the following sample approach worked fine:     try {         saxReader.read(f);     } catch (DocumentException e) {         logger.warn(&quot;  - File is not XML: &quot; + e.getMessage());         return false;     }     return true;  As expected, when XML is well formed, the test would pass and method would return true. If something bad happens and file can't be parsed, false will be returned. This breaks however when we deal with a malformed XML (still XML though) file. I'd rather not rely on .xml extension (fails all the time), looking for &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; string inside the file etc. Is there another way this can be handled? What would you have to see inside the file to &quot;suspect it may be XML though DocumentException was caught&quot;. This is needed for parsing purposes.
14680
How do you get a Media Type (MIME type) from a file using Java?  So far I've tried JMimeMagic &amp; Mime-Util. The first gave me memory exceptions, the second doesn't close its streams properly. How would you probe the file to determine its actual type (not merely based on the extension)?
11049
I'm looking for a library/method to parse an html file with more html specific features than generic xml parsing libraries.
97056
I am looking for a tool which will take an XML instance document and output a corresponding XSD schema.  I certainly recognize that the generated XSD schema will be limited when compared to creating a schema by hand (it probably won't handle optional or repeating elements, or data constraints), but it could at least serve as a quick starting point.
20412
I was looking for a generic method in .Net to encode a string for use in an Xml element or attribute, and was surprised when I didn't immediately find one.  So, before I go too much further, could I just be missing the built-in function?    Assuming for a moment that it really doesn't exist, I'm putting together my own generic EncodeForXml(string data) method, and I'm thinking about the best way to do this.    The data I'm using that prompted this whole thing could contain bad characters like &amp;, &lt;, &quot;, etc.  It could also contains on occasion the properly escaped entities: &amp;amp;, &amp;lt;, and &amp;quot;, which means just using a CDATA section may not be the best idea.  That seems kinda klunky anyay; I'd much rather end up with a nice string value that can be used directly in the xml.  I've used a regular expression in the past to just catch bad ampersands, and I'm thinking of using it to catch them in this case as well as the first step, and then doing a simple replace for other characters.    So, could this be optimized further without making it too complex, and is there anything I'm missing? :  Function EncodeForXml(ByVal data As String) As String     Static badAmpersand As new Regex("&amp;(?![a-zA-Z]{2,6};|#[0-9]{2,4};)")      data = badAmpersand.Replace(data, "&amp;amp;")      return data.Replace("&lt;", "&amp;lt;").Replace("""", "&amp;quot;").Replace("&gt;", "gt;") End Function   Sorry for all you C# -only folks-- I don't really care which language I use, but I wanted to make the Regex static and you can't do that in C# without declaring it outside the method, so this will be VB.Net  Finally, we're still on .Net 2.0 where I work, but if someone could take the final product and turn it into an extension method for the string class, that'd be pretty cool too.  Update The first few responses indicate that .Net does indeed have built-in ways of doing this.  But now that I've started, I kind of want to finish my EncodeForXml() method just for the fun of it, so I'm still looking for ideas for improvement.  Notably: a more complete list of characters that should be encoded as entities (perhaps stored in a list/map), and something that gets better performance than doing a .Replace() on immutable strings in serial.
16625
The code looks like this:   StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings)) {     XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());     s.Serialize(xmlWriter, objectToSerialize); }   The resulting serialized document includes namespaces, like so:   &lt;message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"      xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"      xmlns="urn:something"&gt;  ... &lt;/message&gt;   To remove the xsi and xsd namespaces, I can follow the answer from .  I want my message tag as &lt;message&gt; (without any namespace attributes).  How can I do this?
25788
I have an XSD file that is encoded in UTF-8, and any text editor I run it through doesn't show any character at the beginning of the file, but when I pull it up in Visual Studio's debugger, I clearly see an empty box in front of the file.      I also get the error:   Data at the root level is invalid. Line 1, position 1.    Anyone know what this is?  Update: Edited post to qualify type of file. It's an XSD file created by Microsoft's XSD creator.
960
See code:   var file1 = "50.xsl"; var file2 = "30.doc"; getFileExtension(file1); //returns xsl getFileExtension(file2); //returns doc  function getFileExtension(filename) {     /*TODO*/ }
27515
I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?  String unformattedXml = "&lt;tag&gt;&lt;nested&gt;hello&lt;/nested&gt;&lt;/tag&gt;"; String formattedXml = new [UnknownClass]().format(unformattedXml);   Note: My input is a String. My output is a String.   (Basic) mock result:    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;root&gt;   &lt;tag&gt;     &lt;nested&gt;hello&lt;/nested&gt;   &lt;/tag&gt; &lt;/root&gt;
53765
When I parse my xml file (variable f) in this method, I get an error      C:\Documents and Settings\joe\Desktop\aicpcudev\OnlineModule\map.dtd (The system cannot find the path specified)   I know I do not have the dtd, nor do I need it.  How can I parse this File object into a Document object while ignoring DTD reference errors?  private static Document getDoc(File f, String docId) throws Exception{     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     DocumentBuilder db = dbf.newDocumentBuilder();     Document doc = db.parse(f);       return doc; }
27747
Is there a simple method of parsing XML files in C#? If so, what?
26428
I need to stream a file from Salesforce in our webapplication. To make sure the file is properly opened, I have to set Content-Type header.  How to retrieve the Content-Type (MIME) of a file?  I expect values like: image/png, application/pdf, text/html etc.  How to get these values? I am using the Java and a SOAP client to retrieve the data.
76521
I need to validate an XML string (and not a file) against a DTD description file.   How can that be done in python?
34099
I am using XmlReader in .NET to parse an XML file using a loop:  while (xml.Read()) {    switch xml.NodeType {      case XmlNodeType.Element:       //Do something      case XmlNodeType.Text:       //Do something      case XmlNodeType.EndElement:         //Do something    } }   I was wondering if it was normal that the following XML code does not produce some EndElement nodes? Please note the missing space before the /> but I don't think that's the problem.  &lt;date month="November" year="2001"/&gt; &lt;zone name="xml"/&gt;   Is there a different NodeType or property to indicate a self-closing element?
35765
I have a variable string that contains well-formed and valid XML. I need to use JavaScript code to parse this feed.  How can I accomplish this using (browser-compatible) JavaScript code?
65595
Not quite an Attribute, not quite a Method. Stereotypes? &lt;&lt;get&gt;&gt; &lt;&lt;set&gt;&gt;?    I'm retro-modelling an existing system, so I need to clearly reflect that this is not the same as a readonly field or a methods pair (regardless of what the IL says), so I think I'll go with the stereotype, but I'll accept the language independant get_ set_ as a general solution. Thanks all for the sanity test.
37897
There are situations, where it is practical to have a type-cast return a null value instead of throwing a ClassCastException. C# has the as operator to do this. Is there something equivalent available in Java so you don't have to explicitly check for the ClassCastException?
2110
At work we are being asked to create XML files to pass data to another offline application that will then create a second XML file to pass back in order to update some of our data. During the process we have been discussing with the team of the other application about the structure of the XML file.   The sample I came up with is essentially something like:  &lt;INVENTORY&gt;    &lt;ITEM serialNumber="something" location="something" barcode="something"&gt;       &lt;TYPE modelNumber="something" vendor="something"/&gt;     &lt;/ITEM&gt; &lt;/INVENTORY&gt;   The other team said that this was not industry standard and that attributes should only be used for meta data. They suggested:  &lt;INVENTORY&gt;    &lt;ITEM&gt;       &lt;SERIALNUMBER&gt;something&lt;/SERIALNUMBER&gt;       &lt;LOCATION&gt;something&lt;/LOCATION&gt;       &lt;BARCODE&gt;something&lt;/BARCODE&gt;       &lt;TYPE&gt;          &lt;MODELNUMBER&gt;something&lt;/MODELNUMBER&gt;          &lt;VENDOR&gt;something&lt;/VENDOR&gt;       &lt;/TYPE&gt;    &lt;/ITEM&gt; &lt;/INVENTORY&gt;   The reason I suggested the first is that the size of the file created is much smaller. There will be roughly 80000 items that will be in the file during transfer. Their suggestion in reality turns out to be three times larger than the one I suggested. I searched for the mysterious "Industry Standard" that was mentioned, but the closest I could find was that XML attributes should only be used for meta data, but said the debate was about what was actually meta data.  After the long winded explanation (sorry) how do you determine what is meta data, and when designing the structure of an XML document how should you decide when to use an attribute or an element?