Reading XML with Javascript

Discussion in 'XML' started by Trevor, Jan 16, 2006.

  1. Trevor

    Trevor Guest

    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;&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
    <>". With the way the XML file is currently set up
    (sender is Name &lt;&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
    Trevor, Jan 16, 2006
    #1
    1. Advertising

  2. Trevor wrote:


    > <imapmail>
    > <message>
    > <subject>Testing, 123</subject>
    > <sender>Name &lt;&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 <>'

    > I ultimately want to user to see that the sender is "Name
    > <>". With the way the XML file is currently set up
    > (sender is Name &lt;&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 <>'
    which then will cause the parser to somehow try to interpret
    <> 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/
    Martin Honnen, Jan 16, 2006
    #2
    1. Advertising

  3. Trevor

    Trevor Guest

    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;&gt;</sender> into
    <sendername>Name</sendername> and
    <senderemail></senderemail>. That's easier to deal and
    it makes more sense in the structure of my XML document.
    Trevor, Jan 17, 2006
    #3
  4. JRS: In article <>,
    dated Sun, 15 Jan 2006 23:52:43 remote, seen in
    news:comp.lang.javascript, Trevor <> 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)
    Dr John Stockton, Jan 17, 2006
    #4
  5. Trevor

    Trevor Guest

    Thank you for the advice, I'll make sure I take care of that.
    Trevor, Jan 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    780
  2. John Nash
    Replies:
    0
    Views:
    149
    John Nash
    Apr 17, 2004
  3. Erik Wasser
    Replies:
    5
    Views:
    449
    Peter J. Holzer
    Mar 5, 2006
  4. Trevor

    Reading XML with Javascript

    Trevor, Jan 16, 2006, in forum: Javascript
    Replies:
    4
    Views:
    108
    Trevor
    Jan 18, 2006
  5. noff
    Replies:
    0
    Views:
    204
Loading...

Share This Page