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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Importing only elements with a certain attribute

    Hello, I have just started using XML and I am having trouble importing the data where I need it to go. The XML file contains something like this...
    Code:
    <Root>
       <Status name="Good">
           <Category name="Software">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
            </Category>
            <Category name="Hardware">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
            </Category>
        <Status name="Bad">
             <Category name="Software">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
              </Category>
             <Category name="Hardware">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
              </Category>
         </Status>
    </Root>
    What I need to do is import the data child elements from
    <Status name="Good"> & <Category ="Software"> to text area 1.
    <Status name="Good"> & <Category ="Hardware"> to text area 2.
    <Status name="Bad"> & <Category ="Software"> to text area 3.
    <Status name="Bad"> & <Category ="Hardware"> to text area 4.

    Like I said I am new to XML. I have been searching for an answer to this for a while now and I can't seem to find it. I assumed this would be easy, but then again maybe it can't even be done. Let me know. Thanks in advance for your time.
    Last edited by tmgordon; 03-04-2009 at 07:35 PM. Reason: correction

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tmgordon View Post
    Hello, I have just started using XML and I am having trouble importing the data where I need it to go. The XML file contains something like this...
    Code:
    <Root>
       <Status name="Good">
           <Category name="Software">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
            </Category>
            <Category name="Hardware">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
            </Category>
        <Status name="Bad">
             <Category name="Software">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
              </Category>
             <Category name="Hardware">
               <Data id="-">
                   <child1>***</child1>
                   <child2>***</child2>
                   <child3>***</child3>
                </Data>
                (additional <Data id="-">'s)
              </Category>
         </Status>
    </Root>
    What I need to do is import the data child elements from
    <Status name="Good"> & <Category ="Software"> to text area 1.
    <Status name="Good"> & <Category ="Hardware"> to text area 2.
    <Status name="Bad"> & <Category ="Software"> to text area 3.
    <Status name="Bad"> & <Category ="Hardware"> to text area 4.

    Like I said I am new to XML. I have been searching for an answer to this for a while now and I can't seem to find it. I assumed this would be easy, but then again maybe it can't even be done. Let me know. Thanks in advance for your time.
    put your code between [ code] and [ /code] tags, please. You can edit your previous post to do that. Thank you.
    Look for xpath and xslt processing to solve your problem. It's very easy when you know how to do but a little hard until then, you must know at least some basic things.
    Try to solve and post question/problems, we will help you if we can and if is something more specific.

    best regards

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem Solved

    I figured it out. Sorry if you didn't understand what I was asking. I was making it harder than it really was.

    Code:
    function Import_XML() {
    
    
    
    // code for IE
    if (window.ActiveXObject)
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation.createDocument)
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    xmlDoc.async=false;
    xmlDoc.load("data.xml");
    
    var z = xmlDoc.getElementsByTagName("Status");
    	for (var k=0;k<z.length;k++) {
    			
    		var GoB = z[k].getAttribute("name");
    	
    var y = z[k].getElementsByTagName("Category");
    	for (var j=0;j<y.length;j++) {	
    	  
    	   var HoS = y[j].getAttribute("name");
    			  
    var x=y[j].getElementsByTagName("Data");
    	for (var i=0;i<x.length;i++) { 
    		
    
    if (GoB=="Good" && HoS=='Software') {var Output = document.form.Output_1};
    
    else if (GoB=="Good" && HoS=='Hardware') {var Output = document.form.Output_2};
    
    else if (GoB=="Bad" && HoS=='Software') {var Output = document.form.Output_3};
    
    else if (GoB=="Bad" && HoS=='Hardware') {var Output = document.form.Output_4};
    	
    		  
    Output.value = Output.value + x[i].getElementsByTagName("child1")[0].childNodes[0].nodeValue
    
    Output.value = Output.value + x[i].getElementsByTagName("child2")[0].childNodes[0].nodeValue;
    
    Output.value = Output.value + x[i].getElementsByTagName("child3")[0].childNodes[0].nodeValue;
    
    Output.value = Output.value + x[i].getElementsByTagName("child4")[0].childNodes[0].nodeValue;
    
    		}
    	}
    }
    This actual code had a little more to it, but that is the basic format. I noticed that it doesn't work in firefox though. The final version is an HTA, so it doesn't really matter, but does anybody know what I did wrong?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    if (GoB=="Good" && HoS=='Software') {var Output = document.form.Output_1};
    
    else if (GoB=="Good" && HoS=='Hardware') {var Output = document.form.Output_2};
    
    else if (GoB=="Bad" && HoS=='Software') {var Output = document.form.Output_3};
    
    else if (GoB=="Bad" && HoS=='Hardware') {var Output = document.form.Output_4};
    	
    		  
    Output.value = Output.value + x[i].getElementsByTagName("child1")[0].childNodes[0].nodeValue
    
    Output.value = Output.value + x[i].getElementsByTagName("child2")[0].childNodes[0].nodeValue;
    
    Output.value = Output.value + x[i].getElementsByTagName("child3")[0].childNodes[0].nodeValue;
    
    Output.value = Output.value + x[i].getElementsByTagName("child4")[0].childNodes[0].nodeValue;
    
    		}
    	}
    }
    1. probably some combination of GoB and HoS don't match any if condition and you don't have an else to last if. => Output will be undefined
    2. Output is local in each if

    Could be also other things but that was at first look.

    best regards

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Firefox problem solved as well...

    Problem was with the ";" being outside the if statements. IE overlooks them, firefox doesn't. I did notice that firefox takes much longer to load the data than IE. In IE it just appears in the text areas. In firefox you can actually see the lines being written.... area 1, then area 2, ect... Then it takes another second or two for firefox to put red lines under all of the mispelled words.

    Code:
    if (GoB=="Good" && HoS=='Software') {Output = document.form.Output_1}
    
    else if (GoB=="Good" && HoS=='Hardware') {Output = document.form.Output_2}
    
    else if (GoB=="Bad" && HoS=='Software') {Output = document.form.Output_3}
    
    else if (GoB=="Bad" && HoS=='Hardware') {Output = document.form.Output_4}

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tmgordon View Post
    Problem was with the ";" being outside the if statements. IE overlooks them, firefox doesn't. I did notice that firefox takes much longer to load the data than IE. In IE it just appears in the text areas. In firefox you can actually see the lines being written.... area 1, then area 2, ect... Then it takes another second or two for firefox to put red lines under all of the mispelled words.

    Code:
    if (GoB=="Good" && HoS=='Software') {Output = document.form.Output_1}
    
    else if (GoB=="Good" && HoS=='Hardware') {Output = document.form.Output_2}
    
    else if (GoB=="Bad" && HoS=='Software') {Output = document.form.Output_3}
    
    else if (GoB=="Bad" && HoS=='Hardware') {Output = document.form.Output_4}
    I didn't notice that ';',
    anyway is a good idea to have them. I guess will take longer to parse js when ';' miss.
    You can try to profile the script and see what take so long in firefox.

    best regards


  •  

    Posting Permissions

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