Article 5: XML magic Plugin-xml dom (1) [reprint]

Source: Internet
Author: User
Tags processing instruction xsl
Article 5: XML magic example-xml dom (1)

Author: Yi's information, Xu jiangren

Integrate XML-related technologies
In the previous four articles, we introduced the techniques of using ADO to extract data from the database into XML, using XSL technology to compile documents, and linking data. this will be quickly displayed on the website. How should these technologies be used in the development of Web applications? When should I use what technologies? There have been many blurred images that seem to have been familiar with XML-related technologies, but they are always difficult to understand.
So how should we start? First, we need to figure out what XML can do? For example, if you want to make the same item have different display methods, you can use XSL to export it. For example: if you want to quickly list data without using program, you can use the techniques of material association at this time. For example: to enable users to collect data, they do not need to execute an ASP program from the new user every time to retrieve another region or another item, at this time, xml dom objects are used.

Retrieve XML data through recordset

Three examples are used to integrate these three applications. First, we want to extract the required XML data from the data warehouse. Of course, it is through ADO, as for how to obtain XML data, there have been many ways in the third article. Here, we strive to make the program simple and fast, therefore, you can use the recordset object of ADO to generate XML data directly.
The following program extracts pubs information from SQL Server and generates XML files directly to users. For example:
<%
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "provider = sqloledb; user id = sa; initial catalog = pubs; Data Source = (local );"
Set rs = conn. Execute ("select title_id, title, type, price from titles ")

'Specify the XML format of the response to the user
Response. contenttype = "text/XML"
'If the data source has Chinese characters, the encoding must be set to big5.
'Response. Write "<? XML version = "" 1.0 "" encoding = "" big5 ""? >"
'You can also directly apply the XSL Information
'Response. Write "<? XML-stylesheet type = "" text/XSL "" href = "" ADO-XML-DOM.xsl ""? >"
'Use the recordset save method to directly convert to an XML file and store it to the doc.
Rs. Save response, 1

%>

ADO-XML-DOM1.asp

In the first program listed above, the recordset information is still retrieved through ADO. However, if there is Chinese in the information, you must add Pi (Processing Instruction) partial encoding is set to "big5". In addition, if you want to apply the XSL protocol to analyze the current information, you must add the applicable encoding method, the results of the generated XML data are as follows:

Show resources through XSL

If the following XSL workflow is applied, the information can be displayed. Examples and results are as follows:
<? XML version = "1.0" encoding = "big5 "? >
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/TR/WD-xsl">
<XSL: template match = "/">
<HTML>
<Head> <Body>
<Table border = "0" cellpadding = "3" cellspacing = "3">
<Tr>
<TH style = "background-color: beige"> commandid </Th>
<TH style = "background-color: beige"> secret name </Th>
<TH style = "background-color: beige"> classification </Th>
<TH style = "background-color: beige"> simple upload </Th>
</Tr>
<XSL: For-each select = "// Z: Row" Order-by = "@ title">
<Tr valign = "TOP"
Onmouseover = "This. style. backgroundcolor = 'lightcyance '"
Onmouseout = "This. style. backgroundcolor = 'white '"
Style = "background-color: White; cursor: Hand" onclick = "alert ('Cool, huh? ') ">
<TD> <XSL: value-of select = "@ title_id"/> </TD>
<TD> <XSL: value-of select = "@ title"/> </TD>
<TD> <XSL: value-of select = "@ Type"/> </TD>
<TD> <XSL: value-of select = "@ price"/> </TD>
</Tr>
</XSL: For-each>
</Table>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>

ADO-XML-DOM.xsl

The results are as follows:

Show XML data through data Preview

However, when writing an XSL dataset, some design and Division operations may occur because of the lack of useful tools, in addition, the main usage of XSL is not to show the current information, but to apply the information format. Therefore, we can use the information link to show the information, for example:
<HTML>
<Head>
<Meta name = "generator" content = "Microsoft Visual Studio 6.0">
<XML id = "DSO">
<% Set conn = server. Createobject ("ADODB. Connection ")
'Create an xml dom object Doc
Set Doc = server. Createobject ("Microsoft. xmldom ")

Conn. Open "provider = sqloledb; user id = sa; initial catalog = pubs; Data Source = (local );"
Set rs = conn. Execute ("select title_id, title, type, price from titles ")

'Save the recordset item as XML to Doc
Rs. Save doc, 1
'Retrieve the information part through the selectnodes method of the xml dom object
Set datanodes = Doc. selectnodes ("// Z: Row ")

Response. Write "<recordset>"
For each row in datanodes
Response. Write row. xml
Next
Response. Write "</recordset>"
%>
</XML>

</Head>
<Body>

<Table border = "0" cellpadding = "3" cellspacing = "3" datasrc = "# DSO">
<Thead>
<TH style = "background-color: beige"> commandid </Th>
<TH style = "background-color: beige"> secret name </Th>
<TH style = "background-color: beige"> classification </Th>
<TH style = "background-color: beige"> simple upload </Th>
</Thead>
<Tr valign = "TOP"
Onmouseover = "This. style. backgroundcolor = 'lightcyance '"
Onmouseout = "This. style. backgroundcolor = 'white'">
<TD> <span dataworkflow = "title_id"> </span> </TD>
<TD> <span dataworks = "title"> </span> </TD>
<TD> <span dataworks = "type"> </span> </TD>
<TD> <span dataworks = "price"> </span> </TD>
</Tr>
</Table>

</Body>
</Html>

In the above program, because the Save method of the recordset object produces the XML schema and XML data section, only the XML data section is retrieved and placed in the data island, therefore, first create an xml dom object Doc and save the recordset information as XML to the doc.
The selectnodes method of the xml dom object is used to retrieve all the XML data parts, and then the XML data of each batch record is printed in a circle, the data island generated by the product can be seen in the original region of zookeeper:

The data splitting function is implemented based on the data processing technology.

Although it has already been listed in the table, it is better to cut data to collect data, so we can see through the data processing technologies introduced in the previous period, therefore, add datapagesize adequacy and ID adequacy to the table to control the number of partitions. Add four additional nodes for user selection. For example:
<Table Id = tablepage Border = "0" cellpadding = "3" cellspacing = "3"
  Datasrc = "# DSO" Datapagesize = 5 Width = 100% type = "codeph" text = "/codeph">

<Thead>
<TH style = "background-color: beige"> commandid </Th>
<TH style = "background-color: beige"> secret name </Th>
<TH style = "background-color: beige"> classification </Th>
<TH style = "background-color: beige"> simple upload </Th>
</Thead>
<Tr valign = "TOP"
Onmouseover = "This. style. backgroundcolor = 'lightcyance '"
Onmouseout = "This. style. backgroundcolor = 'white'">
<TD> <span dataworkflow = "title_id"> </span> </TD>
<TD> <span dataworks = "title"> </span> </TD>
<TD> <span dataworks = "type"> </span> </TD>
<TD> <span dataworks = "price"> </span> </TD>
</Tr>
<Tfoot>
<TD colspan = 4 align = "right">

  <Input type = "button" value = "first" id = button1 name = button1>
<Input type = "button" value = "previous" id = button2 name = button2>
<Input type = "button" value = "Next" id = button3 name = button3>
<Input type = "button" value = "last" id = button4 name = button4>

</TD>
</Tfoot>
</Table>

ADO-XML-DOM3.asp

Then add the following program control:
<SCRIPT id = clienteventhandlersvbs Language = VBScript>
<! --

Sub button1_onclick
Tablepage. firstpage
End sub

Sub button2_onclick
Tablepage. previouspage
End sub

Sub button3_onclick
Tablepage. nextpage
End sub

Sub button4_onclick
Tablepage. lastpage
End sub

-->
</SCRIPT>

ADO-XML-DOM3.asp

The results are as follows:

Make DHTML Dynamic Content changes through the magic render xml dom object

When the program reaches this stage, the Creator will find the advantage of the website information, and the information will be transferred to the client under XML data island, for the metadata, you do not need to go back to the server, that is, you do not need to write ASP programs. This method not only reduces server traffic, but also reduces traffic usage, and greatly improves the efficiency of traffic allocation.
The author may ask, if the information in the table column finds the information that you want to see more frequently, that is, simply reading the information, can you? Of course, there is no problem. In the old saying, the information is already on the client side, next, as long as the xml dom object and DHTML are passed through, it will be able to expand the current resource, the previous ADO-XML-DOM3.asp modification example below:
<Tr valign = "TOP"
Onmouseover = "This. style. backgroundcolor = 'lightcyance '"
Onmouseout = "This. style. backgroundcolor = 'white'">

  <TD style = "cursor: hand; color: Blue; text-Decoration: underline"
  Onclick = "VBScript: showrecord">

<Span dataworks = "title_id"> </span> </TD>
<TD> <span dataworks = "title"> </span> </TD>
<TD> <span dataworks = "type"> </span> </TD>
<TD> <span dataworks = "price"> </span> </TD>
</Tr>

Add a div tag under the table and specify the ID as message. Then, you can use the DIV tag to show the metadata as follows:
<Div id = "message"> </div>
Then add the following client program:

Sub showrecord
'Get xml dom information of XML dataisland and put it in Doc
Set Doc = DSO. xmldocument
'The selectnodes method of the xml dom object Doc is used to obtain the nodes of all the data points.
Set nodes = Doc. selectnodes ("// Z: Row ")
'The location where the title_id selected for the vertex is located and in the DIV tag
Show the XML content of a metric point
For each node in nodes
If node. getattribute ("title_id") = Window. event. srcelement. innertext then
Message. innertext = node. xml
Exit
End if
Next
End sub

In the above program, let the user click "showrecord" and then click "show record". First, get the xml dom information of XML dataisland and put it into the doc object, then, the selectnodes method is used to obtain the nodes set for all the data points, and then the information points selected by the for each cycle.
Node. getattribute ("title_id") can be used to retrieve the content of the specified vertex, window. event. srcelement. innertext can be used to obtain the vertex selection information. After comparison, if it is correct, it will show the content of the vertex and jump out of the circle through the DIV mark, the results are as follows:

Master displays detail information

Now that you have been able to obtain consistent single-dataset information, you must be able to use HTML to display it. Therefore, modify the showrecord program. For example:
Sub showrecord
Set Doc = DSO. xmldocument
Set nodes = Doc. selectnodes ("// Z: Row ")
For each node in nodes if
Node. getattribute ("title_id") = Window. event. srcelement. innertext then

  MSG = "<Table border = 1>"
MSG = MSG & "<tr> <TD> commandid </TD> <input name = text1 value = '"&
Node. getattribute ("title_id") & "'> </TD>"
MSG = MSG & "<TD> producer name </TD> <input name = text2 size = 50 value = '"
& Node. getattribute ("title") & "'> </TD> </tr>"
MSG = MSG & "</table>"
Message. innerhtml = msg

Exit
End if
Next
End sub

ADO-XML-DOM4.asp

Get the content of the resource through the getattribute method of the xml dom object, and use the textbox of the HTML tag to display it, as for how to present and show the market resources, the results are as follows:

Conclusion

Integrated with the use of traditional technologies, the author can see the xml dom object and the combination of DHTML, so that online production can change instantly, it is not only efficient but also convenient to implement failover. It is a very friendly method for users, and the above example also shows the use of textbox. Of course, you can also use form to adjust the role control value, the modification or division function is implemented.
In fact, in the Internet environment, there are indeed too many users on the Internet, it is of course the best practice to reduce the demand for zookeeper and the server's zookeeper, so the relevant XML technology will not be developed and applied on various platforms, people who do not understand XML can skip this step.
In the next article, I will explore the magic XML DOM (XML Document Object Model) of XML. What functions can the XML DOM perform in addition to accessing XML and obtaining XML? How should we deal with changes to resources?

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.