Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cross Browser XML Parsing...

    Hello, can anyone help me? I need to create a way to parse XML on the client-side in as many browsers as possible. Can someone give me a link or something, thanks a lot.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there a way to do this so that I can use an XSL transform sheet instead of doing all this javascript stuff? Thanks a lot.
    ~evlich

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are there any ways to do it with XSL stylesheet?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    XSLT1.0 will work in IE6, NS7(I believe) and Mozilla 1.0+ only

    patrick

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is ultimately what I want to do:
    Write a function
    Code:
    function loadXMLSource("XML_File_Name", "XSL_File_Name", "HTML element that I want to put the output into")
    {
    ...
    }
    and I want to know what I need to put in for the "..." I already know the stuff with the MSXML engine and stuff like that, but logic is telling me that since Microsoft didn't make Netscape or any other browser, that MSXML is not supported in the other browsers. Is there a way to do this? Thanks a lot.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Jason posted this some time ago and it works for IE6 and Mozilla:

    <html>
    <head>
    <title>Importing and Manipulating XML</title>
    <script language="javascript">

    /*************************************
    DECLARE GLOBAL VARIABLES
    *************************************/
    var c
    var d='';
    var e='';
    var f='';
    var g='';
    var xml_doc

    /*************************************
    MOZILLA - EXTRACT NODE INFORMATION
    *************************************/
    function readFileMoz()
    {
    node_list = xml_doc.getElementsByTagName("RESTAURANT")
    for (i = 0; i <= node_list.length-1; i++)
    {
    c = node_list[i].childNodes[1].childNodes[0].nodeValue
    d = node_list[i].childNodes[3].childNodes[0].nodeValue
    //e = node_list[i].childNodes[5].nodeValue
    //f = node_list[i].childNodes[7].nodeValue
    //g = node_list[i].childNodes[9].nodeValue
    alert("C = " + c + " D = " + d)
    }
    }


    /*************************************
    INTERNET EXPLORER - EXTRACT NODE INFORMATION
    *************************************/
    function readFileIE()
    {
    node_list = xml_doc.getElementsByTagName("RESTAURANT")

    for (i = 0; i <= node_list.length-1; i++)
    {
    c = node_list[i].childNodes[0].childNodes[0].nodeValue
    d = node_list[i].childNodes[1].childNodes[0].nodeValue
    //e = node_list[i].childNodes[2].nodeValue
    //f = node_list[i].childNodes[3].nodeValue
    //g = node_list[i].childNodes[4].nodeValue
    alert("C = " + c + " D = " + d)
    }

    }


    /*************************************
    This function loads the file.
    *************************************/
    function importXML(zFileName)
    {
    var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
    var ie = (typeof window.ActiveXObject != 'undefined')

    if (moz)
    {
    xml_doc = document.implementation.createDocument("", "", null)
    xml_doc.onload = readFileMoz
    }

    else if (ie)
    {
    xml_doc = new ActiveXObject("Microsoft.XMLDOM")
    xml_doc.onreadystatechange = function()
    {
    if (xml_doc.readyState == 4) setTimeout(readFileIE,0)
    }
    }

    xml_doc.load(zFileName)

    }

    importXML('cust.xml');

    </script>


    </head>
    <body>
    </body>
    </html>

    This is the xml file which goes with it:

    <?xml version="1.0"?>

    <LIST>

    <RESTAURANT>
    <NAME>A Special Place</NAME>
    <ADDRESS>3124 Williamsburg Dr.</ADDRESS>
    </RESTAURANT>

    <RESTAURANT>
    <NAME>Al Fresco Cafe</NAME>
    <ADDRESS>3398 El Camino Real</ADDRESS>
    </RESTAURANT>

    </LIST>

    patrick

  • #8
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, but what heppens if I don't exactly know what the tags in it are going to be. For instance from this I may load a list of numbers, or I may load a paragraph which would each have different tags?

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You must have some idea of what tags are going to be there; you can't parse an xml doc without referring to the tags. That's what DTD is for.

  • #10
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, the thing is I could use this to import xml files that work of different DTD's correct. I would have a different stylesheet for each one that would define the way to manipulate only that data. For instance, one of the things that I am planning on doing this to is the menu on my site. I wanted to do a heirarchal menu and was going to use
    <menu>
    <submenu>
    <item>
    with various attributes. However, I would like to use the same function to inport table data, like a table of contents which might be like this:
    <toc>
    <section>
    <title href="">
    <pageN>
    </section>
    Is it possible to parse them off the same function?

  • #11
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Evlich,

    The example that mpjbrennan used was when someone was trying to explain all of this stuff to me. Listen... for your own sake, do not use this "node_list[i].childNodes[0].childNodes[0].nodeValue" stuff. Use XML tag names instead. That way, you don't have to worry about IE/Moz whitespace crap...

    I'd suggest using something like this:

    If you have an XML file named "names.xml" that looks like this,
    Code:
    <?xml version="1.0"?>
    <DOC>
    	<DATA>Bobby</DATA>
    	<DATA>Fredo</DATA>
    	<DATA>Miguel</DATA>
    </DOC>
    You'll want your script to do something like this:

    Code:
    /*************************************
    This function loads the file when run.
    *************************************/
    function importXML(zFileName)
    {
    	var xmlDoc;
    	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
    	var ie = (typeof window.ActiveXObject != 'undefined');
    
    	if (moz)
    	{
    		xmlDoc = document.implementation.createDocument("", "", null)
    		xmlDoc.onload = readXML;
    	}
    
    	else if (ie)
    	{
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.onreadystatechange = function()
    		{
    			if (xmlDoc.readyState == 4) setTimeout(readXML,0);
    		}
    	}
    
    	xmlDoc.load(zFileName);
    }
    
    
    /*************************************
    This is the function that is run when importXML()
    is called...
    *************************************/
    function readXML()
    {
    	var xmlFile=xmlDoc.getElementsByTagName("DOC"); // There is only one DOC tag, so this is a variable instead of an array.
    	var xmlFileData=xmlFile.getElementsByTagName("DATA"); // There is more than one DATA tag, so this becomes an array.  The first DATA tag (value is Bobby) can be accessed as xmlData[0], the next xmlData[1], and so on.
    
    	for (x=0; x < xmlFileData.length; x++) // Since this is an array...
    	{
    		document.write(xmlFileData[x] + '<br />');
    	}
    }
    
    
    /*************************************
    Actually import the document.
    *************************************/
    var loc=location.href;
    if (loc.indexOf('.htm?') != -1)
    {
    	var impurl=loc.split('?');
    	importXML(impurl[1]);
    }
    else alert('You need to specify an XML document to load.');
    With this, you can use the same HTML page to load different XML files one at a time. For example, you would pull up a new page by going to "http://www.domain.com/filename.htm?names.xml". After the filename.htm, you would add a ? followed by the name of the XML file, not quotated.

    Granted, you can do much more with the readXML() function than this, but this is just something that you can look at to figure out where you can go from here.

    --Sky

  • #12
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks great. Only one question. You wrote this function:
    Code:
    /*************************************
    This is the function that is run when importXML()
    is called...
    *************************************/
    function readXML()
    {
    	var xmlFile=xmlDoc.getElementsByTagName("DOC"); // There is only one DOC tag, so this is a variable instead of an array.
    	var xmlFileData=xmlFile.getElementsByTagName("DATA"); // There is more than one DATA tag, so this becomes an array.  The first DATA tag (value is Bobby) can be accessed as xmlData[0], the next xmlData[1], and so on.
    
    	for (x=0; x < xmlFileData.length; x++) // Since this is an array...
    	{
    		document.write(xmlFileData[x] + '<br />');
    	}
    }
    I see what you are doing, you are getting the data from the data elements and putting them in an array and stuff like that, but is it possible to instead to saying:
    Code:
    document.write(xmlFileData[x] + '<br />');
    to say something that would apply a template in an xsl file to the node?
    Thanks a lot.
    ~evlich


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •