/    Sign up×
Community /Pin to ProfileBookmark

my function is too long and complex.

this is the dynamic menu of my site. it works clearly but i need a shorter and simpler function. help me pls. thanks

[CODE]
[B]XML[/B]
<root>
<m t=”1″>
<m t=”1.1″/>
<m t=”1.2″ />
<m t=”1.3″>
<m t=”1.3.1″ />
<m t=”1.3.2″ />
</m>
</m>
<m t=”2″>
<m t=”2.1″ />
</m>
<m t=”3″/>
</root>
[/CODE]

[CODE]
[B]JS[/B]
x=x.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1){
txt=x[i].attributes[0];
lnk=x[i].attributes[1];
if(txt) t+=txt.nodeValue;
if(lnk) t+=”.”+lnk.nodeValue;
t+=”<br>”;

y=x[i].childNodes;
for (k=0;k<y.length;k++)
{
if (y[k].nodeType==1){
txt=y[k].attributes[0];
lnk=y[k].attributes[1];
if(txt) t+=”—“+txt.nodeValue;
if(lnk) t+=”.”+lnk.nodeValue;
t+=”<br>”;

z=y[k].childNodes;
for (j=0;j<z.length;j++)
{
if (z[j].nodeType==1){
txt=z[j].attributes[0];
lnk=z[j].attributes[1];
if(txt) t+=”———–“+txt.nodeValue;
if(lnk) t+=”.”+lnk.nodeValue;
t+=”<br>”;

}
}
}
}
}
}[/CODE]

[upl-file uuid=18d2decb-0348-454f-b1f6-471d40c362b7 size=6kB]m.jpg[/upl-file]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mjdamatoDec 12.2006 — Here are two options - neither has been tested, so there could be some errors, but the logic is sound:

Here is your original script parsed down:
for (nodeObj in x.childNodes) {
if (nodeObj.nodeType==1){
t+=(nodeObj.attributes[0])?nodeObj.attributes[0].nodeValue:"";
t+=(nodeObj.attributes[1])?"."+lnk.nodeValue:"";
t+="&lt;br&gt;";
for (childObj1 in nodeObj.childNodes) {
if (childObj1.nodeType==1){
t+=(childObj1.attributes[0])?"---"+childObj1.attributes[0].nodeValue:"";
t+=(childObj1.attributes[1])?"."+childObj1.attributes[1].nodeValue:"";
t+="&lt;br&gt;";
for (childObj2 in childObj1.childNodes) {
if (childObj2.nodeType==1){
t+=(childObj2.attributes[0])?"-----------"+childObj2.attributes[0].nodeValue:"";
t+=(childObj2.attributes[1])?"."+childObj2.attributes[1].nodeValue:"";
t+="&lt;br&gt;";
}
}
}
}
}
}


This is the code converted to a recursive function. Note that this will continue to parse childnodes until there are no more, so if there are more levels of child nodes than you want to display this would need to be modified.
spacer = "";
t="";
function parseNodes(parentNode) {
for (nodeObj in parentNode.childNodes) {
if (nodeObj.nodeType==1){
t+=(nodeObj.attributes[0])?nodeObj.attributes[0].nodeValue:"";
t+=(nodeObj.attributes[1])?"."+lnk.nodeValue:"";
t+="&lt;br&gt;";
if (nodeObj.childNodes) {
spacer+="----";
parseNodes(nodeObj);
spacer=spacer.substr(0, (spacer.length-4));
}
}
}
}
Copy linkTweet thisAlerts:
@gxlrygtauthorDec 12.2006 — thanks mjdamato its a great script but i couldnt work it. can u check again. i sent to full script(applied your code)

[CODE]
<script type="text/javascript">
function loadXMLDoc(dname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}

xmlDoc=loadXMLDoc("xml.xml");

t="";
spacer = "";

function parseNodes(parentNode) {
for (nodeObj in parentNode.childNodes) {
if (nodeObj.nodeType==1){
t+=(nodeObj.attributes[0])?nodeObj.attributes[0].nodeValue:"";
t+=(nodeObj.attributes[1])?"."+lnk.nodeValue:"";
t+="<br>";
if (nodeObj.childNodes) {
spacer+="----";
parseNodes(nodeObj);
spacer=spacer.substr(0, (spacer.length-4));
}
}
}
}


parseNodes(xmlDoc.documentElement);
document.write(t);
</script>
[/CODE]
Copy linkTweet thisAlerts:
@mjdamatoDec 13.2006 — Well, I've got it partially working. For some reason it doesn't work after the first sub element. Might be something to do with the recursion. I might take a look at it again later. Maybe you can figure out the rest

function parseNodes(parentNode, spacer) {
var t="";
cNodes = parentNode.childNodes;
for (i=0; i&lt;cNodes.length; i++) {

<i> </i> if (cNodes[i].nodeType==1){
<i> </i> t+=(cNodes[i].attributes[0])?spacer+cNodes[i].attributes[0].nodeValue:"";
<i> </i> t+=(cNodes[i].attributes[1])?"."+cNodes[i].attributes[1].nodeValue:"";
<i> </i> document.write(t+"&lt;br&gt;");

<i> </i> if (cNodes[i].childNodes) {
<i> </i> spacer+="----";
<i> </i> parseNodes(cNodes[i], spacer);
<i> </i> spacer=spacer.substr(0, (spacer.length-4));
<i> </i> }
<i> </i> }
<i> </i>}
}

xmlDoc=loadXMLDoc("xml.xml");
parseNodes(xmlDoc.documentElement,'');
Copy linkTweet thisAlerts:
@gxlrygtauthorDec 13.2006 — im using this. i couldnt solve the problem.
[CODE]
function echo(){
if(txt) t+=txt.nodeValue;
if(lnk) t+="."+lnk.nodeValue;
t+="<br>";
}

xmlDoc=loadXMLDoc("xml.xml");

var x=xmlDoc.documentElement;


t="";
x=x.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1){
txt=x[i].attributes[0];
lnk=x[i].attributes[1];
echo ();

y=x[i].childNodes;
for (k=0;k<y.length;k++)
{
if (y[k].nodeType==1){
txt=y[k].attributes[0];
lnk=y[k].attributes[1];
echo ();

z=y[k].childNodes;
for (j=0;j<z.length;j++)
{
if (z[j].nodeType==1){
txt=z[j].attributes[0];
lnk=z[j].attributes[1];
echo ();

}
}

}
}


}
[/CODE]
×

Success!

Help @gxlrygt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...