Importing XML using Javascript, same html file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dbsmilr@gmail.com

    Importing XML using Javascript, same html file

    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.getEle mentById("myXml ");
    alert(xmlDoc.ha sChildNodes);
    </script>
    </html>

    But that doesn't work, it says document.getEle mentById("xmlXm l") has
    no properties. How do I do a scenario like this? Thanks!
  • Bart Van der Donck

    #2
    Re: Importing XML using Javascript, same html file

    dbsm...@gmail.c om 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.getEle mentById("myXml ");
        alert(xmlDoc.ha sChildNodes);
      </script>
    </html>
    >
    But that doesn't work, it says document.getEle mentById("xmlXm l") 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:

    The W3Schools online code editor allows you to edit code and view the result in your browser


    Hope this helps,

    --
    Bart

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Importing XML using Javascript, same html file

      dbsmilr@gmail.c om 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.getEle mentById("myXml ");
      alert(xmlDoc.ha sChildNodes);
      </script>
      </html>
      >
      But that doesn't work, it says document.getEle mentById("xmlXm l") 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.getEle mentById() 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.getEle mentsByTagName( "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="&m yxml;">
      <head>
      <title>Embedd ed 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.getEle mentsByTagNameN S("&myxml;", "xml");

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

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


      PointedEars
      --
      var bugRiddenCrashP ronePieceOfJunk = (
      navigator.userA gent.indexOf('M SIE 5') != -1
      && navigator.userA gent.indexOf('M ac') != -1
      ) // Plone, register_functi on.js:16

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Importing XML using Javascript, same html file

        Bart Van der Donck wrote:
        dbsm...@gmail.c om 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.getEle mentById("myXml ");
        > alert(xmlDoc.ha sChildNodes);
        > </script>
        ></html>
        >>
        >But that doesn't work, it says document.getEle mentById("xmlXm l") 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:
        >
        The W3Schools online code editor allows you to edit code and view the result in your browser

        >
        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.


        Extensible Stylesheet Language Transformations (XSLT) is an XML-based language used, in conjunction with specialized processing software, for the transformation of XML documents.



        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>

        Comment

        • Bart Van der Donck

          #5
          Re: Importing XML using Javascript, same html file

          Thomas 'PointedEars' Lahn wrote:
          Bart Van der Donck wrote:
          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.or g.

          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://developer.mozilla.org/en/docs/XSLT
          My experiences with XSLT are not very positive. I would avoid it.

          --
          Bart

          Comment

          • Pavel Lepin

            #6
            Re: Importing XML using Javascript, same html file


            Bart Van der Donck <bart@nijlen.co mwrote in
            <1d6abb47-76cb-4cb5-9ab7-5ce5251761de@m4 4g2000hsc.googl egroups.com>:
            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.

            Comment

            Working...