When I was working on a project, I needed to create a dynamic unlimited linkage menu. I am too lazy to search for the relevant code on the Internet, but many of them do not meet the requirements. One of the articles is about using JavaScript to operate XML files to implement an infinitely interactive menu, we can use ASP to read database values, write XML files, read them using JavaScript, and control their association.
The key here is to read the n-layer data classes in the database:
The structure of my database table is as follows:
'Tbl _ class
Description of the Data Type length of the column name
Classid int Class 4 ID
Moduleid int 4 module ID
Groupid int 2 identifies a group
Classname nvarchar 50 category name
Parentid int 2 connects to the group (0 indicates the parent class)##################################### My asp the Code is as follows ###################################### ####
'I ignored the database connection code. 'Function name: openxml (filename)
'Entry parameter: the XML file name to be connected or opened by filename
'Return value: xmldoc is an object that successfully loads the XML document.
'Print the error message strerror if an error exists.
'------------------------------------------------ Function openxml (filename)
Dim strsourcefile, xmldoc, strerror
Strsourcefile = filename
Set xmldoc = server. Createobject ("Microsoft. xmldom") 'create an xmldom instance
Xmldoc. async = false
Xmldoc. Load (strsourcefile)
Openxml = xmldoc. parseerror. errorcode
If xmldoc. parseerror. errorcode <> 0 then
Strerror = "<H2> error" & xmldoc. parseerror. errorcode & "</H2>"
Strerror = strerror & xmldoc. parseerror. Reason & "<br>"
Strerror = strerror & xmldoc. parseerror. url & "<br>"
Strerror = strerror & xmldoc. parseerror. Line & "<br>"
Strerror = strerror & xmldoc. parseerror. filepos & "<br>"
Strerror = strerror & xmldoc. parseerror. srctext & "<br>"
Response. Write strerror 'output Error
Else
Set openxml = xmldoc 'Return instance
End if
End Function '------------------------------------------------
'Function name: closexml ()
'Parameter: xmldoc XML component instance
'------------------------------------------------
Function closexml (xmldoc)
If isobject (xmldoc) then
Set xmldoc = nothing
End if
End Function '------------------------------------------------
'Function name: selectxmlnode
'Parameter: xmldoc XML component instance
'E element name
'Return element instance
'------------------------------------------------
Function selectxmlnode (xmldoc, E)
Dim N Set n = xmldoc. selectsinglenode ("//" & E)
Set selectxmlnode = N End Function Dim N, NP, maxgroup, root, xmldoc, NT, filename, S, SS, torf
Filename = server. mappath ("demo. xml ")
Set xmldoc = openxml (filename) 'open XML
Removeallnodes xmldoc, "root" 'clears the root and its sub-items. This can be easily read and written multiple times.
Set root = xmldoc. createelement ("root ")
Xmldoc. appendchild root creates a top-level element.
SQL = "select max (groupid) from tbl_class" 'maximum read level
Set rsw.cn.exe cute (SQL)
If isnull (RS (0) Then maxgroup = 0 else maxgroup = RS (0) 'if it is null, it indicates no data
S = "": Ss = ""
Set Nt = xmldoc. createelement ("item ")
NT. setattribute "text", "select"
Root. appendchild nt 'creates an element
For I = 1 to maxgroup 'Start the loop
SQL = "select * From tbl_class where groupid =" & I 'read from the underlying layer to the top layer
Set rsw.cn.exe cute (SQL)
Torf = false' creates an "Please select" empty for each layer.
Do While Rs. EOF = false' start to read lower-level data
Set n = xmldoc. createelement ("item" & RS ("classid") 'creates a Tag Element named item + id
N. setattribute "text", RS ("classname") 'sets its attribute "text" Classname
N. setattribute "value", RS ("classid") 'sets its attribute "value" Classid
If RS ("parentid")> 0 then 'has upper-layer data
Set Np = selectxmlnode (xmldoc, "item" & RS ("parentid") 'First reads its upper-layer data elements and stores them in NP
If torf = false then' if torf is false
Set Nt = xmldoc. createelement ("item") 'creates an element and stores it in NT
NT. setattribute "text", "select" ', and set its text attribute to "select"
NP. appendchild nt 'np add it as a child element
End if
Torf = true' set true
NP. appendchild n'np add N as a child element
Else
Root. appendchild n' if it is the first layer of data, add it as a child element under root.
End if
Rs. movenext next pointer
Loop Ss = SS & "<select id = select" & I & "width = 1> </SELECT> </span>" 'creates <SELECT>
S = S & ", 'select" & I & "'"' stores each <SELECT> ID in the variable S. The format is id1, Id2, ID3, ID4. Next
Xmldoc. Save filename 'formally Save the XML file
Closexml xmldoc 'close the XML file Response. Write ss' directly output <SELECT> to the document
S = mid (S, 2) // Call xmlselect. js in the HTM file
<Script language = JavaScript src = "xmlselect. js"> </SCRIPT>
<Script language = JavaScript>
<! -- // ** Power by fason
Function Init (){
VaR sel = [<% = S %>];
Attachselect ("demo. xml", Sel, ["Forum navigation"]);
}; // -->
</SCRIPT>
Init (); // ######### I have posted its JS Code by the author: PU foxin, http://fason.nease.net/samples/xmlselect/this example and code are provided. // ----------------------------- The xmlselect. js file starts ------------------------------- //-----------------------------------------------------/
// Nichname: fason
// Autho: Forbes Pu (PU foxin)
// Email: fason_pfx@hotmail.com
// Homepage: http://fason.nease.net
// Blog: http://blog.mvpcn.net/fason/
// Http://blog.csdn.net/fason/
//
// Function: attachselect (sxmlsrc, Asel [, sstore])
// Param1: sxmlsrc (URL of XML data source)
// Param2: Asel (array of select controls's ID)
// Param3: array of default value when display
//-----------------------------------------------------/ Function attachselect (sxmlsrc, Asel, sstore ){
VaR oxml = createxmldocument ();
VaR odocument = NULL;
VaR store = sstore? Sstore: [];
VaR sel = new array (); For (VAR I = 0; I <Asel. length; I ++)
Sel [I] = Document. getelementbyid (Asel [I]); If (! Oxml) {Throw new error ('not support! \ Nplease install a XML Parser '); return ;}
Oxml. onreadystatechange = function (){
If (oxml. readystate = 4 ){
Attachxml ();
}
};
Oxml. Load (sxmlsrc ); Function createxmldocument (){
If (document. Implementation & document. Implementation. createdocument ){
VaR Doc = Document. Implementation. createdocument ("", "", null );
If (Doc. readystate = NULL ){
Doc. readystate = 1;
Doc. addeventlistener ("LOAD", function (){
Doc. readystate = 4;
If (typeof Doc. onreadystatechange = "function ")
Doc. onreadystatechange ();
}, False );
}
Return Doc;
}
Else if (window. activexobject ){
VaR prefix = ["msxml3", "msxml2", "MSXML", "Microsoft"];
For (VAR I = 0; I <prefix. length; I ++ ){
Try {
VaR Doc = new activexobject (prefix [I] + ". Domdocument ");
Doc. setproperty ("selectionlanguage", "XPath ");
Return Doc;
} Catch (e ){}
}
}
Return NULL;
}; Function attachxml (){
Odocument = oxml.doc umentelement;
If (! Odocument) {Throw new error ('no XML data! '); Return ;}
For (VAR I = 0; I <Asel. Length-1; I ++ ){
Addevent (SEL [I], "onchange", function (x ){
Return function () {dochange (x );}
} (I + 1 ));
}
Dochange (0 );
}; Function selectxmlnode (x ){
VaR Spath = "/*";
VaR odoc = odocument;
For (VAR I = 0; I <X; I ++)
Spath + = "/* [" + (SEL [I]. selectedindex + 1) + "]";
If (typeof (odoc. selectsinglenode )! = 'Undefined') Return Odoc. selectsinglenode (Spath );
Else {
VaR Doc = odoc. nodetype = 9? Odoc: odoc. ownerdocument;
VaR res = Doc. creatensresolver (odoc. nodetype = 9? Odoc.doc umentelement: odoc );
Return Doc. Evaluate (Spath, odoc, res, 9, null). singlenodevalue;
}
}; Function addevent (El, shandler, FNC ){
If (El. attachevent ){
El. attachevent (shandler, FNC );
} Else if (El. addeventlistener ){
El. addeventlistener (shandler. Replace (/on/I, ''), FNC, false );
} Else {
El [shandler] = FNC;
}
}; Function dochange (n ){
VaR El = selectxmlnode (N );
VaR nodes = El? El. childnodes: [];
VaR S = sel [N];
VaR f = 0;
If (nodes. length> 0 ){
With (s ){
Length = 0;
For (VAR I = 0, j = 0; I <nodes. length; I ++ ){
If (nodes [I]. nodetype! = 1) continue;
VaR T = nodes [I]. getattribute ("text ");
VaR v = nodes [I]. getattribute ("value ")? Nodes [I]. getattribute ("value"): T;
If (V = store [N]) F = J;
Options [J ++] = New Option (T, V );
}
Options [f]. Selected = true;
}
If (++ n <SEL. Length) dochange (N );
} Else {
For (VAR I = N; I <SEL. length; I ++ ){
With (SEL [I]) {
Length = 0;
Options [0] = New Option ('--');
Options [0]. Selected = true;
}
}
}
};
}; // ----------------------------- Xmlselect. js file ended ------------------------------- XML is easy to use because it is simple and direct. I only know some knowledge about XML. If you have any errors, please point out. I don't think it is enough to understand XML and other professional knowledge.
|