JavaScript to read XML attributes

Discussion in 'HTML' started by MP, Feb 4, 2008.

  1. MP

    MP Guest

    Could someone please help to find a JavaScript function to read the
    contents of an XML file?

    I want my XHTML code to read the value of a known attribute from an XML
    file. I.e. I know the attribute name, and I want to read the value from
    the XML file.

    I need to be able to run the XHTML code in a non-connected PC (i.e. a
    computer that is not connected to a network).
    Google Maps API provides the function GDownloadUrl(). However, it cannot
    be run on a non-connected PC.

    Finally, the function should be both browser and OS independent - well,
    at least to some extend.

    Could someone please help me to find such JavaScript code.
    Thanks!

    MP
    MP, Feb 4, 2008
    #1
    1. Advertising

  2. MP

    Andy Dingley Guest

    On 4 Feb, 11:50, MP <> wrote:
    > Could someone please help to find a JavaScript function to read the
    > contents of an XML file?


    Search for "AJAX"

    This will give you client-side techniques or pre-built frameworks to
    allow you to access XML from outside the client in a standardised
    cross-browser manner. Some AJAX frameworks are also server-side, which
    means they're server-side code (in a language like PHP, ASP/
    JavaScript, Java or Perl) that will in turn generate client-side AJAX
    code fragments embedded in a HTML page.

    All these AJAX tools revolve around downloading XML docuemnts from a
    server by means of XMLHTTPRequest or similar objects (hiding this
    variation behind a consistent facade is important for AJAX's cross-
    browser support). Many also support non-XML formats, such as JSON, but
    that doesn't concern us here. If you modify them so that they'll
    retrieve this XML from elsewhere (such as a file:// URL) then you can
    use practically the same code to do what you need.


    > I need to be able to run the XHTML code in a non-connected PC (i.e. a
    > computer that is not connected to a network).


    Remember that there are security restrictions on where JavaScript can
    load resources from. If you're trying to load local files, you'll
    probably run into these. It's workable, but you'll have to
    specifically permit some security settings per-browser to allow
    access. This restriction will bite you however you write the code,
    it's not an AJAX thing.


    > I want my XHTML code to read the value of a known attribute from an XML
    > file.


    If this is quite simple, and only accessible from your web app, then
    cookies might be a simpler way to do it.
    Andy Dingley, Feb 4, 2008
    #2
    1. Advertising

  3. MP

    MP Guest

    Andy Dingley wrote:
    > On 4 Feb, 11:50, MP <> wrote:

    ....
    >


    Thanks!

    But, don't I need an AJAX server or something? Do I need to do something
    on the server side?

    I have no web server on my PC, and my ISP support me very limited
    services (basically I only can upload files, which are accessible by a
    browser).

    When I say "reading XML file on the HD", I mean "reading XML file in the
    same folder as the XHTML file, which may be on the HD".
    In other words, I would have the (X)HTML file taking a command line
    parameter which tells the XML file to read. The (X)HTML code then uses
    the contents of the XML file, to build up the content that the browser
    will show. Yet, slightly more complicated that what I could do with
    CSS+XML only.

    The idea of using such "reading the content from XML" I got from here
    (this uses the GDownloadUrl()):
    http://econym.googlepages.com/basic3.htm

    I have written the XHTML (and XML) files, but am not happy to use the
    GDownloadUrl()as it requires Internet connection to run.


    MP
    MP, Feb 4, 2008
    #3
  4. MP

    Andy Dingley Guest

    On 4 Feb, 14:11, MP <> wrote:

    > But, don't I need an AJAX server or something? Do I need to do something
    > on the server side?


    Only if you use AJAX. I'm not suggesting this, I'm suggesting taking
    avilable AJAX code and modifying it so that it loads file-based
    resources instead. This is just a small change to a large body of
    useful code that you can re-use.

    Also an "AJAX server" can be very simple. At its simplest it's just a
    static web server serving static XML documents.

    > I have no web server on my PC,


    You could always install one. It's really not hard (check for adequate
    memory, then download and install a copy of Apache, even under
    Windows)

    > and my ISP support me very limited services
    > (basically I only can upload files, which are accessible by a
    > browser).


    They probably run the Apache web server, set to serve only static
    files (maybe a pre-existing counter script or mailform handler too).
    You can still use this to serve XML files, just not to generate them
    dynamically, or to save changes written back to the server.


    > When I say "reading XML file on the HD", I mean "reading XML file in the
    > same folder as the XHTML file, which may be on the HD".


    Yes, this is easy enough - however you'll have to adjust security to
    permit it.


    > In other words, I would have the (X)HTML file taking a command line
    > parameter which tells the XML file to read.


    URL parameter more likely. You can generate this (for most browsers)
    from a command line parameter, but the page itself only gets to see
    the URL.

    > The (X)HTML code then uses
    > the contents of the XML file, to build up the content that the browser
    > will show. Yet, slightly more complicated that what I could do with
    > CSS+XML only.


    You might find XSLT a more appropriate languge than JavaScript though.
    Andy Dingley, Feb 4, 2008
    #4
  5. MP

    Jeff Guest

    MP wrote:
    > Could someone please help to find a JavaScript function to read the
    > contents of an XML file?
    >
    > I want my XHTML code to read the value of a known attribute from an XML
    > file. I.e. I know the attribute name, and I want to read the value from
    > the XML file.


    You'll hit some security bumps along the way, but probably nothing that
    can't be "fixed" with some user intervention.

    I have no particular expertise in this, so take that as a caution.

    Load your XML doc into an IFRAME. That's as simple as setting the src of
    the IFRAME to the xml doc. You may wish to hide the IFRAME by either
    styling it or shrinking it.

    You should be able to parse what you need out of that IFRAME by using
    getElementsByTagname and a little mumbo jumbo. I'm assuming though that
    your question was more about loading the doc rather than parsing it.

    Doubtless that there are more complex ways of doing this, but that I
    think may be the simplest. I think IFRAMEs are part of HTML4, so they
    should be widely supported. Don't use them myself though...

    HTH,

    Jeff



    >
    > I need to be able to run the XHTML code in a non-connected PC (i.e. a
    > computer that is not connected to a network).
    > Google Maps API provides the function GDownloadUrl(). However, it cannot
    > be run on a non-connected PC.
    >
    > Finally, the function should be both browser and OS independent - well,
    > at least to some extend.
    >
    > Could someone please help me to find such JavaScript code.
    > Thanks!
    >
    > MP
    Jeff, Feb 4, 2008
    #5
  6. MP

    Andy Dingley Guest

    On 4 Feb, 15:45, Jeff <jeff@spam_me_not.com> wrote:

    > Load your XML doc into an IFRAME.


    <iframe>s are for HTML, not XML. Even if the content is XHTML, this
    will cause problems with IE (unless you follow Appendix C).
    Andy Dingley, Feb 4, 2008
    #6
  7. MP

    MP Guest

    Thanks again!
    Particularly answering my entry level questions!


    Yes, I meant parameters on URL, not on command line - didn't remember
    the right term :)


    Next I will need to go studying AJAX coding, and to lear how to "modify
    available AJAX code to load file-based resources".
    Also, XSLT is a new acronym for me. Any proposal where to start to lear it?


    BTW: I don't think I can install a web browser on my local PC. Its owned
    (and controlled) by my employer...


    I created an example XHTML page, to demonstrate what I want to do.
    The page takes one or two URL parameters:
    - fig="XLS filename" (required)
    - lang="Language" (optional)

    Here's a link to the page:
    http://www.elisanet.fi/matti.puputti/shared/xml_test/kuva.htm?fig=img_3376&lang=en
    and the XML file it reads:
    http://www.elisanet.fi/matti.puputti/shared/xml_test/img_3376.xml

    The code works just fine both locally and on web. However, the
    GDownloadUrl() -function is loaded over the Internet, and therefore
    cannot be used in a non-connected PC.
    (I know it works on a Windows PC, on most common browsers. I don't know
    how it works on Linux)


    MP
    MP, Feb 4, 2008
    #7
  8. MP

    MP Guest

    Thanks again!
    Particularly answering my entry level questions!


    Yes, I meant URL, not command line - didn't remember the right term :)


    Next I will need to go studying AJAX coding, and to lear how to "modify
    available AJAX code to load file-based resources"
    Also, XSLT is a new acronym for me. Any proposal where to start to lear it?


    BTW: I don't think I can install a web browser on my local PC. Its owned
    (and controlled) by my employer...


    I created an example XHTML page, to demonstrate what I want to do.
    The page takes one or two URL parameters:
    - XLS filename (required)
    - Language (optional)

    Here's a link to the page:
    http://www.elisanet.fi/matti.puputti/shared/xml_test/kuva.htm?fig=img_3376&lang=en
    and the XML file it reads:
    http://www.elisanet.fi/matti.puputti/shared/xml_test/img_3376.xml

    The code works just fine both locally and on web. However, the
    GDownloadUrl() -function is loaded over the Internet, and therefore
    cannot be used in the non-connected mode.
    (I know it works on a Windows PC, I don't know how it works on Linux)

    MP
    MP, Feb 4, 2008
    #8
  9. MP

    MP Guest

    Ok, so I googled for XSLT-tutorials, and browsed quickly one of them
    (http://www.w3schools.com/xsl/)
    It looks very promising - in fact, this may be exactly what I need.


    However, I still have one (entry level?) question: Can I use XSLT on
    Safari browser?

    The example (on page http://www.w3schools.com/xsl/xsl_client.asp) seems
    to work fine on most of my browsers (IE 6, Firefox 2.0.0.8, Opera 9.25).
    But it doesn't seem to work on the Safari (3.0.4, Win version). How can
    I get it working on Safari?


    MP
    MP, Feb 4, 2008
    #9
  10. MP

    MP Guest

    It wasn't too difficult with XSLT, I already wrote the required code.
    And it works on all my graphical browsers (including Safari and Netscape
    7) :)

    I'm not happy with how my code handles languages, though. I had to make
    different XML files for different languages. There should be another way
    for the user to select between supported languages.

    The code I generated is here:
    http://www.elisanet.fi/matti.puputti/kuvat/test_xsl/img_9777_pt_en.xml

    User selects the language by clicking the flag at the top of the page.
    (currently only two options: english and finnish)
    How could I pass the user preferred language option to the XLS code, and
    to let the user change the preference during the run-time?

    MP
    MP, Feb 5, 2008
    #10
    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. Max
    Replies:
    1
    Views:
    471
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,793
    bruce barker
    Jan 31, 2007
  3. Replies:
    3
    Views:
    2,797
  4. Replies:
    4
    Views:
    834
    Joe Kesselman
    Nov 30, 2006
  5. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    1
    Views:
    286
    bruce barker
    Mar 8, 2007
Loading...

Share This Page