Store the HTML form data as an XML format

xiaoxiao2021-03-05  21

Store the HTML form data as an XML format

If you are familiar with ASP, XML, and HTML4.0, read the following example

Deposit form data as an XML format

Usually, the data submitted in the ASP is generally written to the database. However, if you want to send data easier, then you can write it as an XML file format. This approach is more useful for data collected on the web. Because XML is very simple for the platform used, there is no way to convert the data format.

Write the submitted data as an XML document, you need to create a new XML document with Microsoft XMLDOM Object. Microsoft XMLDOM Object has an extensible object library that makes an XML document by creating Elements, Attributes, and VALUES. I can't make a complete introduction throughout the target model because it contains it too wide. For the website that will be built, the target model can even build a relatively complete part by itself.

After XMLDOM Object is created, the structure of XML is demonstrated by creating a target (this goal is about constituting Elements in the XML document). Next, it will be given to explain how XMLDOM is created. After creating root element, it is attached to the xmldom file. Then create Child Elements and attach to root element, and finally store the document.

Demonstrate Microsoft XMLDOM object

<

%

DIM

Objdom

DIM

Objroot

DIM

Objchild1

DIM

Objchild2

DIM

Objpi

"

XMLDOM object creates a CreateObject method for the Server object

Set

Objdom

=

Server.

CreateObject

(

"

Microsoft.xmldom

"

)

"

Create an IXmldomenElement object using XMLDOM's CreateEleMnet method.

"

CreateLemNet method is another string parameter, which indicates the name of the ELEMENT. The return value is passed to the ObjrooT variable. Objroot represents the root element of the XML document..

Set

Objroot

=

ObjDom.createElement

"

RootElement

"

)

"

Use the appendchild method of the xmldom object to add the objroot

"

Element Reference

TO

The xml document. Objdom.Appendchild Objroot

"

Now, Following the Same Stes, You Will Create References To The

"

Child Elements

for

The XML Document. The Only Difference.

IS

WHEN THE

"

Child Elements Are Appended to The Document, You Will Call THE

"

Appendchild method of the ixmldomelement

Object

Rather Than

"

Appendchild method of the xmldom object. by use the ixmldomelement

"

TO

Append the children, you are differentiating

and

Applying tiet

"

Structure to) The child elements from the root element.

Set

Objchild1

=

ObjDom.createElement

"

ChildElement1

"

) ObjRoot.Appendchild Objchild1

Set

Objchild1

=

ObjDom.createElement

"

ChildElement2

"

) Objroot.AppendChild Objchild2

"

The final step to take care of before saving this document is to add

"

An XML Processing Instruction. this

IS

Necessary So That XML Parsers

"

Will Recognize this document as an xml document.

Set

Objpi

=

ObjDom.createProcessinginstructionTRuction

"

xml

"

,

"

Vertsion =

"

1.0

""

)

"

Call the insertbefore method of the xmldom object in Order to Insert

"

The processing instruction before the root element (The Zero Element

"

In the xmldom childnodes collection.

Objdom.insertbefore objpi, objdom.childnodes

0

)

"

Calling the save method of the xmldom object will save this XML

"

Document

TO

Your Disk Drive. in this

Case

, The Document Will Be Saved

"

To the

"

C:

"

Drive and will be named by DRIVE AND WILL

"

MYXMLDoc.xml

"

When Saving an

"

XML Document,

IF

The File Does

NOT

EXIST, IT WILL BE CREATED.

IF

IT

"

Does exist, IT Will Be overwritten.

Objdom.save

"

C: myXmldoc.xml "

%

>

After the document is archived, if you open this document again, then you will appear in the form of the following code list:

MyXMLDoc.xml:

XML Version = "1.0"

?>

<

RootElement

>

<

ChildElement1

/>

<

ChildElement2

/>

RootElement

>

In the "MYXMLDoc.xml" document, ChildElement1 and ChildElement2 will appear in empty Elements. If they are assigned, each value will be enclosed by the marker.

Now let's think about how to write HTML data to the XML document. We already know how to create and store XML documents. Write a form data to the XML document process, and now evolve into the REQUEST OBJECT "S Form Collection and the steps to set up each table single field to XML Element Value to repeat. The above can be done by ASP. Example: Connect data to XML

Now, we will explain an example of a normal HTML form. This Form has several domains such as user name, address, phone, and E-mail. And write this information into the XML file and save it. ENTERCONTACT.HTML:

<

HTML

>

<

HEAD

>

<

Title

>

Contact Information

Title

>

HEAD

>

<

Body

>

<

TABLE

Border

= "1"

>

<

FORM

Action

= "ProcessForm.asp"

Method

= "POST"

>

<

TR

> <

TD

Colspan

= "2"

>

Please Input Your Contact Infomation Here:

TD

>

TR

>

<

TR

> <

TD

>

First Name:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Firstname"

Name

= "Firstname"

>

TD

>

TR

>

<

TR

> <

TD

>

Last Name:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Lastname"

Name

= "Lastname"

>

TD

>

TR

>

<

TR

> <

TD

>

Address # 1:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Address1"

Name

= "Address1"

>

TD

>

TR

>

<

TR

> <

TD

>

Address # 2:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Address2"

Name

= "Address2"

>

TD

>

TR

>

<

TR

> <

TD

>

Phone Number:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Phone"

Name

= "Phone"

>

TD

>

Tr>

<

TR

> <

TD

>

E-mail:

TD

> <

TD

> <

INPUT

Type

= "text"

id

= "Email"

Name

= "Email"

>

TD

>

TR

>

<

TR

> <

TD

Colspan

= "2"

Align

= "center"

> <

INPUT

Type

= "Submit"

id

= "BTNSUB"

Name

= "BTNSUB"

Value

= "Submit"

>

TD

>

TR

>

FORM

>

TABLE

>

Body

>

HTML

>

Send data in FORM to ProcessForm.asp. This is an ASP page that writes the same function repeatedly in this ASP to write the FORM data to XML.

file.

ProcessForm.asp:

<

%

'

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

'

The "ConvertFormToxml" Function Accepts to Parameters.

'

Strxmlfilepath - The Physical path where the xml file will be saved.

'

Strfilename - The name of the xml file what will be saved.

'

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

FUNCTION

ConvertformToxml (strxmlfilepath, strfilename)

'

Declare Local Variables.

DIM

Objdom

DIM

Objroot

DIM

Objfield

DIM

ObjfieldValue

DIM

Objattid

DIM

ObjattTaborder

DIM

Objpi

DIM

x

'

Instantiate the microsoft xmldom.

Set

Objdom

=

Server.

CreateObject

(

"

Microsoft.xmldom

"

) ObjDom.PreserveWhitespace

=

True

'

Create Your root element and append it to the xml document.

Set

Objroot

=

ObjDom.createElement

"

Contact

"

) Objdom.Appendchild Objroot

'

Itereate Through The Form Collection of The Request Object.

For

x

=

1

TO

REQUEST.FORM.COUNT

'

Check to see if "btn" is in the name of the form element.

'

IT IT IS, THEN IT IS A Button and We Do Not Want To Add IT '

To the xml document.

IF

Instr

(

1

, Request.form.Key (x),

"

BTN

"

)

=

0

THEN

'

Create An Element, "Field".

Set

Objfield

=

ObjDom.createElement

"

Field

"

)

'

CREATE AN Attribute, "ID".

Set

Objattid

=

ObjDom.createAttribute

"

Id

"

)

'

Set the value of the id attribute equal the the name of

'

The current form field.

Objattid.text

=

Request.form.Key (x)

'

The SetAttributeEthode Will Append The ID Attribute

'

To The Field Element.

Objfield.setttribute ObjattiD

'

Create Another Attribute, "Taborder". This Just Orders The

'

Elements.

Set

ObjattTaborder

=

ObjDom.createAttribute

"

Taborder

"

)

'

Set the value of the taborder attribute.

ObjattAborder.Text

=

x

'

Append The Taborder Attribute To The Field Element.

Objfield.setttribute ObjattTaborder

'

Create a New Element, "Field_Value".

Set

ObjfieldValue

=

ObjDom.createElement

"

FieldValue

"

)

'

Set the value of the field_value element equal to

'

The Value of The Current Field in The Form Collection.

ObjfieldValue.Text

=

Request.form (x)

'

Append The Field Element as a child of the root element.

Objroot.AppendChild Objfield

'

Append the field_value element as a child of the field elemnt.

Objfield.Appendchild ObjfieldValue

End

IF

NEXT

'

Create the xml processing instruction.

Set

Objpi

=

ObjDom.createProcessinginstructionTRuction

"

xml

"

,

"

Version = "" 1.0 ""

"

)

'

Append the processing instruction to the xml document.objdom.insertbefore objpi, objDom.childnodes

0

)

'

Save the xml document.

Objdom.save strxmlfilepath

&

""

&

Strfilename

'

Release all of your object.

Set

Objdom

=

Nothing

Set

Objroot

=

Nothing

Set

Objfield

=

Nothing

Set

ObjfieldValue

=

Nothing

Set

Objattid

=

Nothing

Set

ObjattTaborder

=

Nothing

Set

Objpi

=

Nothing

END FUNCTION

'

Do Not Break on an error.

On

Error

Resume

NEXT

'

Call The ConvertFormToxml Function, Passing In The Physical Path To

'

Save the file to and the name. You wish to use for the file.

ConvertFormToxml

"

F: / asp /

"

,

"

Contact.xml "

'

Test to see et estilor occurred, if so, let the user know.

'

OtherWise, Tell The User That The Operation WAS Successful.

IF

Err.Number

<>

0

THEN

Response.write

"

Errors Occurred While Saving Your form submission.

"

)

Else

Response.write

"

Your form Submission Has Been Saved.

"

)

End

IF

%

>

If you use the above code in your own application, please remember one thing, in the case where the "ConvertFormToxml" function has been run, if the XML file name already exists, the file will be overwritten. Here, I suggest that it is best to use a randomly established file name before using the "ConvertFormToxml" function. In this way, the risk of being rewritten in value data is reduced to zero.

For the generation of XML files, examples are as follows:

Contact.xml:

XML Version = "1.0"

?>

Edited with xmlspy v2004 rel. 4 u (http://www.xmlspy.com) by Dicky (Apple's Eden)

->

<

Contact

>

<

Field

Id

= "Firstname"

Taborder

= "1"

>

<

FieldValue

>

Dicky

FieldValue

>

Field

>

<

Field

Id

= "Lastname"

Taborder

= "2"

>

<

FieldValue

>

GU

FieldValue

>

Field

>

<

Field

Id

= "Address1"

Taborder

= "3"

>

<

FieldValue

>

Shanghai

FieldValue

>

Field

>

<

Field

Id

= "Address2"

Taborder

= "4"

>

<

FieldValue

>

Beijing

FieldValue

>

Field

>

<

Field

Id

= "Phone"

Taborder

= "5"

>

<

FieldValue

>

123456

FieldValue

>

Field

>

<

Field

Id

= "Email"

Taborder

= "6"

>

<

FieldValue

>

Applebbs@gmail.com

FieldValue

>

Field

>

Contact

>

I recommend this: Copy the above code to the same name page on your personal website server and run the above example. Be sure to clarify the path and file name that is valid for your personal server.

When you are ready, check your XML file again.


New Post(0)