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 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    XML and Javascript - Ajax - Need help navigating the DOM object

    Hi guys,

    I have pretty large project due on friday and im stuck, I won't be sending you guys all the project files so i'll focus on where i'm having issue ...

    After the call has been made to AJAX, I receive my XML file back

    Code:
    var domTreeText = xhr.responseText;
    		alert(domTreeText);
    the alert shows this ....

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xml>
    <livres>
        <idCateg>1</idCateg>
        <idLivre>829974</idLivre>
        <titre>Lexique érotique illustré</titre>
        <auteur>DELVAU ALFRED</auteur>
        <prix>6.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>1</idCateg>
        <idLivre>815264</idLivre>
        <titre>Un Privilège à votre table</titre>
        <auteur>TREMBLAY DIANE</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>1</idCateg>
        <idLivre>967477</idLivre>
        <titre>Stars de cinéma</titre>
        <auteur>MANFERTO DE FABIANIS VALERIA &</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>1</idCateg>
        <idLivre>1074537</idLivre>
        <titre>L'Amour au menu</titre>
        <auteur>ARSENAULT LINDA</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>2</idCateg>
        <idLivre>1192373</idLivre>
        <titre>L'Atelier du grand Verrocchio</titre>
        <auteur>LEGAULT MATTHIEU</auteur>
        <prix>2.95</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>2</idCateg>
        <idLivre>1189596</idLivre>
        <titre>La chute de Sparte</titre>
        <auteur>BIZ</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>2</idCateg>
        <idLivre>1195198</idLivre>
        <titre>Un vrai casse-tête</titre>
        <auteur>DESCHENEAUX NADINE & LAPLANTE </auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>2</idCateg>
        <idLivre>1192379</idLivre>
        <titre>Le Rêve d'Émilie</titre>
        <auteur>JADE BÉRUBÉ</auteur>
        <prix>0.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>3</idCateg>
        <idLivre>1198335</idLivre>
        <titre>Mise à nu</titre>
        <auteur>CASTLE RICHARD</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>3</idCateg>
        <idLivre>1206018</idLivre>
        <titre>Les Souvenirs</titre>
        <auteur>FOENKINOS DAVID</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>3</idCateg>
        <idLivre>1194006</idLivre>
        <titre>La Femme au miroir</titre>
        <auteur>SCHMITT ÉRIC-EMMANUEL</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>3</idCateg>
        <idLivre>1202053</idLivre>
        <titre>100 plus beaux sites du monde</titre>
        <auteur>COLLECTIF</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>4</idCateg>
        <idLivre>1198098</idLivre>
        <titre>Les Invisibles</titre>
        <auteur>WINCHLER MARTIN</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>4</idCateg>
        <idLivre>1203302</idLivre>
        <titre>Le Rêve d'Émily</titre>
        <auteur>BÉRUBÉ JADE</auteur>
        <prix>0.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>4</idCateg>
        <idLivre>1209460</idLivre>
        <titre>Rose déluge</titre>
        <auteur>AWUMEY EDEM</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>4</idCateg>
        <idLivre>1205047</idLivre>
        <titre>Ivresse</titre>
        <auteur>MCKENZIE CATHERINE</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>5</idCateg>
        <idLivre>1203854</idLivre>
        <titre>Avalon High</titre>
        <auteur>CABOT MEG</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>5</idCateg>
        <idLivre>1193948</idLivre>
        <titre>Otages N. éd.</titre>
        <auteur>POIRIER CLAUDE</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>5</idCateg>
        <idLivre>1198791</idLivre>
        <titre>Le Sentier de la gloire</titre>
        <auteur>ARCHER JEFFREY</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>5</idCateg>
        <idLivre>1203182</idLivre>
        <titre>Amore 14</titre>
        <auteur>MOCCIA FEDERICO</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>6</idCateg>
        <idLivre>1193942</idLivre>
        <titre>La Main d'Iman</titre>
        <auteur>ASSANI-RAZAKI RYAD</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>6</idCateg>
        <idLivre>1199869</idLivre>
        <titre>Des vies d'oiseaux</titre>
        <auteur>OVALDÉ VÉRONIQUE</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>6</idCateg>
        <idLivre>1190603</idLivre>
        <titre>Rouge dragon</titre>
        <auteur>DE VILLIERS GÉRARD</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>6</idCateg>
        <idLivre>1138711</idLivre>
        <titre>Le Paris de Gainsbourg</titre>
        <auteur>LEIBOWITCH ERSIN & LORIOU DOMI</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>7</idCateg>
        <idLivre>1201037</idLivre>
        <titre>A chacun son podium</titre>
        <auteur>SYLVIE FRÉCHETTE</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>7</idCateg>
        <idLivre>1194558</idLivre>
        <titre>La maladie d'Alzheimer</titre>
        <auteur>SERGE GAUTHIER JUDES POIRIER</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>7</idCateg>
        <idLivre>1202313</idLivre>
        <titre>Démons quotidiens</titre>
        <auteur>NANCY HUSTON RAPH PETTY</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>7</idCateg>
        <idLivre>1194556</idLivre>
        <titre>Su : la cuisine turque de Fisun Ercan</titre>
        <auteur>FISUN ERCAN</auteur>
        <prix>9.99</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>8</idCateg>
        <idLivre>163</idLivre>
        <titre>TASHEN</titre>
        <auteur>T.B.A</auteur>
        <prix>0.00</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>8</idCateg>
        <idLivre>1928</idLivre>
        <titre>Réveiller le sommelier en vous</titre>
        <auteur>T.B.A</auteur>
        <prix>0.00</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>8</idCateg>
        <idLivre>1931</idLivre>
        <titre>Prix littéraires</titre>
        <auteur>T.B.A</auteur>
        <prix>0.00</prix>
        <qteInvent>20</qteInvent>
    </livres>
    <livres>
        <idCateg>8</idCateg>
        <idLivre>2271</idLivre>
        <titre>Le Polar</titre>
        <auteur>T.B.A</auteur>
        <prix>0.00</prix>
        <qteInvent>20</qteInvent>
    </livres>
    </xml>
    if I do a document.write instead of an alert i get this :
    (which I'm not sure if that's the way it's suppose to be because i don't any tags ( <> </> ) it's just raw text, can someone please clarify this for me ? )


    Code:
     1 829974 Lexique érotique illustré DELVAU ALFRED 6.99 20 1 815264 Un Privilège à votre table TREMBLAY DIANE 9.99 20 1 967477 Stars de cinéma MANFERTO DE FABIANIS VALERIA & 9.99 20 1 1074537 L'Amour au menu ARSENAULT LINDA 9.99 20 2 1192373 L'Atelier du grand Verrocchio LEGAULT MATTHIEU 2.95 20 2 1189596 La chute de Sparte BIZ 9.99 20 2 1195198 Un vrai casse-tête DESCHENEAUX NADINE & LAPLANTE 9.99 20 2 1192379 Le Rêve d'Émilie JADE BÉRUBÉ 0.99 20 3 1198335 Mise à nu CASTLE RICHARD 9.99 20 3 1206018 Les Souvenirs FOENKINOS DAVID 9.99 20 3 1194006 La Femme au miroir SCHMITT ÉRIC-EMMANUEL 9.99 20 3 1202053 100 plus beaux sites du monde COLLECTIF 9.99 20 4 1198098 Les Invisibles WINCHLER MARTIN 9.99 20 4 1203302 Le Rêve d'Émily BÉRUBÉ JADE 0.99 20 4 1209460 Rose déluge AWUMEY EDEM 9.99 20 4 1205047 Ivresse MCKENZIE CATHERINE 9.99 20 5 1203854 Avalon High CABOT MEG 9.99 20 5 1193948 Otages N. éd. POIRIER CLAUDE 9.99 20 5 1198791 Le Sentier de la gloire ARCHER JEFFREY 9.99 20 5 1203182 Amore 14 MOCCIA FEDERICO 9.99 20 6 1193942 La Main d'Iman ASSANI-RAZAKI RYAD 9.99 20 6 1199869 Des vies d'oiseaux OVALDÉ VÉRONIQUE 9.99 20 6 1190603 Rouge dragon DE VILLIERS GÉRARD 9.99 20 6 1138711 Le Paris de Gainsbourg LEIBOWITCH ERSIN & LORIOU DOMI 9.99 20 7 1201037 A chacun son podium SYLVIE FRÉCHETTE 9.99 20 7 1194558 La maladie d'Alzheimer SERGE GAUTHIER JUDES POIRIER 9.99 20 7 1202313 Démons quotidiens NANCY HUSTON RAPH PETTY 9.99 20 7 1194556 Su : la cuisine turque de Fisun Ercan FISUN ERCAN 9.99 20 8 163 TASHEN T.B.A 0.00 20 8 1928 Réveiller le sommelier en vous T.B.A 0.00 20 8 1931 Prix littéraires T.B.A 0.00 20 8 2271 Le Polar T.B.A 0.00 20

    This is where i'm having issues :

    I'm to parse the XML DOM Object sent back from AJAX.
    I can't seem to get to the root node.

    here are the constraints :\

    • Can't use Jquery
    • Can't use any parsers out there
    • I have to use the DOM methods such as (firstChild, nextSibling ....)



    This is what i have so far ...

    Code:
                    var domTree = xhr.responseXML;
    		var domTreeText = xhr.responseText;
    		alert(domTreeText);
    		var livre = domTree.firstChild.nextSibling.firstChild.nextSibling;
    		alert(livre.firstChild.nodeValue);
    Code:
    
    alert(livre.firstChild.nodeValue);
    which shows me ...
    Code:
    
     <auteur>MANFERTO DE FABIANIS VALERIA &</auteur>
    ------------------------------------------^
    According to this output ... I could in fact reach the XML DOM Object, but I fall in the wrong node. Which is as you can see ... the 3rd <livre> -> the <auteur> nodeValue.


    One other thing that is makin go a little crazy is the :

    ------------------------------------------^

    I just don't know what that is ... Please help !

    Can anyone of you please help me out figure this out ?


    Also, I have seen discrepencies whether I use IE for parsing or Firefox... they don't have the same amounts of childNodes.

    I read somewhere that netscape based browsers, interpret spaces and CRLF, How do i go about navigating the DOM structure of my XML file whether i use IE or Firefox ?

    Can someone please clarify this to me ?

    One last thing ... this is my second thread on this forum can someone please explain to me how to close the thread once i have received a proper answer ... I think my previous is still open, hahaha...

    Thank you...

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I believe we need to navigate via documentElement, which refers to the root XML element, especially when using childNodes, etc.:

    Code:
    var domTree = xhr.responseXML;
    var domRoot = domTree.documentElement;
    BTW I don't think having a root element of <xml> is helpful

    It is also more reliable to use getElementsByTagName():

    Code:
    var books = domRoot.getElementsByTagName('livres');
    Using childNodes, etc., you may have to check the tagName fairly often, in case IE has inserted something mysterious for you
    Last edited by AndrewGSW; 08-24-2012 at 12:47 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    hayonj (08-24-2012)

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    & is a special character in XML: character entities begin that way. For instance, we encode quotes as
    Code:
    &quot;
    . You probably want
    Code:
    &amp;
    .
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Alex Vincent View Post
    & is a special character in XML: character entities begin that way. For instance, we encode quotes as
    Code:
    &quot;
    . You probably want
    Code:
    &amp;
    .

    Seem like i cant access anything ....

    is it possible i have encoding issues ? how do i fix that ?

    when i write :

    Code:
     var reponseServeurXML = function (xhr){ 
            var domTree = xhr.responseXML;
            var domTreeText = xhr.responseText;
            alert(domTreeText);
    	   
    	   var domRoot = xhr.responseXML.getElementsByTagName('livres')[0].firstChild.nodeValue;        //domTree.documentElement;   ( tried it but it didn't work )
    
    	   alert(domRoot);
    I get ths error ....

    Timestamp: 8/24/2012 5:06:57 AM
    Error: not well-formed
    Source File: http://127.0.0.1/DOM_AJAX/Ajax_Jonat.../categorie.php
    Line: 23, Column: 43
    Source Code:
    <auteur>MANFERTO DE FABIANIS VALERIA & AL</auteur>
    -------------------------------------------^

    im really stressed out i have to give this in by tomorrow and im no where near done if i can't access the database....

    the envy of manually removing any special characters from the database is taking over me slowly ... but i know it goes against the ethics of good programming skills and i should really my head around the better idea out there ... i know its possible i just have to be thought the proper way of how to navigate through the DOM object return through XML ....

    whether it's through IE or Firefox ...
    I have faith in the scene and the fellow gurus out there that will share with me the enlightenment of this AH HA moment

  • #5
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    If the XML is as you've posted it, you need to yell at whoever wrote it or serves it, because that & character by itself breaks XML well-formedness, as I said earlier. It's not the fault of the person trying to read it...
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Alex Vincent View Post
    If the XML is as you've posted it, you need to yell at whoever wrote it or serves it, because that & character by itself breaks XML well-formedness, as I said earlier. It's not the fault of the person trying to read it...


    I wrote the XML file as follows :
    Code:
    <?php
    require("../paramDB/connexion.php");
    header('Content-Type: text/html; charset=ISO-8859-1');  // Fix the encoding issue 
    function listeJSON($req){
     //	$action="J";
    
    	
         	echo "[";
    	while($row= mysql_fetch_assoc($req)){ 
    	
    	 		echo "{";
    			echo "\"idCateg\"".":"."\"".$row['idCateg']."\"".",";
    			echo "\"categorie\"".":"."\"".$row['categorie']."\"";
    			echo "},";
    		 }
    		 	echo "]";
    }		 
    
    
    
    function listeXML($req){
    // 	$action="X";
     header ("Content-Type: text/xml");
    		echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
    		echo "<xml>\n";
    		//echo "<action>$action</action>\n";
    		while ($row = mysql_fetch_object($req))
    		{
    			echo "<livres>\n";
    			echo "    <idCateg>$row->idCateg</idCateg>\n";
    			echo "    <idLivre>$row->idLivre</idLivre>\n";
    			echo "    <titre>$row->titre</titre>\n";
    			echo "    <auteur>$row->auteur</auteur>\n";
    			echo "    <prix>$row->prix</prix>\n";
    			echo "    <qteInvent>$row->qteInvent</qteInvent>\n";
    			echo "</livres>\n";
    		}
    		echo "</xml>"; 
    }
    
    
    $action=$_POST['action'];
    switch($action){
    
    
    case "J" :	
    		mysql_select_db($sql_bdd,$db_link);
    		$requete="SELECT * FROM categories";
    		$req=mysql_query($requete,$db_link) or die(mysql_error());
    		listeJSON($req);
    		break;
    
    
    
    case "X" :	
    		mysql_select_db($sql_bdd,$db_link);
    		$requete="SELECT * FROM livres";
    		$req=mysql_query($requete,$db_link) or die(mysql_error());
    		listeXML($req);
    		break;
    }//fin du switch
    ?>
    Maybe it's because in the mySQL database it's stored with theses like so... is there a way i can bypass or escape those caracters ?

  • #7
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • Users who have thanked Alex Vincent for this post:

    hayonj (08-24-2012)

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Alex Vincent View Post
    thanks for the link ... although i have move on ... i manually removed any " & " from the database ... i was luck i only had to modify 3 records out of 32 ....

    next time i'll try the htmlspecialchars PHP function ... it could have the way to go if i had thousand of records in the database ....


    but now ... even after i cleaned it ... i still can't navigate the dom of the XML file why is that ?
    now i get no errors ... but come in as blank or null...


    I read and re-read different ways of doing it ...

    and found this link ... it's in french though ...

    http://www.chezneg.fr/leblog/chezneg...php?id_art=125

    not exactly what i had in mind but it works ...
    Last edited by hayonj; 08-24-2012 at 08:39 PM. Reason: found a solution

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    how do i close this thread ?


  •  

    Posting Permissions

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