Importing XML using Javascript, same html file

Discussion in 'Javascript' started by dbsmilr@gmail.com, Apr 12, 2008.

  1. Guest

    This is what I want to do:

    <html>
    <xml id="myXml">
    <book title="book1" />
    <book title="book2" />
    </xml>
    <div id="out"><!-- I would output the xml formatted nicely for the
    user in this div</div>

    <script type="text/javascript">
    var xmlDoc = document.getElementById("myXml");
    alert(xmlDoc.hasChildNodes);
    </script>
    </html>

    But that doesn't work, it says document.getElementById("xmlXml") has
    no properties. How do I do a scenario like this? Thanks!
    , Apr 12, 2008
    #1
    1. Advertising

  2. wrote:

    > This is what I want to do:
    >
    > <html>
    >   <xml id="myXml">
    >     <book title="book1" />
    >     <book title="book2" />
    >   </xml>
    >   <div id="out"><!-- I would output the xml formatted nicely for
    > the user in this div</div>
    >
    >   <script type="text/javascript">
    >     var xmlDoc = document.getElementById("myXml");
    >     alert(xmlDoc.hasChildNodes);
    >   </script>
    > </html>
    >
    > But that doesn't work, it says document.getElementById("xmlXml") has
    > no properties.  How do I do a scenario like this?  Thanks!


    XML-data should always be approached with the XML-parser of the
    browser. Here is a nice example of string parsing:

    http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2

    Hope this helps,

    --
    Bart
    Bart Van der Donck, Apr 12, 2008
    #2
    1. Advertising

  3. wrote:
    > This is what I want to do:
    >
    > <html>
    > <xml id="myXml">
    > <book title="book1" />
    > <book title="book2" />
    > </xml>
    > <div id="out"><!-- I would output the xml formatted nicely for the
    > user in this div</div>
    >
    > <script type="text/javascript">
    > var xmlDoc = document.getElementById("myXml");
    > alert(xmlDoc.hasChildNodes);
    > </script>
    > </html>
    >
    > But that doesn't work, it says document.getElementById("xmlXml") has
    > no properties. How do I do a scenario like this?


    The first thing you would need to make sure is Valid markup. The above is
    not. document.getElementById() where `document' refers to an object
    implementing the HTMLDocument interface finds *HTML* elements with
    attributes of type ID; `xml' is not an HTML element.

    You might have better luck with document.getElementsByTagName("xml")[0] and
    continuing from there. But that does not make your markup Valid; to embed
    XML in HTML, you have to use XHTML and namespaces, for example:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" [
    <!ENTITY myxml "http://foo.example/bar">
    ]>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
    xmlns:myxml="&myxml;">
    <head>
    <title>Embedded XML Data</title>
    </head>

    <body>
    <myxml:xml>
    <myxml:book title="book1" />
    <myxml:book title="book1" />
    </myxml:xml>

    <script type="text/javascript">
    // works only with application/xhtml+xml
    var xmlDoc = document.getElementsByTagNameNS("&myxml;", "xml");

    // "[object NodeList]" in Gecko
    window.alert(xmlDoc[0].childNodes);
    </script>
    </body>
    </html>

    That said, you should solve this with an external XML resource and XSLT,
    preferably server-side, instead.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Apr 12, 2008
    #3
  4. Bart Van der Donck wrote:
    > wrote:
    >> <html>
    >> <xml id="myXml">
    >> <book title="book1" />
    >> <book title="book2" />
    >> </xml>
    >> <div id="out"><!-- I would output the xml formatted nicely for
    >> the user in this div</div>
    >>
    >> <script type="text/javascript">
    >> var xmlDoc = document.getElementById("myXml");
    >> alert(xmlDoc.hasChildNodes);
    >> </script>
    >> </html>
    >>
    >> But that doesn't work, it says document.getElementById("xmlXml") has
    >> no properties. How do I do a scenario like this? Thanks!

    >
    > XML-data should always be approached with the XML-parser of the
    > browser. Here is a nice example of string parsing:
    >
    > http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2
    >
    > Hope this helps,


    This unsurprisingly inefficient, error-prone, incompatible, invalid example
    cannot help with the OP's problem since the OP is not getting at the invalid
    `xml' element object to begin with, as the error message indicates (`null'
    has no properties).

    Using XSLT to transform the XML markup into HTML markup, and CSS to format
    the HTML markup, is the proper way here, and if XSLT is used server-side it
    degrades gracefully.

    http://en.wikipedia.org/wiki/XSLT
    http://developer.mozilla.org/en/docs/XSLT


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
    Thomas 'PointedEars' Lahn, Apr 12, 2008
    #4
  5. Thomas 'PointedEars' Lahn wrote:

    > Bart Van der Donck wrote:


    >> http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2


    > This unsurprisingly inefficient, error-prone, incompatible, invalid example
    > cannot help with the OP's problem since the OP is not getting at the invalid
    > `xml' element object to begin with, as the error message indicates (`null'
    > has no properties).


    That javascript code is less optimal indeed. I think the main issue is
    the bad exception handling (which involves feature detection too) on
    several points. Also best to correct the errors of validator.w3.org.

    In a strict sense, your error message hasn't anything to do with the
    application. The XML-data should be valid and well-formed; that is
    Ipso Facto the responsibility of the data itself and not from the
    application that parses it (which of course doesn't mean that it
    shouldn't be checked anymore).

    > Using XSLT to transform the XML markup into HTML markup, and CSS to format
    > the HTML markup, is the proper way here, and if XSLT is used server-side it
    > degrades gracefully.
    >
    > http://en.wikipedia.org/wiki/XSLT
    > http://developer.mozilla.org/en/docs/XSLT


    My experiences with XSLT are not very positive. I would avoid it.

    --
    Bart
    Bart Van der Donck, Apr 17, 2008
    #5
  6. Pavel Lepin Guest

    Bart Van der Donck <> wrote in
    <>:
    > Thomas 'PointedEars' Lahn wrote:
    >> Using XSLT to transform the XML markup into HTML markup,
    >> and CSS to format the HTML markup, is the proper way
    >> here, and if XSLT is used server-side it degrades
    >> gracefully.
    >>
    >> http://en.wikipedia.org/wiki/XSLT
    >> http://developer.mozilla.org/en/docs/XSLT

    >
    > My experiences with XSLT are not very positive. I would
    > avoid it.


    My experiences with XSLT were very positive. I wouldn't
    avoid it.

    Just a second opinion.

    --
    I'm not dead, just pinin' for the fnords.
    Pavel Lepin, Apr 17, 2008
    #6
    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. Dhanasankar S via DotNetMonster.com

    How to read the BODY of HTML file from another HTML file using javascript?

    Dhanasankar S via DotNetMonster.com, Feb 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    463
    =?Utf-8?B?dHBlcnJp?=
    Feb 28, 2005
  2. darrel
    Replies:
    2
    Views:
    470
    darrel
    Apr 5, 2006
  3. qurat
    Replies:
    13
    Views:
    670
    Toby A Inkster
    Feb 29, 2004
  4. plb
    Replies:
    2
    Views:
    347
  5. Visco Shaun
    Replies:
    3
    Views:
    253
    Gabriel Genellina
    Apr 22, 2009
Loading...

Share This Page