ASP + XML + JavaScript implementation dynamic unlimited level linkage menu

xiaoxiao2021-03-06  23

Combined with the ASP to complete the reading of the database value, then write to the XML file, and then read it with JavaScript and control it.

The key here is to read the N layer data classes in the database:

My database table structure is like this:

'TBL_CLASS

Column Data Type Length Description

ClassID INT 4 ID

ModuleID INT 4 Module ID

GroupID INT 2 identifies a group

ClassName NVARCHAR 50 Class Name

ParentID INT 2 Connect to the group (0 indicates a parent class)

'############ My ASP code is as follows ############# I put the connection database The code is ignored.

'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 instance' ----------------------------- ------------------- Function Selectxmlnode (Xmldoc, e) Dim Nset 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 N.SetAttribute" Text " , 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