Results 1 to 3 of 3

Thread: issues loading XML in Firefox

  1. #1
    Join Date
    Aug 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy issues loading XML in Firefox

    Ive been banging my head on my keyboard for a few hours now with this issue. This about is basic as it can get. Im trying to load XML data and post it to div's with ID's, but when i run it in Firefox the java console says:

    "xmlDoc.getElementsByTagName("title").item(0).text has no properties"

    It works fine in IE.


    html page
    PHP Code:
    <html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    script type="text/javascript" >

    var 
    article "news1.xml";
    var 
    xmlDoc;
    function 
    loadArticle()
    {

            
    //for IE
            
    if (window.ActiveXObject)
            {
                
    xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
                
    xmlDoc.async="false";
                
    xmlDoc.load(article);
                
    displayXML();
            }
            
    //for FF
            
    else if (document.implementation && document.implementation.createDocument)
            {
                
    xmlDoc document.implementation.createDocument("","",null);
                
                
    xmlDoc.load(article);
                
    displayXML();
            }

            else if(
    xmlDoc)
            
    alert('Sorry, this browser is not XML-compliant and cannot render the XML data.');

    }

    function 
    displayXML(){

    document.getElementById("title").innerHTML xmlDoc.getElementsByTagName("title").item(0).text;
    subtitle.innerTexttitle.innerText xmlDoc.getElementsByTagName("subtitle").item(0).text;
    author.innerText=xmlDoc.getElementsByTagName("author").item(0).text;
    main.innerText=xmlDoc.getElementsByTagName("main_text").item(0).text ;
    pict_path.innerText=xmlDoc.getElementsByTagName("pict_path").item(0).text;

    }
    </script>

    </head>
    <body onload=loadArticle()>



    <div id = "title"></div>
    <div id = "subtitle"></div>
    <div id = "main"></div>
    <div id = "author"></div>

    </body>
    </html> 
    news1.xml
    PHP Code:
    <?xml version="1.0"?>

    <!DOCTYPE article [

       <!ELEMENT article (section, pict_path, title, subtitle, author, main_text)>
       <!ELEMENT section (#PCDATA)>
       <!ELEMENT pict_path (#PCDATA)>
       <!ELEMENT author (#PCDATA)>

       <!ELEMENT title (#PCDATA)>

       <!ELEMENT subtitle (#PCDATA)>

       <!ELEMENT main_text (#PCDATA)>

    ]>




       <article>
               <section>news</section>
           <pict_path>images/current/news1.gif</pict_path>
          <title>Chocolate Chip Bars</title>      
          <subtitle>Chocolate Chip Bars sub</subtitle>
          <author>Carol Schmidt</author>
          <main_text>
    Preheat oven to 350 degrees. Melt butter;
    combine with brown sugar and vanilla in large mixing bowl.

    Set aside to cool.  Combine flour, baking powder, and salt; set aside.

    Add eggs to cooled sugar mixture; beat well.  Stir in reserved dry
    ingredients, nuts, and chips.

    Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes until golden
    brown; cool.  Cut into squares.         

          </main_text>
          </article>

  2. #2
    Join Date
    Aug 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default c'mon

    Have i truly stumped the Guru's of Dynamic Drive?!?!?

  3. #3
    Join Date
    Aug 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have had the same problem and just found out how to get it working. Apparently the function that processes the XML was called before the xml document was completely loaded. The following code should work for you, it checks if the document is loaded and will not calls displayXML() before that.

    Code:
    if (document.implementation && document.implementation.createDocument)
            {
                xmlDoc = document.implementation.createDocument("","",null);
                var isLoaded = xmlDoc.load(article);
                if (isLoaded == true) {  displayXML();  }
            }
    You will need to make a similar check for IE :
    Code:
    if (window.ActiveXObject)
            {
                xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
                xmlDoc.async="false";
                while (xmlDoc.readyState != 4) {} // readyState will be set to 4 when the document is loaded
                xmlDoc.load(article);
                displayXML();
            }

Bookmarks

Posting Permissions

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