0

I am really struggling with a Jquery script I can't get to work.

I have an XML string and I want to return the name 'Miki' from it, however its just not working and i can't figure it out. Can someone help me?

function LoadParseXML() {

         var xml
         xml = '<?xml version="1.0" encoding="utf-8"?><CATALOG><VAR><PREVIOUSPAT>Miki</PREVIOUSPAT></VAR></CATALOG>';
         loadXMLDoc(xml, 'PREVIOUSPAT');
          }


     function loadXMLDoc(url, Node) {
         var xmlhttp;
         var txt, x, xx, i;
         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp = new XMLHttpRequest();
         }
         else {// code for IE6, IE5
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange = function () {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                 x = xmlhttp.responseXML.documentElement.getElementsByTagName("VAR");

                 for (i = 0; i < x.length; i++) {

                     xx = x[i].getElementsByTagName(Node);

                     txt = xx[0].firstChild.nodeValue;

                     alert(txt);

                 }

             }
         }

         xmlhttp.open("GET", url, true);
         xmlhttp.send();
     }
2
  • log xx variable and see what's inside Commented Aug 30, 2013 at 14:22
  • 2
    Just skimming over your code, why aren't you using jQuery when you mention it? jQuery has $.ajax() and $.parseXML() which should hopefully fix any problems you're having. For traversal through the XML document, use jQuery methods like .find(), .filter(), or .children(). And then .text() to retrieve element content Commented Aug 30, 2013 at 14:30

1 Answer 1

1

try this jQuery code to loop PREVIOUSPAT occurrences

var xml = '<?xml version="1.0" encoding="utf-8"?><CATALOG><VAR><PREVIOUSPAT>Miki</PREVIOUSPAT></VAR></CATALOG>';
xmlDoc = $.parseXML( xml );
$xml = $( xmlDoc );

$xml.find('CATALOG > VAR > PREVIOUSPAT').each(function(){
    alert( $(this).text() );
})
Sign up to request clarification or add additional context in comments.

1 Comment

God bless you all, thank you Michael B, I didn't realise it was that straight forward. I need to obviously learn more about JQuery but you good samaritans helped me out considerably. Thank you Ian too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.