Tidy XML / ASP?!

  • 15 years ago

    I'm new to XML and have been doing a littl reading up about it.  From the initial reading I did I created an .asp file and a .xml file as a test to traverse the xml file.


    ASP


    <%


    Dim strSaveURL
    Dim strFile
    Dim objXML
    Dim strXMLFile
    Dim objList
    Dim strDate


    strSaveURL = "C:\Documents and Settings\willsaunders\Desktop\"
    strFile = "test.xml"
       
    'XML File Object
    Set objXML = Server.CreateObject("Microsoft.XMLDOM")
    Set objLst = Server.CreateObject("Microsoft.XMLDOM")


    objXML.async = False


    'strPath = strSaveURL & strFile


    objXML.Load(Server.MapPath("test.xml"))


    'is there an error
    If objXML.parseError.errorCode <> 0 Then
       'handle the error
       Response.Write("Error during XML parse...<br />")
       With objXML.parseError
           Response.Write("Line number " & .Line)
       End With
       Response.End
    End If


    'Get values for title
    Set objLst = objXML.getElementsByTagName("title")
    ReDim arrTitle(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrTitle(i) = objLst.item(i).text
       'Response.Write(objLst.item(i).text)
    Next


    'Get values for initial
    Set objLst = objXML.getElementsByTagName("initial")
    ReDim arrInitial(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrInitial(i) = objLst.item(i).text
    Next


    'Get values for forename
    Set objLst = objXML.getElementsByTagName("forename")
    ReDim arrForename(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrForename(i) = objLst.item(i).text
    Next


    'Get values for surname
    Set objLst = objXML.getElementsByTagName("surname")
    ReDim arrSurname(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrSurname(i) = objLst.item(i).text
    Next


    'Get values for daytime number
    Set objLst = objXML.getElementsByTagName("daynumber")
    ReDim arrDayNum(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrDayNum(i) = objLst.item(i).text
    Next


    'Get values for evening number
    Set objLst = objXML.getElementsByTagName("evenumber")
    ReDim arrEveNum(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrEveNum(i) = objLst.item(i).text
    Next


    'Get values for mobile number
    Set objLst = objXML.getElementsByTagName("mobnumber")
    ReDim arrMobNum(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrMobNum(i) = objLst.item(i).text
    Next


    'Get values for address line 1
    Set objLst = objXML.getElementsByTagName("addressln1")
    ReDim arrAddLine1(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrAddLine1(i) = objLst.item(i).text
    Next


    'Get values for address line 2
    Set objLst = objXML.getElementsByTagName("addressln2")
    ReDim arrAddLine2(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrAddLine2(i) = objLst.item(i).text
    Next


    'Get values for town
    Set objLst = objXML.getElementsByTagName("town")
    ReDim arrTown(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrTown(i) = objLst.item(i).text
    Next


    'Get values for county
    Set objLst = objXML.getElementsByTagName("county")
    ReDim arrCounty(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrCounty(i) = objLst.item(i).text
    Next


    'Get values for postcode
    Set objLst = objXML.getElementsByTagName("postcode")
    ReDim arrPostcode(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrPostcode(i) = objLst.item(i).text
    Next


    'Get values for payperiod
    Set objLst = objXML.getElementsByTagName("payperiod")
    ReDim arrPayperiod(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrPayperiod(i) = objLst.item(i).text
    Next


    'Get values for optioncode
    Set objLst = objXML.getElementsByTagName("optioncode")
    ReDim arrOptioncode(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrOptionCode(i) = objLst.item(i).text
    Next


    'Get values for upgrade
    Set objLst = objXML.getElementsByTagName("upgrade")
    ReDim arrUpgrade(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrUpgrade(i) = objLst.item(i).text
    Next


    'Get values for delivery
    Set objLst = objXML.getElementsByTagName("delivery")
    ReDim arrDelivery(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrDelivery(i) = objLst.item(i).text
    Next


    'Get values for payrollnumber
    Set objLst = objXML.getElementsByTagName("payrollnumber")
    ReDim arrPayrollNum(objLst.Length)
    For i = 0 to (objLst.Length - 1)
       arrPayrollNum(i) = objLst.item(i).text
    Next



    'get recordcount
    strRC = objLst.Length


    %>


    <html>
    <head>
    <title>XML Test</title>
    <style>
    body
    {
    font-family: arial;
    }
    </style>
    </head>
    <body>


    <h1><i>Traversing the XML nodes</i></h1>


    <table>
       <tr>
           <td nowrap="nowrap"><b>Title</b></td>
           <td nowrap="nowrap"><b>Initial</b></td>
           <td nowrap="nowrap"><b>Forename</b></td>
           <td nowrap="nowrap"><b>Surname</b></td>
           <td nowrap="nowrap"><b>Day #</b></td>
           <td nowrap="nowrap"><b>Eve #</b></td>
           <td nowrap="nowrap"><b>Mob #</b></td>
           <td nowrap="nowrap"><b>Address Line 1</b></td>
           <td nowrap="nowrap"><b>Address Line 2</b></td>
           <td nowrap="nowrap"><b>Town</b></td>
           <td nowrap="nowrap"><b>County</b></td>
           <td nowrap="nowrap"><b>Postcode</b></td>
           <td nowrap="nowrap"><b>Pay Period</b></td>
           <td nowrap="nowrap"><b>Option Code</b></td>
           <td nowrap="nowrap"><b>Upgrade</b></td>
           <td nowrap="nowrap"><b>Delivery</b></td>
           <td nowrap="nowrap"><b>Payroll #</b></td>
       </tr>
       <%
       For i = 0 to (strRC - 1)
       %>
       <tr>
       <td nowrap="nowrap"><%Response.Write(arrTitle(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrInitial(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrForename(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrSurname(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrDayNum(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrEveNum(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrMobNum(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrAddLine1(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrAddLine2(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrTown(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrCounty(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrPostcode(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrPayPeriod(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrOptionCode(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrUpgrade(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrDelivery(i))%></td>
       <td nowrap="nowrap"><%Response.Write(arrPayrollNum(i))%></td>
       </tr>
       <%
       Next
       %>
    </table>


    </body>
    </html>


    <%
    Set objXML = Nothing
    Set objLst = Nothing
    %>


    XML


    <?xml version="1.0" encoding="ISO-8859-1" ?>


    <employees>
       <employee>
           <title>Ms</title>
           <forename>N M</forename>
           <initial></initial>
           <surname>Simbian</surname>
           <email>none</email>
           <daynumber>555 32121</daynumber>
           <evenumber>555 32133</evenumber>
           <mobnumber>none</mobnumber>
           <addressln1>232 High Street</addressln1>
           <addressln2>Oak Heath</addressln2>
           <town>Cheltenham</town>
           <county>Gloucestershire</county>
           <postcode>GL2 6RF</postcode>
           <payperiod>Monthly</payperiod>
           <optioncode>Option12</optioncode>
           <upgrade></upgrade>
           <delivery>Standard</delivery>
           <payrollnumber>5678</payrollnumber>
       </employee>
       <employee>
           <title>Mrs</title>
           <forename>W B</forename>
           <initial></initial>
           <surname>Williams</surname>
           <email>none</email>
           <daynumber>567 21468</daynumber>
           <evenumber>567 21468</evenumber>
           <mobnumber>07740 424242</mobnumber>
           <addressln1>138 Briar Close</addressln1>
           <addressln2></addressln2>
           <town>Roading</town>
           <county>WORCS</county>
           <postcode>WR93 6FB</postcode>
           <payperiod>Monthly</payperiod>
           <optioncode>Option6</optioncode>
           <upgrade>Printer</upgrade>
           <delivery>Evening</delivery>
           <payrollnumber>1234</payrollnumber>


       </employee>
    </employees>


    I've done more reading since and it looks like this is by no means the tidiest method to use.  Can someone take a look and make me some suggestions and perhaps alter the code or let me know where I need to alter the code.


    JJJ.

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe