Reading XML in Safari through selectNode

Discussion in 'Javascript' started by J_Zanetti, Jan 9, 2007.

  1. J_Zanetti

    J_Zanetti Guest

    Hello everybody,

    In my applications I've a ton of scripts that use remote XML file to
    fill forms and evaluate contents; In these scripts I always use the
    method SelectNode (that, with some workaround, works fine also in
    Mozilla).
    I've just found out that this method doesnt work in Safari browser,
    therefore my applications are not usable by this browser.
    Can anyone provide me any solution or workaround to be able to read XML
    files in Safari wihout rewriting all of my scripts??

    Thanks everyone :)
     
    J_Zanetti, Jan 9, 2007
    #1
    1. Advertising

  2. J_Zanetti wrote:

    > In my applications I've a ton of scripts that use remote XML file to
    > fill forms and evaluate contents; In these scripts I always use the
    > method SelectNode (that, with some workaround, works fine also in
    > Mozilla).


    Neither IE using MSXML nor Mozilla have any method called SelectNode
    exposed on XML DOM objects.
    MSXML provides two methods, one called selectSingleNode, the other
    called selectNodes. Are you using one of these?

    > I've just found out that this method doesnt work in Safari browser,
    > therefore my applications are not usable by this browser.
    > Can anyone provide me any solution or workaround to be able to read XML
    > files in Safari wihout rewriting all of my scripts??


    I don't think Safari has XPath support so far thus if you really use
    XPath expressions as arguments to that functions then you need to write
    more code. For other simple tasks you might get away with using
    getElementsByTagName e.g. you can replace
    xmlDoc.selectNodes('//element-name')
    with
    xmlDoc.getElementsByTagName('element-name')


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 9, 2007
    #2
    1. Advertising

  3. J_Zanetti

    J_Zanetti Guest

    Martin Honnen ha scritto:

    > MSXML provides two methods, one called selectSingleNode, the other
    > called selectNodes. Are you using one of these?


    Thanks for answering Martin!
    Yes, Im using selectNodes. Of course it works on IE, and in Mozilla
    only if i add some code that redefine the working of this method; to be
    precise, i add this code at the beginning of my script

    if( document.implementation.hasFeature("XPath", "3.0") )
    {
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }
    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
    aResult = aItems.snapshotItem(i);
    }
    return aResult;
    }
    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
    if(this.ownerDocument.selectNodes)
    {
    return this.ownerDocument.selectNodes(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }
    }

    This doesnt work under Safari, since Xpath is not supported there; thus
    should i write something similar for Safari? Honestly, i wouldnt know
    where to start from...

    > I don't think Safari has XPath support so far thus if you really use
    > XPath expressions as arguments to that functions then you need to write
    > more code. For other simple tasks you might get away with using
    > getElementsByTagName e.g. you can replace


    Yes, I use xpath expressions as parameters for the selectNodes method
    :(
    So If i got it well, there are two options: rewriting my scripts with
    getElementsByTagName or redefining selectNodes. Which way do you think
    is the best one?
     
    J_Zanetti, Jan 9, 2007
    #3
  4. J_Zanetti

    J_Zanetti Guest

    Martin Honnen ha scritto:

    > MSXML provides two methods, one called selectSingleNode, the other
    > called selectNodes. Are you using one of these?


    Thanks for answering Martin!
    Yes, Im using selectNodes. Of course it works on IE, and in Mozilla
    only if i add some code that redefine the working of this method; to be
    precise, i add this code at the beginning of my script

    if( document.implementation.hasFeature("XPath", "3.0") )
    {
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
    if( !xNode ) { xNode = this; }
    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
    aResult = aItems.snapshotItem(i);
    }
    return aResult;
    }
    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
    if(this.ownerDocument.selectNodes)
    {
    return this.ownerDocument.selectNodes(cXPathString, this);
    }
    else{throw "For XML Elements Only";}
    }
    }

    This doesnt work under Safari, since Xpath is not supported there; thus
    should i write something similar for Safari? Honestly, i wouldnt know
    where to start from...

    > I don't think Safari has XPath support so far thus if you really use
    > XPath expressions as arguments to that functions then you need to write
    > more code. For other simple tasks you might get away with using
    > getElementsByTagName e.g. you can replace


    Yes, I use xpath expressions as parameters for the selectNodes method
    :(
    So If i got it well, there are two options: rewriting my scripts with
    getElementsByTagName or redefining selectNodes. Which way do you think
    is the best one?
     
    J_Zanetti, Jan 9, 2007
    #4
  5. J_Zanetti

    RobG Guest

    J_Zanetti wrote:
    > Hello everybody,
    >
    > In my applications I've a ton of scripts that use remote XML file to
    > fill forms and evaluate contents; In these scripts I always use the
    > method SelectNode (that, with some workaround, works fine also in
    > Mozilla).
    > I've just found out that this method doesnt work in Safari browser,
    > therefore my applications are not usable by this browser.
    > Can anyone provide me any solution or workaround to be able to read XML
    > files in Safari wihout rewriting all of my scripts??


    Post a minimal example or a link. Safari supports the selectNode
    method as the following example shows:

    <script type="text/javascript">

    function foo(id) {
    var range = document.createRange();
    range.selectNode(document.getElementById(id));
    range.deleteContents();
    }

    </script>
    <button onclick="foo('div0');">Remove div0</button>
    <div id="div0">Here is div0</div>


    --
    Rob
     
    RobG, Jan 9, 2007
    #5
  6. J_Zanetti

    J_Zanetti Guest

    RobG ha scritto:

    > J_Zanetti wrote:
    > Post a minimal example or a link. Safari supports the selectNode
    > method as the following example shows:
    >
    > <script type="text/javascript">
    >
    > function foo(id) {
    > var range = document.createRange();
    > range.selectNode(document.getElementById(id));
    > range.deleteContents();
    > }
    >
    > </script>
    > <button onclick="foo('div0');">Remove div0</button>
    > <div id="div0">Here is div0</div>


    Thank you Rob!!
    Here you seem to be using the methond selectNode of the objectRange
    object.
    In my scripts, I have only Document object; would it be enough to
    define the Range for every document i have and then use its
    selectNode()?
    thanks :)
     
    J_Zanetti, Jan 9, 2007
    #6
  7. J_Zanetti

    J_Zanetti Guest

    RobG ha scritto:

    > J_Zanetti wrote:
    > Post a minimal example or a link. Safari supports the selectNode
    > method as the following example shows:
    >
    > <script type="text/javascript">
    >
    > function foo(id) {
    > var range = document.createRange();
    > range.selectNode(document.getElementById(id));
    > range.deleteContents();
    > }
    >
    > </script>
    > <button onclick="foo('div0');">Remove div0</button>
    > <div id="div0">Here is div0</div>


    Thank you Rob!!
    Here you seem to be using the methond selectNode of the objectRange
    object.
    In my scripts, I have only Document object; would it be enough to
    define the Range for every document i have and then use its
    selectNode()?
    thanks :)
     
    J_Zanetti, Jan 9, 2007
    #7
  8. J_Zanetti

    VK Guest

    > Reading XML in Safari through selectNodes

    Safari has to become a browser yet before talking about any extensive
    support. So far it is a rather rare case when buggy beta sketches being
    distributed as final release two times in the row (Safari 1.x and
    Safari 2.x) As I said in an older post I would understand it from any
    Acme, Inc. but not from Apple.
    Nightly builds of 3.0 are looking promising though: it seems that the
    critics produced some effect and Apple is finally targeted to produce a
    decent software product. I'm not saying "great" but at least "usable".

    For the time being if Safari support is an absolute must then you can
    try a library like ajaxslt:
    <http://code.google.com/p/ajaxslt/>
    former <http://sourceforge.net/projects/goog-ajaxslt/>

    This is "the guy who made it" :) - his project went from sourceforge
    to Google, so copyright and license changed a bit in comparison with
    the sourceforge stage. Still it remains free to use.

    ajaxslt works with Safari 1.2 or higher because it is a "screw them
    all" library. When Sarissa uses native XSLT tools of each browser,
    whatever it can find, and then covers all differences and bugs under
    one interface - ajaxslt-like libraries simply disregard any native XSLT
    tools and implement them from the scratch by javascript means. This is
    its power and weakness at the same time.
    The power is because it is much easier to maintain and to extent. It
    also make it really universal - with some modifications one could have
    "XSLT" on Netscape 4.x
    The weakness is because of lower level processing code they are using
    top level javascript statements. This way you may get a noticeable
    productivity impact depending on the kind and complexity of your
    transformations.
    Alas currently - until Apple starts to produce a usable browser - this
    is the only option to support XSLT on Safari.
     
    VK, Jan 10, 2007
    #8
  9. J_Zanetti

    Randy Webb Guest

    VK said the following on 1/10/2007 4:47 AM:
    >> Reading XML in Safari through selectNodes

    >
    > Safari has to become a browser yet before talking about any extensive
    > support.


    And you wonder why people say the things about you that get said.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jan 10, 2007
    #9
  10. J_Zanetti

    RobG Guest

    VK wrote:
    [...]
    > For the time being if Safari support is an absolute must then you can
    > try a library like ajaxslt:

    [...]
    > this is the only option to support XSLT on Safari.


    <URL: http://developer.apple.com/internet/safari/faq.html#anchor21 >

    It seemed to me that the OP was looking to use XML data to update the
    content of DOM elements. For that, DOM 2 Core methods will do the job.

    --
    Rob
     
    RobG, Jan 10, 2007
    #10
  11. J_Zanetti

    RobG Guest

    J_Zanetti wrote:
    > RobG ha scritto:
    >
    > > J_Zanetti wrote:
    > > Post a minimal example or a link. Safari supports the selectNode
    > > method as the following example shows:
    > >
    > > <script type="text/javascript">
    > >
    > > function foo(id) {
    > > var range = document.createRange();
    > > range.selectNode(document.getElementById(id));
    > > range.deleteContents();
    > > }
    > >
    > > </script>
    > > <button onclick="foo('div0');">Remove div0</button>
    > > <div id="div0">Here is div0</div>

    >
    > Thank you Rob!!
    > Here you seem to be using the methond selectNode of the objectRange
    > object.
    > In my scripts, I have only Document object; would it be enough to
    > define the Range for every document i have and then use its
    > selectNode()?


    I don't know, I don't know what you are trying to do - ranges are
    useful for manipulating document fragments in ways that aren't
    available using other DOM methods. I only suggested range.selectNode
    because I thought you'd made a typo with "SelectNode".

    As I understand it, you want to extract data from an XML document and
    update the values of DOM elements. For that you can use common DOM
    Core methods such as getElementsByTagName, getAttribute, nextSibling,
    etc. that probably have wider support than other methods.


    --
    Rob
     
    RobG, Jan 10, 2007
    #11
  12. J_Zanetti

    J_Zanetti Guest

    RobG ha scritto:

    > 21. Does Safari support XSLT?
    >
    > It seemed to me that the OP was looking to use XML data to update the
    > content of DOM elements. For that, DOM 2 Core methods will do the job.


    Thank you all guys!
    At the end i went to choose for rewriting my functions by using the DOM
    object functions.
    With getElementsByTagName and the data property, ive been able to get
    data for the XML as i needed to do. So finally, my application works
    fine in IE, Firefox and Safari too:)

    Thanks again
     
    J_Zanetti, Jan 11, 2007
    #12
    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:
    807
  2. CES
    Replies:
    2
    Views:
    106
    Martin Honnen
    Nov 30, 2004
  3. VUNETdotUS

    Parse XML in Safari?

    VUNETdotUS, Dec 5, 2007, in forum: Javascript
    Replies:
    7
    Views:
    242
    Thomas 'PointedEars' Lahn
    Dec 7, 2007
  4. Andrew Poulos

    xml load and safari 3

    Andrew Poulos, Feb 12, 2008, in forum: Javascript
    Replies:
    1
    Views:
    117
    Martin Honnen
    Feb 12, 2008
  5. jul
    Replies:
    1
    Views:
    135
Loading...

Share This Page