Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 2nd Edition by Nicholas C Zakas; ISBN: 9780470227800
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 9th, 2009, 06:04 PM
Registered User
 
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default Javascript and XML...how to convert an Xslt file to DOM obj

Working through the Javascript book "Professional Javascript for Web Developers" by Wrox, author "Zakas".
In particular I am working through his section on how to handle XML processing in Javascript.

The book gives a really good cross browser function for handling xslt transformation, but it stops short in
telling me how to create a dom object out of an xslt file. "myfile.xslt"

I have never worked with any of the XML methods or properties before so I am not really sure of what I am
looking for. I do have a function that creates a dom object from an xml string that is cross browser functional.
Can I use this function to create an xslt dom object;

Code:
 
function parseXml(xml){
    
    var xmldom = null;
    
    if(typeof DOMParser != "undefined"){
        xmldom = (new DOMParser()).parseFromString(xml, "text/xml");
        var errors = xmldom.getElementsByTagName('parseError');
        if(errors.length){
            throw new Error{"xml parsing error:" + errors[0].textContent);
        }
    } else if (document.implementation.hasFeature("LS", "3.0")){
        var i = document.implementation;
        var parser = i.createLSParser(i.MODE_SYNCHRONOUS, null);
        var input = i.createLSInput();
        input.stringData =xml;
        xmldom = parser.parse(input); // if this fails it automatically throws an error with the data
    }else if(typeof ActiveXObject != "undefined"){
        var xmldom = createDocument();
        xmldom.loadXML(xml);
        if(xmldom.parseError != 0){
            throw new Error("xml parsing error: " + xmldom.parseError.reason);
        }
    } else {
        throw new Error("no parser available");
    }
    return xmldom;
} // end cross browser function for creating DOM object from strings
 
function createDocument(){
    
    if(typeof arguments.calle.activeXString != "string"){
        var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"];
        
        for(var i=0, len=versions.length; i<len; i++){
            try{
                var xmldom = new ActiveXObject(versions[i]);
                arguments.calle.activeXString = versions[i];
                return xmldom;
            } catch (ex) {
                //skip
            }
        }
    }
    return new ActiveXObject(arguments.callee.activeXString);
}
var xmldom = parseXml("my xml string");
The book on page 540 and 541 shows how to create an xslt dom object using the function:

createThreadSafeDocument()

but it doesn't give an idea on how to create the apropriate code for other browsers. The above is an IE only approach.

I searched on the web but maybe I am using the wrong search criteria
Can you advise?

thanx
Kevin
  #2 (permalink)  
Old December 9th, 2009, 08:55 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

An XSLT file is just an XML file, so you can load it into a DOM in the exact same way as you would any other XML file. Only IE has the concept of FreeThreadedDocument, all other browsers just use the same DOM document object as they do for XML.
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
The Following User Says Thank You to nzakas For This Useful Post:
krraleigh (December 10th, 2009)
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript and XML...convert a file into a string krraleigh BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 1 December 9th, 2009 08:54 PM
Convert XML file using XSLT sushil.sharma75 XSLT 13 November 20th, 2009 12:59 PM
How to save XML file using JScript/DOM in IE induriprakash XML 2 April 26th, 2005 07:37 AM
XML DOM and Javascript lilu XML 3 September 25th, 2003 07:55 AM
Problem writing xml file using DOM Bala XML 0 September 6th, 2003 02:15 AM



All times are GMT -4. The time now is 07:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.