Automatically determine XML documents
Is it possible to setup EmEditor to automatically determine if opened file is XML document, regardless of extension, and apply XML scheme.
Any XML document would have
<?xml version="1.0" encoding="utf-8"?> (encoding could be different)
as 1st line.
So, in theory, it should be very easy to determine just by looking at 1st line.
Hi slav.
You could utilize a macro script for that, which acts on event "File Opened"
Steps:
- save the macro as "DetectXML.jsee" to your EmEditor folder (I have a sub folder "Macros" created for that use)
- utilize "Macros > Select..." and select this new macro file to add it to MyMacros category.
- chose "Macros > Customize..."
- select the macro (at the end of the list),
- enable "[x] Run at Events"
- press at [Events...] and chose "[x] File Opened"
- ok
- ok
Test by opening an file containing XML structured text but lacks the XML extension (or not)
The macro:
(you may want to delete the "alert()" lines)
if (document.selection.Find("<?xml",eeFindNext)){
alert ('xml file detected'); //this line can be delete
alert("Current Configuration: " + document.ConfigName); //line can be delete
document.ConfigName = "XML"; //switch configuration to XML
alert("Configuration switched to: " + document.ConfigName); //line can be delete
}
Could be improved with
strFirstLine = document.GetLine( 1 ); if strFirstLine.indexOf("<?xml").....
to detect at top of document only.
.Works great, thank you very much.
The DetectXML.jsee only seems works on a freshly opened documents. But if you move the cursor below the first line and then re-open the document it can't find "<?xml" since the cursor position is saved and past the 1st line. I was unable to get the "strFirstLine = document.GetLine( 1 );" to work as it spits out a "Expected (" error.
Is there an alternate method for automagically detecting and switching to XML scheme?
4 posts in this topic