Reading XML with Javascript

T

Trevor

I'm extremely new to XML, tell me what needs to be fixed in my XML file
as well as in my javascript.

My XML file looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>

<imapmail>
<message>
<subject>Testing, 123</subject>
<sender>Name &lt;[email protected]&gt;</sender>
<date>1/2/03</date>
<size>640 KB</size>
<read>0</read>
</message>
</imapmail>

I'm accessing it with this javascript:

var sender =
xmlDocument.getElementsByTagName("sender").item(0).firstChild.data;

I ultimately want to user to see that the sender is "Name
<[email protected]>". With the way the XML file is currently set up
(sender is Name &lt;[email protected]&gt;), the only that shows up
on the javascript end is Name. Is the way I'm storing it in my XML file
the best way to be doing it?

Thanks for your help,
Trevor
 
M

Martin Honnen

Trevor wrote:

<imapmail>
<message>
<subject>Testing, 123</subject>
<sender>Name &lt;[email protected]&gt;</sender>
<date>1/2/03</date>
<size>640 KB</size>
<read>0</read>
</message>
</imapmail>

I'm accessing it with this javascript:

var sender =
xmlDocument.getElementsByTagName("sender").item(0).firstChild.data;

The variable sender should then have a string value with
'Name said:
I ultimately want to user to see that the sender is "Name
<[email protected]>". With the way the XML file is currently set up
(sender is Name &lt;[email protected]&gt;), the only that shows up
on the javascript end is Name.

Well how exactly do you use sender later? I am sure if you do e.g.
alert(sender)
you will see the string I have outlined above or if you do
document.body.appendChild(document.createTextNode(sender))
where document is a HTML document object will then show that string at
the end of the document.body.

I suspect you do e.g.
someHTMLElement.innerHTML = sender
and that way you cause the HTML parser to try to parse
'Name <[email protected]>'
which then will cause the parser to somehow try to interpret
<[email protected]> as a tag of an element in HTML and it can't do much
with that. Of course it does not make sense to use innerHTML to parse a
string which does not contain HTML markup.

But enough guessing, what exactly are you doing with sender?
 
T

Trevor

You guessed right, it's being written using innerHTML. I need to write
that value along with other markup which is why I'm doing it that way.
But I did figure it out. I'm just splitting <sender>Name
&lt;[email protected]&gt;</sender> into
<sendername>Name</sendername> and
<senderemail>[email protected]</senderemail>. That's easier to deal and
it makes more sense in the structure of my XML document.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 15 Jan 2006 23:52:43 remote, seen in
news:comp.lang.javascript said:
I'm extremely new to XML, tell me what needs to be fixed in my XML file
as well as in my javascript.
<date>1/2/03</date>

Change the date format to something incompatible with FFF. Hopefully
that will not require upgrading any formal standards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,851
Latest member
CliftonCor

Latest Threads

Top