ASP + XML + JavaScript implementation dynamic unlimited level linkage menu

xiaoxiao2021-03-06  43

When I do a project, there is a need for a dynamic unlimited linkage. Since I am lazy to find the relevant code, I have not met the demand, one of which has an article with JavaScript to implement an infinite-level linkage menu with JavaScript, and we can combine ASP to complete the reading of database values. And write to the XML file, read out with JavaScript and control it. The key here is to read the N layer data classes in the database and write it correctly in the XML file: My database table structure is this: 'TBL_CLASS column name Data Type length Description ClassID INT 4 Class IDModuleId INT 4 Module IDGroupID INT 2 Identify a group of classname nvarchar 50 category name ParentID INT 2 Connect to group (0 said is a parent class)

'########################################################################################################################################################################################################################################################################################################## #################### 'I ignore the code connected to the database.

'Function Name: OpenXML (filename)' Entry parameters: filename needs to be connected or opened XML file name 'return value: XMLDOC is an object that successfully loads an XML document. 'There are errors, print error message strerror' ----------------------------------------- -------

function OpenXml (filename) dim strSourceFile, XmlDoc, strErrorstrSourceFile = filenameSet XmlDoc = Server.CreateObject ( "Microsoft.XMLDOM") 'Create instance XMLDOM XmlDoc.async = false XmlDoc.load (strSourceFile) OpenXml = XmlDoc.parseerror.errorcode if XmlDoc. parseerror.errorcode <> 0 then strError = "

error" & XmlDoc.parseerror.errorcode & "" strError = strError & XmlDoc.parseerror.reason & "
" strError = strError & XmlDoc.parseerror.url & "
" strError = strError & XmlDoc.parseerror.line & "
" strError = strError & XmlDoc.parseerror.filepos & "
" strError = strError & XmlDoc.parseerror.srcText & "
" response.write strError 'output error else set OpenXml = XmlDoc' Back Example END IF END FUNCTION

'------------------------------------------------' Function Name: CloseXml () 'Parameters: XMLDOC XML Components College ------------------------------------ ------------ 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 Example '------------------------------------------------ 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, s, torffilename = server.mappath ("demo.xml") set Xmldoc = OpenXML (filename) 'Open XMLRemoveAllNodes Xmldoc, "root"' Put Root and It is clear below, so you can read and write SET root = Xmldoc.createElement ("root") xmldoc.Appendchild root 'Create a top element SQL = "SELECT MAXID) from TBL_CLASS"' read the biggest Level SET RS = cn.execute (SQL) IF ISNULL (RS (0)) THEN MAXGROUP = 0 else maxgroup = rs (0) 'If NULL indicates no data S = ": ss =" "" SET = XMLDOC. CreateElement ("item") nt.setttribute "text", "Please select Root.Appendchild NT 'Create an element for i = 1 to maxGroup' Start loop SQL =" SELECT * from TBL_CLAS WHERE GroupID = "& i 'by the underlying Read the SET RS = cn.execute (SQL) Torf = false 'to create an "Please select" for each layer. Do WHILE RS.EOF = FALSE 'Start reading the underlying data set n = xmldoc.createElement ("Item" & r ("ClassID") "Create a tag element N.SetAttribute" text "named Item ID number , RS ("classname") 'sets its properties "text" to the database table

ClassName N.SetAttribute "value", RS ("classid") 'Sets its properties "value" to the database table

ClassID IF RS ("ParentID")> 0 THEN 'is SET NP = SELECTXMLNODE (XMLDoc, "Item" & RS ("ParentID")) "ParentID") "" ParentID ")" "ParentID")' = false then 'If the Torf is a false value set nt = xmldoc.createElement ("iTEM") Create an element saved in NT NT.SetAttribute "text", "Please select" Set its text property to "Please select "Np.appendchild NT 'NP adds it as a child element END if Torf = true' Set true np.appendchild n 'NP to add n to child elements else root.Appendchild n' If it is the first layer of data, it adds it to One child elements under root End if xixt 'Next Pointer loop ss = SS & " s = s & ", 'select" & I & "'" 'Putting each output to document S = MID (s, 2)

// Call Xmlselect.js