By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Reading XML with Javascript

P: n/a
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@example.com&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
<em***@example.com>". With the way the XML file is currently set up
(sender is Name &lt;email@example.com&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

Jan 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Trevor wrote:

<imapmail>
<message>
<subject>Testing, 123</subject>
<sender>Name &lt;email@example.com&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 <em***@example.com>'
I ultimately want to user to see that the sender is "Name
<em***@example.com>". With the way the XML file is currently set up
(sender is Name &lt;email@example.com&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 <em***@example.com>'
which then will cause the parser to somehow try to interpret
<em***@example.com> 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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 16 '06 #2

P: n/a
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@example.com&gt;</sender> into
<sendername>Name</sendername> and
<senderemail>em***@example.com</senderemail>. That's easier to deal and
it makes more sense in the structure of my XML document.

Jan 17 '06 #3

P: n/a
JRS: In article <11*********************@o13g2000cwo.googlegroups. com>,
dated Sun, 15 Jan 2006 23:52:43 remote, seen in
news:comp.lang.javascript, Trevor <tr*********@gmail.com> posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jan 17 '06 #4

P: n/a
Thank you for the advice, I'll make sure I take care of that.

Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.