473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Converting text to DOM object

I have an XML file hosted by my ISP free web space. It naively treats
the file as text/plain. I would like to convert this data into a DOM
object. So far my googling has turned up nothing, although looking over
the DOM manual on Mozilla I came up with:

function convertDOM(text)
{
var lines = text.split("\r\n");
var dom = document.createDocumentFragment();
for (var i = 0; i < lines.length; i++)
{
var node = document.createTextNode(lines[i]);
dom.appendChild(node);
}
return dom;
}

The returned object is not treated as an DOM object.
Anything I am missing / A different approach maybe?

Any help is greatly apprecited.

Adonis
Jul 23 '05 #1
2 2104
Adonis wrote:
I have an XML file hosted by my ISP free web space. It naively treats
the file as text/plain. I would like to convert this data into a DOM
object. So far my googling has turned up nothing, although looking over
the DOM manual on Mozilla I came up with:

function convertDOM(text)
{
var lines = text.split("\r\n");
var dom = document.createDocumentFragment();
for (var i = 0; i < lines.length; i++)
{
var node = document.createTextNode(lines[i]);
dom.appendChild(node);
}
return dom;
}

The returned object is not treated as an DOM object.
Anything I am missing / A different approach maybe?

Any help is greatly apprecited.

Adonis

Forgot to mention, the file is being retrieved using XMLHttpRequest,
which then the server replies with text/plain and filling the
reponseText property of XMLHttpRequest.

Adonis
Jul 23 '05 #2


Adonis wrote:
Adonis wrote:
I have an XML file hosted by my ISP free web space. It naively treats
the file as text/plain.
Then contact your ISP to change the server settings so that XML is
served as application/xml. Or ask them which file suffix they have
associated with the MIME type application/xml or text/xml, perhaps they
have choosen something else that .xml.

I would like to convert this data into a DOM
object.


Mozilla has a method overrideMimeType that allows you to get the
plain/text parsed by the XML parser, you can do

var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'test2005071301.txt', true);
if (typeof httpRequest.overrideMimeType != 'undefined') {
httpRequest.overrideMimeType('application/xml');
}
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
alert(httpRequest.responseXML.getElementsByTagName ('*').length);
}
};
httpRequest.send(null);

I don't know of any other browser implementing that method but Opera 8
seems to parse the response as XML even if it is not delivered with an
XML MIME type.
Not sure what Safari does.
IE with MSXML will not parse text/plain as XML so you have good reasons
to ask your ISP to change the settings.
As a workaround you can try to parse responseText with MSXML yourself
but you should be aware that the approach has shortcomings when it comes
to using encodings other that UTF-8. You would simply pass the
responseText to the loadXML method see
<http://www.faqts.com/knowledge_base/view.phtml/aid/15302/fid/616>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
4
by: Clark Stevens | last post by:
I have a program that I'm converting from VB6 to VB.NET. It reads in a text file containing barcode numbers and their corresponding descriptions. Then the user enters the barcode number and the...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
5
by: almurph | last post by:
RE: Tryign to convert Graphics object to a bitmap Hi, Hope you can help me with this. I have to open a file and add some text to it and then display it. So I create an Image object then...
2
by: meetu3181 | last post by:
Hi All, I am new to perl.I need to parse a text file in unix which is having data like this .I need to to store each value in a seperate variable. The TXT FILE TO BE PARSED: HTTP_PORT = 7779...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.