Filling in a Form w/data from a XML file

  • I have a ASP page that contains a form which I want to populate with values from a XML file which I get from a MainFrame server.

    I have the code to get the xml file and place it into a Variable that I named "GetStuff"

    My problem is extracting the information from the XML and placing it into the form.

    This is the code I use to get the information intop a XML file and put it into the "GetStuff" Variable. This code works well for me.

    ---------

    <%

    option explicit

    Dim Browser,URL,ParseMe

    Dim Address

    Dim Getstuff

    Dim objWinHttp

    Set Browser = Server.CreateObject("WinHttp.WinHttpRequest.5")

    URL="http://10.10.10.10:8000/impf/default.bas?cmd=get13screen&Version=XML&msn=111111111"

    Browser.Open "GET",URL,False

    Browser.Send

    GetStuff=Browser.ResponseText

    %>

    --------------------

    My XML contains 37 items. I have put in "1's" the fields.

    Now I want to be able to get, lets say the information contained in the <ADDRESS>11111</ADDRESS> section and put it into a Form input area on my ASP page using the Variable "Address". I want to be able to do this for almost all the sections in the XML but if I can get one to work I can just duplicate it for the other sections

    Here is the code.

    -------

    <html>

    <head>

    <title>Untitled Document</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    </head>

    <body>

    <h2>

    Get infor from XML and place into the Form:

    </h2>

    <p><p>

    <form name="form1" method="post" action="">

    <input name="textfield2" type="text" value=" <%=Address%>" />

    </form>

    </body>

    </html>

    ---------

    Below is the XML file which is in the variable "GetStuff" that the first part of the code retreived.

    <?xml version="1.0"?>

    <RESULTS>

    <NTFD>1111</NTFD>

    <PRB><1111/PRB>

    <NAME>11111</NAME>

    <ADDRESS>1111111111</ADDRESS>

    <CITYSTZIP>11111119</CITYSTZIP>

    <CITY>11111</CITY>

    </RESULTS>

    -------------------------------------------

    Can anyone Pleae help me with this.

  • OK, first off you've got one problem. Your XML is not well-formed. I'm guessing that it should be this:

    <?xml version="1.0"?>

    <RESULTS>

    <NTFD>1111</NTFD>

    <PRB>1111</PRB> <!-- this line was bad -->

    <NAME>11111</NAME>

    <ADDRESS>1111111111</ADDRESS>

    <CITYSTZIP>11111119</CITYSTZIP>

    <CITY>11111</CITY>

    </RESULTS>

    One way to do this would be to load the XML data into a DOM and then fill the form with the desired data. If you've got a lot of data, then maybe building your page dynamically using XSLT might be appropriate. Depends on what you want to do.

    To load your XML into a DOM, something like the following code could be used:

    Dim xmlDoc

    Set xmlDoc = CreateObject("MSXML2.DOMDocument")

    docLoaded = xmlDoc.loadXml(GetStuff)

    ...and then, on your form, do this:

    <INPUT value=" ID="Text1"/>Not a great example (sorry) but hopefully it's enough to start with.

  • Thank You for your response..This problem has been keeping me up at nights.

    Below is the error I get when I try to view the page wit youtr code added.

    Below that is the code as it is in the page.

    Where am I going wrong?

    Error Type:

    Microsoft VBScript runtime (0x800A01A8)

    Object required: '[object]'

    /ONSA_NSS/openxml.asp, line 32

    -------------------------------------------------------

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

    <%

    Dim objWinHttp

    Set Browser = Server.CreateObject("WinHttp.WinHttpRequest.5")

    URL="http://13.245.60.166:8000/impf/default.bas?cmd=get13screen&Version=XML&msn=FU2111111"

    Browser.Open "GET",URL,False

     

    Browser.Send

    GetStuff=Browser.ResponseText

    %>

    <%

    Dim xmlDoc

    Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

    docLoaded = xmlDoc.loadXml(GetStuff)

    %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>Untitled Document</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    </head>

    <body>

    <form name="form1" id="form1" method="post" action="">

      <INPUT value=" <%= xmlDoc.selectSingleNode("//ADDRESS[text() = '1111111111']").text %> ID="Text1"/>

    </form>

    </body>

    </html>

  • I based my example on your sample XML, so it's probably not appropriate for the real XML that you're actually using. The problem is that this statement:

    xmlDoc.selectSingleNode("//ADDRESS[text() = '1111111111']").text

    is doing this:

    "From all the ADDRESS nodes in the XML document, get the one node [selectSingleNode] that has a text value of  '1111111111', and give me the text of that node."

    The problem is, if there is no node with a value of '1111111111', then this:

    xmlDoc.selectSingleNode("//ADDRESS[text() = '1111111111']")

    Returns null (or Nothing in VBScript), because there is no such node. That means that you're trying to get the .text property of Nothing, which gives you the error.

    Replace the ''1111111111' with a value from an ADDRESS node in your actual, real XML and it should work.

    I suggest that you learn more about XPath if you're interested, try Googling for XML/XPath/DOM tutorials, there's lots out there that will help you get started. I'll see if I can locate some decent links for you, maybe some other folks can post some links too?

     

  • Another thing could be happening, just something more to check. Make sure that your XML document is actually loading into the DOM. After execution of this line of code:

    docLoaded = xmlDoc.loadXml(GetStuff)

    The "docLoaded" variable should have a value of "true". If it doesn't, then there was a problem loading your XML data into the DOM. You can get an idea of what the problem was by checking the parseError property, something like this:

    If Not docLoaded Then Response.Write(xmlDoc.parseError)End If

    I'm not sure what the raw XML data comes in as, but if it's not well-formed then it won't load, and parseError should give you an idea of what the problem is.

  • Here is the problem..I dont know what will be in the XML doc...each one will contain differant information.

    The first part of the code "Browser" grabs information from a Mainframe according to a serial number supplied and creates the XML Doc.

    http://10.10.10.10:8000/impf/default.bas?cmd=get13screen&Version=XML&amp;msn=FU2111111" What information the XML contains depends on the value of msn

    This is the format of the XML doc it sends back. The Values with-in the XML will change each time.

    I want to take the information from this XML and put it into my Web form that I will create.

    <?xml version="1.0"?>

    <RESULTS>

        <CALLTYPE></CALLTYPE>

        <INITDATE></INITDATE>

        <INITTIME></INITTIME>

        <PROBLEMDESC>NO ACTIVITY OUTSTANDING</PROBLEMDESC>

        <DISPDATE></DISPDATE>

        <DISPTIME></DISPTIME>

        <DEPARTDATE>061005</DEPARTDATE>

        <DEPARTTIME>1556</DEPARTTIME>

        <KCM>6.3</KCM>

        <INST>0303</INST>

        <CONTRACTSTATUS>CPC</CONTRACTSTATUS>

        <SPECBILLCODE></SPECBILLCODE>

        <CN>7110296</CN>

        <DIST>1070</DIST>

        <TEAM>08/04</TEAM>

        <CSE>896829</CSE>

        <NTFD>CANC</NTFD>

        <PRB></PRB>

        <NAME>Some Company</NAME>

        <ADDRESS> 57TH STREET 22ND FLOOR</ADDRESS>

        <CITYSTZIP>NEW YORK, NY  10019-</CITYSTZIP>

        <CITY>NEW YORK</CITY>

        <ST>NY</ST>

        <ZIP5>10719</ZIP5>

        <PLUS4></PLUS4>

        <SHIFT>1X5</SHIFT>

        <KOP>Rheal Dugas</KOP>

        <TELEPHONE>506-111-7321</TELEPHONE>

        <ADDF1>Some Company</ADDF1>

        <ADDF2></ADDF2>

        <ADDF3>22ND FLOOR</ADDF3>

        <ADDF4> 57TH STREET</ADDF4>

        <ADDF5>NEW YORK</ADDF5>

        <DATE4>052505</DATE4>

        <METERA4></METERA4>

        <DATE7>042105</DATE7>

        <METERA7></METERA7>

    </RESULTS>

  • OK, that's better.

    Couple of questions:

    1) Does the schema ("record" or "field" structure/names) change between calls, or is the schema constant and only the data in the "fields" changes?

    2) Does your call return a single "record" or multiple "records"? Looks like it returns only one.

    I'm assuming it's a single record, and that the schema does not change between calls. You could simply do something like this, in your <form>, after you've loaded the document (i.e. after a successful .loadXML call):

    <INPUT value="<%= xmlDoc.selectSingleNode("//CALLTYPE").text %> ID="callType"/>

    <INPUT value="<%= xmlDoc.selectSingleNode("//INITTIME").text %> ID="initTime"/>

    etc...

    for each "field" in your XML (using "//<fieldname>" isn't the most efficient XPath expression in this case, but if it's a small document then hopefully not a big deal).

    If the schema changes (including elements that are not there for some calls, e.g. element is missing if it has no data) then it'll be a bit more difficult but still doable. In that case, you could try to grab the node object with a selectSingleNode() call, and if the node is NULL/Nothing then set the value to a default value, else get the node's text.

  • You are a GENIUS..it works..........and it is such a simple piec of code.

    Thank you very much

  • No problem, glad to help. BTW, googling for "XPath tutorial" turns up some pretty good links (but an awful lot of them). This is one I've used that's fairly decent, for XPath and XML stuff in general (inc. XSLT, which is something you might want to check out as it might be useful to you):

    http://www.w3schools.com/

    XPath stuff is at: http://www.w3schools.com/xpath/

    If you're going to do much of this, definitely worth doing the research on it, or grabbing a book or two. I don't have any book recommendations, but do search through some online-tutorials (Google gave me ~152K links :plain, it'll help a lot.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply