Processing of XML data sent and received by ASP

Source: Internet
Author: User
Tags html encode
ASP sends and receives XML data using the MSXML2.XMLHTTP object.
Request. asp

Dim Https
Set Https = server. createobject ("MSXML2.XMLHTTP ")
'Define an XMLHTTP object
Https. open "POST", "http: // 127.0.0.1/testpost/response. asp", false
Https. send "<? Xml version = "" 1.0 ""?> <Misc_command version = "" 1.6 ""> <command_name> echo </command_name>
<Command_data_block> <sid> 123456 </sid> <service_id> 987654 </service_id>
<Sp_id> 11111 </sp_id> <sp_password> 22222 </sp_password> </command_data_block>
</Misc_command>"
If Https. readystate = 4 then
Response. write "submitted successfully"
'Readstate reading status is 4, and the operation is successful. If the reading status is not successful, you do not need to proceed.
Dim objstream
Set objstream = Server. CreateObject ("adodb. stream ")
'Define a stream. You have to deal with it because it is garbled.
Objstream. Type = 1
Objstream. Mode = 3
Objstream. Open
Objstream. Write Https. responseBody
Objstream. Position = 0
Objstream. Type = 2
Objstream. Charset = "GB2312"
Html = objstream. ReadText
'Convert the code and put it in html to close these images.
Objstream. Close
Set objstream = nothing
Set https = nothing
End if
Response. write html

Response. asp

'Create a DOMDocument object
Set xml = Server. CreateObject ("msxml2.DOMDocument ")
Xml. async = False

'Load POST Data
Xml. Load Request
If xml. parseError. errorCode <> 0 Then
Response. write "cannot receive data correctly" & "Description:" & xml. parseError. reason & "<br> Line:" & xml. parseError. Line
End If

Set blogchild = xml. getElementsByTagName ("misc_command ")
'The _ text = blogchild. item (0). childnodes (1). text
'The _ text = blogchild. item (0). text
'For I = 0 to blogchild. length-1
Response. write the_text

Using this method, it is easy to call Servlet or Web Service in ASP!

Use ASP to receive XML data from a remote server

Http://www.kaidianle.com/article/list10381.html
<%
Dim objXML
Dim objRootElement
Dim strValue
Dim strInetURL
Dim strXML
Dim item

StrInetURL = "http://pf.inetsolution.com/inetactive2001/inetactive2001news.xml"
Dim HttpReq

Set HttpReq = server. CreateObject ("MSXML2.XMLHTTP ")

HttpReq. open "GET", "http://pf.inetsolution.com/inetactive2001/inetactive2001news.xml", False
HttpReq. send
StrXML = HttpReq. responseText
Set objXML = Server. CreateObject ("Msxml2.DOMDocument ")
ObjXML. validateonparse = true
ObjXML. async = false
ObjXML. loadXML (strXML)
If objXML. ParseError. errorCode <> 0 then
Response. Write ("Error:" & objXML. parseError. reason & "<br> ")

 

Response. Write ("Code: 0x" & hex (objXML. parseError. errorCode) & "<br> ")
Response. Write ("At Line:" & objXML. parseError. line & "<br> ")
Response. Write ("At pos:" & objXML. parseError. linePos & "<br> ")

Else
Set objRootElement = objXML.doc umentElement
If not isObject (objRootElement) then
Response. Write ("no file loaded ")
Else
Response. Write (objRootElement. childnodes (0). text)
End if
End if
%>

 

ASP Method for XML operations

The main method and implementation of ASP operating XML files on the server through XMLDom
For small data volumes, xml files have many advantages over ACCESS in retrieval and update.

I have tested whether to use a database and store all the Website member information, product data information, transaction information, and website customization information in three xml files. The running result is normal, it seems that it is much faster than the database, but it is not tested and cannot be determined.

The following describes how to create, query, modify, and perform xml operations.

'Create a DOM object
Set objDom = server. CreateObject ("MicroSoft. XMLDom ")

'Retrieve xml data
'Method 1: Get the xml data of the xml file
ObjDom. load ("c: test. xml ")
'Method 2: Get the data of the xml data string
ObjDom. loadxml ("<people> <man name =" sd "/> </people> ")

'Create a Node object
Set Newnode = objDom. CreateElement ("people ")
'Return value to this node
Newnode. Text = "person"
'Add attributes to this node
Set newattride = objDom. CreateNode ("attribute", "name ","")
Newattri. Text = "James"
Newnode. SetAttributeNode NewAttribute
'Add a subnode to this node
Set NewnodeChild = objDom. CreateElement ("address ")
Newnode. appendChild NewnodeChild
'Save this Node object
ObjDom. appendChild Newnode
ObjDom. save ("c: test. xml ")

'Look up a Node object
Set objtofind=objdom.doc umentElement. SelectSingleNode ("// people/man ")
'Get the node name, node value, attribute value, and all xml of the Node object.
Nodename = objtofind. nodename
Nodevalue = objtofind. text
Objtofind. GetAttributeNode ("name"). Nodevalue

'Retrieve an attribute Node object
Set objattrtofind=objdom.doc umentElement. SelectSingleNode ("// people/man"). GetAttributeNode ("name ")
'Retrieve the attribute name and attribute value of this node.
Nodeattrname = objattrtofind. nodename
Nodeattrvalue = objattrtofind. nodevalue

'Delete a Node object
Set objnode1_objdom.doc umentElement. SelectSingleNode ("// people/man") 'node to be deleted
Set objparentnode1_objdom.doc umentElement. SelectSingleNode ("// people") 'parent node of the node to be deleted
Objparentnode. removeChild objnode

'Retrieve the byte point set of a node
Set objnodesnodes objdom.doc umentElement. SelectSingleNode ("// people/man"). ChildNodes

 

Traverse this set
Method 1
For each element in objnodes
Response. write element. nodename byte name
Response. write element. text byte point value
Next
Method 2
Domlength = objnodes. length
For I = 0 to domlength-1
Response. write objnodes. childnodes (I). nodename byte name
Response. write objnodes. childnodes (I). text byte point value
Next

 

'Retrieve the attribute set of a node
Set objnodesincluobjdom.doc umentElement. SelectSingleNode ("// people/man"). GetAttributeNode ("name"). attributes
Traverse this set
For each element in objnodes
Response. write element. nodename attribute name
Response. write element. nodevalue Attribute Value
Next

You can use xmldom objects to operate xml files.
Many asp functions are implemented by xmlhttp objects.

Re: [to] ASP main methods and implementation of XML file operations on the server through XMLDom
Feifei, why don't you describe the @ usage? Without this, it is inconvenient to do a lot of things.
Xml, also known as data compression technology, can be used as a database as its name suggests.
Therefore, we can regard xml as a "small database ". Why is it small? Because the functions and applications of xml are convenient, there are still some differences with databases. Why should we use xml? Sometimes some of our applications perform data access, but if you use a database, the explicit behavior is not flexible and convenient. At this time, we should use xml in combination.
Since xml can be viewed as a database, its first step is to create a link object. (Take ASP + xml as an example)
Use server. createobject to create a database.
The method is as follows:
Set xmlDoc = Server. CreateObject ("microsoft. xmldom ")
XmlDoc. async = false
Xmldata = absolute path of the Data Source
XmlDoc. load xmldata 'use the load method for link here

Because the xml data format is more user-friendly, the data format may be invalid due to human or other reasons. In this case, if you continue to use it, it will cause the program to exit, we often verify the data format after creating a link object.
The method is as follows:
If xmlDoc. parseError. errorCode <> 0 then
... Handle errors

 

<%
'----------------------------------------------------------------
'Program Introduction: added, deleted, modified, and viewed the specified node text in the XML document in asp.
'Entry parameter: None
'Exit parameter: None
'------------------------------------------------
'Function name: ConnectXml ()
'Entry parameter: the xml file name to be connected or opened by filename
'Exit parameter: None
'Return value: ConnectXml = 0. XMLMorntekDocument is an object that successfully loads the XML document.
'Connectxml <> 0, the error message strError is printed.
'------------------------------------------------
Dim XMLMorntekDocument

Function ConnectXml (filename)
Dim strSourceFile
StrSourceFile = Server. MapPath (filename)
Set XMLMorntekDocument = Server. CreateObject ("Microsoft. XMLDOM ")
XMLMorntekDocument. async = false
XMLMorntekDocument. load (strSourceFile)
ConnectXml = XMLMorntekDocument. parseerror. errorcode
If XMLMorntekDocument. parseerror. errorcode <> 0 then
StrError = "

 

 

StrError = strError & XMLMorntekDocument. parseerror. reason & "<br>"
StrError = strError & XMLMorntekDocument. parseerror. url & "<br>"
StrError = strError & XMLMorntekDocument. parseerror. line & "<br>"
StrError = strError & XMLMorntekDocument. parseerror. filepos & "<br>"
StrError = strError & XMLMorntekDocument. parseerror. srcText & "<br>"
Response. write strError
End if
End function

 

'------------------------------------------------
'Function name: CloseXml ()
'Entry parameter: None
'Exit parameter: None
'------------------------------------------------
Function CloseXml (XMLMorntekDocument)
If IsObject (XMLMorntekDocument) then
Set XMLMorntekDocument = nothing
End if
End function

'------------------------------------------------
'Function name: SelectXmlNodeText (elementname)
'Entry parameter: the name of the elementname Element
'Exit parameter: None
'------------------------------------------------
Function SelectXmlNodeText (elementname)
Elementname = "//" & elementname
Temp = XMLMorntekDocument. selectSingleNode (elementname). text
SelectXmlNodeText = server.html encode (temp)

End function

'------------------------------------------------
'Function name: InsertXmlNodeText (befelementname, elementname, elementtext)
'Entry parameter: the name of the element inserted by elementname
'Befelementname: insert an element before the element name
'Elementtext: Text of the inserted element
'Exit parameter: None
'------------------------------------------------
Function InsertXmlNodeText (befelementname, elementname, elementtext)
Dim befelement, element
Set befelement = XMLMorntekDocument. selectSingleNode ("//" & befelementname)
Set element = XMLMorntekDocument. createelement (elementname)
Befelement. insertBefore element, befelement. firstchild
Element. text = elementtext
End function

'------------------------------------------------
'Function name: UpdateXmlNodeText (elementname, newelementtext)
'Entry parameter: the name of the elementname Element
'Newelementtext
'Exit parameter: None
'------------------------------------------------
Function UpdateXmlNodeText (elementname, newelementtext)
Dim element
Set element = XMLMorntekDocument. selectSingleNode ("//" & elementname)
Element. text = newelementtext
End function

'------------------------------------------------
'Function name: DeleteXmlNodeText (elementname)
'Entry parameter: the name of the elementname Element
'Exit parameter: None
'------------------------------------------------
Function DeleteXmlNodeText (elementname)
XMLMorntekDocument. selectSingleNode ("//" & elementname). text = ""
End function
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.