Read XML and Utf-8 and Gb2312 garbled problems in ASP

Source: Internet
Author: User
Tags exit end reference
xml| problem

XML objects are typically created in ASP using: CreateObject ("Microsoft.XMLDOM")
Read node: selectSingleNode ("NodeName")//nodename is the node name.
Read property: Attributes.getnameditem ("attribute")//attribute is the name of the property in the node.
loading XML; Load (Server.MapPath ("Data.xml"))
The Async property indicates whether asynchronous downloads are allowed.

Reference:
<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%>
<%response.charset= "UTF-8"%>
<title>read xml</title>
<body>
<%
Dim Node,i,nodecount
Set Doc = CreateObject ("Microsoft.XMLDOM")
Doc.async = False
Doc.load (Server.MapPath ("Data.xml"))
Set root = Doc.documentelement
Set Nodelis = Root.childnodes
Nodecount = Nodelis.length
For I=1 to Nodecount
Set node = Nodelis.nextnode ()
Set cost = Node.attributes.getNamedItem ("cost")
%>
Section <%=i%> Record:
<table border= "1" >
<tr>
&LT;TD width= "rowspan=" 2 "> "/></td>"
&LT;TD width= > Title </td>
&LT;TD width= "> Press" </td>
&LT;TD width= > Price </td>
</tr>
<tr>
<td>
<%=node.selectsinglenode ("name") .text%>
</td>
<td>
<%=node.selectsinglenode ("publisher") .text%>
</td>
<td>
<%= cost.text%>
</td>
</tr>
</table>
<%
Next
%>
</body>

Xml
Reference:
<?xml version= "1.0" encoding= "Utf-8"?>
<data>
<book cost= ">"
<name>Dreamweaver</name>
<publisher> Shanghai Science and Technology Press </publisher>
img/dw.jpg</img>
</book>
<book cost= ">"
<name>Flash</name>
<publisher> Railway Publishing House </publisher>
img/flash.jpg</img>
</book>
<book cost= ">"
<name>Firweorks</name>
<publisher> Education Press </publisher>
img/fw.jpg</img>
</book>
</data>

The end of Utf-8 and Gb2312 garbled problem
Research for many days, but also tried a lot of ways, summed up the current finding the best way:
Let's say something basic:
<%@ Codepage=65001%>utf-8
<%@ codepage=936%> Simplified Chinese
<%@ codepage=950%> Traditional Chinese
<%@ codepage=437%> American/Canadian English

<%@ codepage=932%> Japanese
<%@ codepage=949%> Han Wen
<%@ codepage=866%> Russian

CODEPAGE specifies what IIS encodes to read the string passed over (form submission, address bar delivery, etc.).

The reason for the garbled is that the site should be integrated when the module code is not the same cause.
As with my blog, this problem happens when you integrate, because blogs are Utf-8,
Recently many netizens are consulting for this problem, I have tried many ways.
The most convenient methods are as follows:
Do not convert any module page encoding the Utf-8 or utf-8, the Gb22312 or Gb2312
In the Utf-8 module package file (such as conn.asp, but be aware that conn.asp must be called on the first line) plus the front
<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%>
<%Session.CodePage=65001%>
At the top of the package file for the GB2312 module plus
<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<%Session.CodePage=936%>
Analogy to other encodings.

ASP Read and write XML
<%
'----------------------------------------------------------------
' Introduction: Complete the ASP language to add, delete, modify, view the text of the specified node in the XML document
' Entry parameters: None
' Exit Parameters: None
'------------------------------------------------
' Function name: Connectxml ()
' Entry parameter: FileName required to be connected or opened by XML filename
' Exit Parameters: None
' Return value: Connectxml=0,xmlmorntekdocument is an object that successfully loads an XML document.
' Connectxml<>0, the error message is printed strerror
'------------------------------------------------
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 parameters: None
' Exit Parameters: 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 Parameters: None
'------------------------------------------------
function Selectxmlnodetext (elementname)
Elementname= "//" &elementname
Temp=xmlmorntekdocument.selectsinglenode (elementname). Text
selectxmlnodetext= Server.HTMLEncode (temp)

End Function

'------------------------------------------------
' Function name: Insertxmlnodetext (Befelementname,elementname,elementtext)
' Entry parameter: ElementName the name of the inserted element
' Befelementname inserts an element before the name of this element
' Elementtext the text of the inserted element
' Exit Parameters: 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
' New text for newelementtext element
' Exit Parameters: 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 Parameters: 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.