...

View Full Version : Importing only elements with a certain attribute



tmgordon
03-04-2009, 06:18 AM
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...


<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.

oesxyl
03-04-2009, 12:15 PM
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...


<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

tmgordon
03-05-2009, 03:52 AM
I figured it out. Sorry if you didn't understand what I was asking. I was making it harder than it really was.



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?

oesxyl
03-05-2009, 04:06 AM
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

tmgordon
03-05-2009, 05:46 AM
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.



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}

oesxyl
03-05-2009, 05:59 AM
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.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum