June 19, 200818 yr Hi! I am new here, less then 1 hour... I have an "advanced" question... how to get a list of attributes from an XML element in javascript, using DOM. I have search all over www to find any answers, but the only ones so far is the one where you know the attibutenames, kind of hardcoded style. What I want is:: <myxmlelement aNotKnownAttributeName="aNotKnownValue"> other data/elements or nothing </myxmlelement> I am creating a HTM Application (HTA) and as far as I know it is IE with full access to the system. this HTA is called/launched from SesamTV to extend the functionality of SesamTV (SesamTV is a kind of HTPC Shell), The functions I am trying to implement at this stage is module based. The main HTA is loading an XML file with instructions how to build a menu. Here comes the part of code/structure for main_buttons.xml <?xml version="1.0" encoding="utf-8"?> <BUTTONS> <_INIT type="text/jscript"> called with eval() function when this document is loaded, which creates and initialise all buttons... code removed.... nothing to do with my question... this technique works for what I am trying to do. </_INIT> <HELP> <ABOUT TOPIC="ABOUT">This HTMLApplication was created to extend SesamTV with a new menu to launch games easy, (I am using SesamTV as the Shell).</ABOUT> </HELP> <BUTTON KIND="MENU" PATH="C:\Documents and Settings\HASSE\My Documents\Mina HTApps\HTAMediaCenter\xmlui\N64Games.xml" CAPTION="N64 Games" HINT="Show list of N64 games" ACCESSMODE="ABSOLUTE"> /* every button with #text with length > 0 will be executed as javascript during INIT, and allow us to create handlers for the button inside the button. */ setAttribute("caption", oButtons[i].getAttribute("CAPTION")); onclick = function() {alert(this.caption) }; </BUTTON> </BUTTONS> I hope you got the idea of how it is supposed to work? MAIN.HTA (loads...) MAIN_BUTTONS.XML (this will be initialized from itself) MAIN.HTA now (hopefully) have a lot of buttons, linking to XML files with lists of games, apps or even a radiomodule... ... but I need help, I need to know how to get a list of all attributes from <BUTTON> element as an example but not knowing any attributeName.... Hoping that you/anyone can help! small screenshots? 640x
June 20, 200818 yr Author Hi! I am new here, less then 1 hour... I have an "advanced" question... how to get a list of attributes from an XML element in javascript, using DOM. I have search all over www to find any answers, but the only ones so far is the one where you know the attibutenames, kind of hardcoded style. What I want is:: I have finally "solved" this problem by follow each method involved with the XMLDOM and analyzing every object and its methods until I came up with one answer that works. I put in my solution if someone else have this problem. the XML file with the unknown <xmlElement attributeName="attibuteValue" ...></xmlElement> <?xml version="1.0" encoding="utf-8"?> <BUTTONS ACCESSMODE="ABSOLUTE | RELATIVE" HOST="C:\Program Files\Project64 1.6\Project64.exe" KIND="MENU | APPLICATION | DOCUMENT"> <BUTTON KIND="DOCUMENT" PATH="M:\torrents\N64 Stuff\(N64 Rom) Zelda Majora's Mask.v64" CAPTION="Zelda Majora's Mask" HINT="Working..." ACCESSMODE="ABSOLUTE" > textdata </BUTTON> </BUTTONS> and the code (inside my HTA) to get the unknown data from <xmlElement attributeName="attibuteValue" ...></xmlElement> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>My HTML Application</title> <script type="text/jscript"> function getCurrentDirectory(){ /* hämta mappen vi kör ifrån */ var fso=new ActiveXObject("Scripting.FileSystemObject"); path = unescape(document.location); path = path.substring(8,path.lastIndexOf("/")+1); path = path.replace(/\//g, "\\"); // ändra slash till windows delete(fso); return path; }; function loadmenu(sPath) { try{ var xmlContent=new ActiveXObject("Microsoft.XMLDOM"); try { xmlContent.async=false; xmlContent.load(getCurrentDirectory()+sPath); return(xmlContent); } catch(e){ alert("2 can't open document::\n"+sPath); }; } catch(e) { alert("can't create Microsoft.XMLDOM::\nthis error came from function loadmenu..."); alert(e.message); }; return(null); }; </script> <hta:application applicationname="MyHTA" border="dialog" borderstyle="normal" caption="My HTML Application" contextmenu="no" icon="myicon.ico" maximizebutton="no" minimizebutton="yes" navigable="no" scroll="no" selection="no" showintaskbar="yes" singleinstance="yes" sysmenu="yes" version="1.0" windowstate="normal" > </head> <body> <!-- HTML goes here --> <script type="text/jscript"> xmlDoc = loadmenu("xml_attrib.xml"); var xmlElems = xmlDoc.getElementsByTagName("BUTTON"); var xmlElem; var xmlAttrib; for(i=0; i < xmlElems.length; i++) { //loop thru elements xmlElem = xmlElems.item(i); for(j=0;j < xmlElem.attributes.length;j++) { //loop thru attribs xmlAttrib = xmlElem.attributes.item(j); sNodeName = xmlAttrib.nodeName; //get the UNKNOWN attributeName sNodeValue = xmlAttrib.nodeValue; //get the UNKNOWN attributeValue alert(sNodeName + " = " + sNodeValue); }; }; </script> </body> </html>
Create an account or sign in to comment