hal.js
function resourceToXml (resource, rel, currentIndent, nextIndent) {
    // Do not add line feeds if no indentation is asked
    var LF = (currentIndent || nextIndent) ? '\n' : '';

    // Resource tag
    var xml = currentIndent + '<resource';

    // Resource attributes: rel, href, name
    if (rel) xml += ' rel="' + escapeXml(rel) + '"';
    if (resource.href || resource._links.self) xml += ' href="' + escapeXml(resource.href || resource._links.self.href) + '"';
    if (resource.name) xml += ' name="' + escapeXml(resource.name) + '"';
    xml += '>' + LF;

    // Add <link> tags
    for (var rel in resource._links) {
      if (!resource.href && rel === 'self') continue;
      xml += currentIndent + nextIndent + resource._links[rel].toXML() + LF;
    }

    // Add embedded
    for (var embed in resource._embedded) {
      // [Naive singularize](https://github.com/naholyr/js-hal#why-this-crappy-singularplural-management%E2%80%AF)
      var rel = embed.replace(/s$/, '');
      resource._embedded[embed].forEach(function (res) {
        xml += resourceToXml(res, rel, currentIndent + nextIndent, currentIndent + nextIndent + nextIndent) + LF;
      });
    }

    // Add properties as tags
    for (var prop in resource) {
      if (resource.hasOwnProperty(prop) && prop !== '_links' && prop !== '_embedded') {
        xml += currentIndent + nextIndent + '<' + prop + '>' + String(resource[prop]) + '</' + prop + '>' + LF;
      }
    }

    // Close tag and return the shit
    xml += currentIndent + '</resource>';

    return xml;
  }
function resourceToXml (resource, rel, currentIndent, nextIndent) {
    // Do not add line feeds if no indentation is asked
    var LF = (currentIndent || nextIndent) ? '\n' : '';

    // Resource tag
    var xml = currentIndent + '<resource';

    // Resource attributes: rel, href, name
    if (rel) xml += ' rel="' + escapeXml(rel) + '"';
    if (resource.href || resource._links.self) xml += ' href="' + escapeXml(resource.href || resource._links.self.href) + '"';
    if (resource.name) xml += ' name="' + escapeXml(resource.name) + '"';
    xml += '>' + LF;

    // Add <link> tags
    for (var rel in resource._links) {
      if (!resource.href && rel === 'self') continue;
      xml += currentIndent + nextIndent + resource._links[rel].toXML() + LF;
    }

    // Add embedded
    for (var embed in resource._embedded) {
      // [Naive singularize](https://github.com/naholyr/js-hal#why-this-crappy-singularplural-management%E2%80%AF)
      var rel = embed.replace(/s$/, '');
      resource._embedded[embed].forEach(function (res) {
        xml += resourceToXml(res, rel, currentIndent + nextIndent, currentIndent + nextIndent + nextIndent) + LF;
      });
    }

    // Add properties as tags
    for (var prop in resource) {
      if (resource.hasOwnProperty(prop) && prop !== '_links' && prop !== '_embedded') {
        xml += currentIndent + nextIndent + '<' + prop + '>' + String(resource[prop]) + '</' + prop + '>' + LF;
      }
    }

    // Close tag and return the shit
    xml += currentIndent + '</resource>';

    return xml;
  }
function resourceToXml ( resource , rel , currentIndent , nextIndent ) { // Do not add line feeds if no indentation is asked var LF = ( currentIndent || nextIndent ) ? '\n' : '' ; // Resource tag var xml = currentIndent + '<resource' ; // Resource attributes: rel, href, name if ( rel ) xml += ' rel="' + escapeXml ( rel ) + '"' ; if ( resource . href || resource . _links . self ) xml += ' href="' + escapeXml ( resource . href || resource . _links . self . href ) + '"' ; if ( resource . name ) xml += ' name="' + escapeXml ( resource . name ) + '"' ; xml += '>' + LF ; // Add <link> tags for ( var rel in resource . _links ) { if ( ! resource . href && rel === 'self' ) continue ; xml += currentIndent + nextIndent + resource . _links [ rel ] . toXML ( ) + LF ; } // Add embedded for ( var embed in resource . _embedded ) { // [Naive singularize](https://github.com/naholyr/js-hal#why-this-crappy-singularplural-management%E2%80%AF) var rel = embed . replace ( / s$ / , '' ) ; resource . _embedded [ embed ] . forEach ( function ( res ) { xml += resourceToXml ( res , rel , currentIndent + nextIndent , currentIndent + nextIndent + nextIndent ) + LF ; } ) ; } // Add properties as tags for ( var prop in resource ) { if ( resource . hasOwnProperty ( prop ) && prop !== '_links' && prop !== '_embedded' ) { xml += currentIndent + nextIndent + '<' + prop + '>' + String ( resource [ prop ] ) + '</' + prop + '>' + LF ; } } // Close tag and return the shit xml += currentIndent + '</resource>' ; return xml ; }
Convert a resource to its XML representation
@private
@param Resource resource
@param String rel → relation identifier for embedded object
@param String currentIdent → current indentation
@param String nextIndent → next indentation
Convert a resource to its XML representation