Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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
using:
Code:
 
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
parser.parseURI('example.xml');
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:
Code:
 
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
 
Old December 9th, 2009, 09:54 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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):

Code:
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;
    }
};
xhr.send(null);
This works in all browsers the same way.
__________________
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)





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.