frequently ask ? : Computers : Programming : Languages : JavaScript : XML : E4X (ECMAScript for XML)

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

Which global settings are there to influence parsing and serialization?
Which global settings are there to influence parsing and serialization?

Apr 7th, 2005 01:54
Martin Honnen,


You can read and write certain properties of the XML constructor
function to manipulate settings influencing parsing and serialization.
There are the following five settings:
  XML.ignoreComments
  XML.ignoreProcessingInstructions
  XML.ignoreWhitespace
  XML.prettyPrinting
  XML.prettyIndent
The setting XML.ignoreComments has the initial value true and then
causes XML comments to be ignored when parsing XML to create XML object
or XMLList objects:
  var xmlObject = <gods>
    <!-- Kibology for all. -->
    <god>Kibo</god>
  </gods>;
  alert(xmlObject.comments().length()); // shows 0
  // change setting so that comments are not ignored
  XML.ignoreComments = false;
  var xmlObject = <gods>
    <!-- Kibology for all. -->
    <god>Kibo</god>
  </gods>;
  alert(xmlObject.comments().length()); // shows 1
The setting XML.ignoreProcessingInstructions has the intial value true
and then causes processing instructions to be ignored when parsing XML
to create XML or XMLList objects:
  var godList = <>
    <?process Kibology="on"?>
    <god>Kibo</god>
    <?process Kibology="off"?>
  </>;
  alert(godList.length()); // shows 1
  // now change setting to not ignore processing instructions
  XML.ignoreProcessingInstructions = false;
  var godList = <>
    <?process Kibology="on"?>
    <god>Kibo</god>
    <?process Kibology="off"?>
  </>;
  alert(godList.length()); // shows 3 
The setting XML.ignoreWhitespace has the intial value true and then
during XML parsing causes insignicant white space between element tags
to be ignored which would otherwise result in white space text nodes
between element nodes:
  var xmlObject = <gods>
    <god>Kibo</god>
    <god>Xibo</god>
  </gods>;
  alert(xmlObject.*.length()); // shows 2 (the element nodes)
  XML.ignoreWhitespace = false;
  var xmlObject = <gods>
    <god>Kibo</god>
    <god>Xibo</god>
  </gods>;
  alert(xmlObject.*.length()); // shows 5 (element and text nodes)
  var result = '';
  for each (var xmlNode in xmlObject.*) {
    result += xmlNode.nodeKind() + '\r\n';
  }
  alert(result);
  /* shows
  'text
   element
   text
   element
   text'
  */
The setting XML.prettyPrinting has the initial value true and then
causes the serialization with toString() and toXMLString() to pretty
print the XML markup based on the number value of the XML.prettyIndent
setting which has the initial value 2:
  var xmlObject = <gods><god>Kibo</god><god>Xibo</god></gods>;
  alert(xmlObject); 
  /* shows
  '<gods>
     <god>Kibo</god>
     <god>Xibo</god>
   </gods>'
  */
  XML.prettyPrinting = false;
  alert(xmlObject);
  /* shows '<gods><god>Kibo</god><god>Xibo</god></gods>' */
  XML.prettyPrinting = true;
  XML.prettyIndent = 8;
  alert(xmlObject);
  /* shows
  '<gods>
           <god>Kibo</god>
           <god>Xibo</god>
   </gods>'
  */
To be able to easily store and restore those settings the XML
constructor function has three methods named settings, setSettings, and
defaultSettings where settings returns an object with the current
settings, where defaultSettings returns an object with the default
settings, and where setSettings takes an object with settings to set the
settings from that object:
  function inspectSettings (settings) {
    var result = '';
    for (var propertyName in currentSettings) {
      result += propertyName + ': ' + currentSettings[propertyName] +
'\r\n';
    }
    alert(result);
  }
  var currentSettings = XML.settings();
  inspectSettings(currentSettings);
  /* shows 
  'ignoreComments: true
   ignoreProcessingInstructions: true
   ignoreWhitespace: true
   prettyPrinting: true
   prettyIndent: 2'
  */ 
  XML.prettyPrinting = false;
  currentSettings = XML.settings();
  inspectSettings(currentSettings);
  /* shows 
  'ignoreComments: true
   ignoreProcessingInstructions: true
   ignoreWhitespace: true
   prettyPrinting: false
   prettyIndent: 2'
  */  
  XML.setSettings(XML.defaultSettings());
  currentSettings = XML.settings();
  inspectSettings(currentSettings);
  /* shows 
  'ignoreComments: true
   ignoreProcessingInstructions: true
   ignoreWhitespace: true
   prettyPrinting: true
   prettyIndent: 2'
  */