Tapatalk

Pretty-print XML

Pretty-print XML

3
NewbieNewbie
3

    Nov 21, 2007#1

    I've created the script below that some of you might find useful. It's dead simple and is documented. To use it, just add the script (I recommend you also associate it with a hotkey). Then you need only select the XML fragment you want to format and run the script. There are a couple situations that you should be aware of, which I plan to raise as issues in the support forum:

    1. Selecting an ENTIRE xml document doesn't work as the XML prolog throws it off - run the script against the document with the prolog unselected and you should be fine.
    2. The script only seems to work against 8-bit character encoded files (i.e. UNIX or DOS shown in the status bar). If you're running the script against selected text in a file recognized as Unicode or UTF-8 with Unicode editing, then the script will fail.

    Code: Select all

    //
    // When invoked, this script expects that the user has selected a well-formed
    // block of XML. It then overwrites that XML fragment in-place with a pretty-
    // printed version.
    //
    // The scripting engine included with UltraEdit-32 is based on Ecma-357 (E4X),
    // which is the same language used within Adobe Flex. Thus, the following site
    // was used as a reference in creating this script:
    //
    //   http://livedocs.adobe.com/flex/2/langref/XML.html
    //
    
    XML.ignoreComments = false;
    XML.ignoreProcessingInstructions = false;
    XML.ignoreWhitespace = true;
    XML.prettyPrinting = true;
    XML.prettyIndent = 2;
    
    var xmlNode = new XML( UltraEdit.activeDocument.selection );
    
    xmlNode.normalize();
    UltraEdit.activeDocument.write( xmlNode.toXMLString() );

    262
    MasterMaster
    262

      Nov 22, 2007#2

      Wow. I hadn't checked the additions to JavaScript 1.6 and JavaScript 1.7. There is a lot of nice functionality for array handling and more - as well as ECMAScript for XML (E4X). Thank you for making me aware of this xavier!

      Regarding UTF-8 files I guess you could try if a simple workaround with script commands UltraEdit.activeDocument.unicodeToASCII() and UltraEdit.activeDocument.ASCIIToUnicode() in the start and end of your script would work. I haven't tried this myself.

      You other point with the XML prolog seems to be a known error but until it is fixed later on you could add this workaround to your script:

      Code: Select all

      //
      // When invoked, this script expects that the user has selected a well-formed
      // block of XML. It then overwrites that XML fragment in-place with a pretty-
      // printed version.
      //
      // The scripting engine included with UltraEdit-32 is based on Ecma-357 (E4X),
      // which is the same language used within Adobe Flex. Thus, the following site
      // was used as a reference in creating this script:
      //
      //   http://livedocs.adobe.com/flex/2/langref/XML.html
      //
      
      XML.ignoreComments = false;
      XML.ignoreProcessingInstructions = false;
      XML.ignoreWhitespace = true;
      XML.prettyPrinting = true;
      XML.prettyIndent = 2;
      
      // Simple regular expression to match
      // <?xml..?><!DOCTYPE..><root>...</root>
      var parsedDoc = /(<\?xml[^?]+\?>)?(<!DOCTYPE[^>]+>)?([^ÿ]*)/.exec(UltraEdit.activeDocument.selection);
      
      // Parsing was succesful
      if (parsedDoc) {
         // assign using "destructuring assignment", new in JavaScript 1.7 
         var [, xmlVersion, xmlDoctype, xmlDoc] = parsedDoc;
      
         var xmlNode = new XML( xmlDoc );
      
         xmlNode.normalize();
         if(xmlVersion) {
            UltraEdit.activeDocument.write(xmlVersion+"\r\n");
         }
         if(xmlDoctype) {
            UltraEdit.activeDocument.write(xmlDoctype+"\r\n");
         }
         UltraEdit.activeDocument.write( xmlNode.toXMLString() );
      }
      This example without DOCTYPE:

      Code: Select all

      <?xml version="1.0" encoding="iso-8859-1"?><menu><item>1</item><item>2</item><item/></menu>
      
      will be reformatted as:

      Code: Select all

      <?xml version="1.0" encoding="iso-8859-1"?>
      <menu>
        <item>1</item>
        <item>2</item>
        <item/>
      </menu>