Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| 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 Search this Thread Display Modes
  #1 (permalink)  
Old December 9th, 2009, 06:58 PM
Registered User
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default javascript and XML...convert a file into a string

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 author gives a really good function to handle converting an XML string into a DOM object for processing,
but the book stops short on suggesting a way to handle files. Now I can handle files in two of the three cases
xmldom.load('myfile.xml') for IE &
//The DOM 3 Load and Save ..works in Opera.. 
var parser = document.implementation.createLSParser(document.implementation.MODE_SYNCHRONOUS, null);
//add event listener to determine when document has loaded
//code here...however...the load event never fires in 9.5..So I need to address this problem also
So the question is how do I convert an XML file to a string if my browser is using DOMParser?
Because this method will only take a string:
var parser = new DOMParser();
var xmldom = parser.parseFromString("<root><child/></root>", "text/xml");
// as you can see the above code only take a string, no other options here.
I was thinking that I could do a file read and use the string buffer function found in the beginning Javascript book. Is there a better way?
thanx kevin
  #2 (permalink)  
Old December 9th, 2009, 09:54 PM
nzakas's Avatar
Wrox Author
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts

I'm not sure I completely understand the question. If you want to read an XML file, your best bet is to use an XMLHttpRequest to retrieve the file and then get the string out of responseText (instead of responseXML):

var xhr = new XMLHttpRequest();
var xmlText;
xhr.open("get", "file.xml", true);
xhr.onreadystatechange = function(){
    if (xhr.readyState == 4 && xhr.status == 200){
        xmlText = xhr.responseText;
This works in all browsers the same way.
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
The Following User Says Thank You to nzakas For This Useful Post:
krraleigh (December 10th, 2009)

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
code of convert xml data type in string by C# prafullaborade XML 2 May 24th, 2008 07:49 AM
convert string to xml prafullaborade XML 3 May 21st, 2008 06:56 AM
VB.NET: Convert String and append to Xml files toytoy Pro VB.NET 2002/2003 0 September 2nd, 2004 10:50 PM

All times are GMT -4. The time now is 09:26 AM.

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