Tutorials to .com

Tutorials to .com » Asp » Xml » Order to use ASP to read and write their own method of XML documents (a)

Order to use ASP to read and write their own method of XML documents (a)

Print View , by: iSee ,Total views: 17 ,Word Count: 1993 ,Date: Wed, 22 Apr 2009 Time: 4:49 AM

Prior to this, I wrote a combination of two xml on asp postings were introduced to replace the use of xml database and xml integration with database technology in these two areas, so that in some cases, the database is no longer the only option for our . XML database and compared with the traditional, more the following advantages: a smaller file size, more flexible data storage, the document easier to read and write (because XML is a text file, so no special software production and maintenance), and XML technology is also the general trend of network development (from the. NET support for XML will be fully reflected in a). If you are interested, let us work together to write some of their Function, to a more convenient and intuitive to read and write XML.

At first glance, XML with the HTML format is very similar, indeed, because they are derived from SGML, the basic data are from the first label, the end tags, comments, labels and the composition of a number of plain text. But XML syntax than HTML in the more stringent requirements, a slight mistake, you will make the XML syntax errors, resulting in data which can not be read. For example, in HTML, we have to <a href="index.html"> written <a href=index.html> is absolutely no problem, but the XML in this way is absolutely not, XML attribute tag must be requested by a single quotation marks or double quotation marks. As for how to write a valid XML document, I have described in the previous two, we can search out, is not much to write here, the following is given a legitimate example of the XML document as well as the next we have to used.

================================================== =============== songlist.xml
<? xml version = "1.0" encoding = "gb2312"?>
<! - Note: The author posted please specify ->
<Song list>
<Song title = "Yesterday" time = "1:53" rating = "5" />
<Song title = "Imagine" time = "3:04" rating = "5" />
<Song title = "All You Need Is Love" time = "3:52" rating = "4" />
<Songs>
<Title> Come Together </ title>
<Time> 4:18 </ time>
<Rating> 4 </ rating>
</ Song>
<Song artist = "John Lennon">
<Title> Oh My Love </ title>
<Time> 2:44 </ time>
<Rating> 5 </ rating>
</ Song>
<Last modified modify people = "" time = ""> No changes <last modified>
</ Song list>

================================================== ==============================

This example XML should be able to fully demonstrate more flexible than the database side, although the records are stored each a song title, time, classification of the three, but we have a wide range of optional storage methods, both can be marked on the properties of the data, the data can also put a tag in the next show, like the above example, the two have no problem co-exist. At the same time, XML is also not required when you first set out the number of records, you record the second must be so out on the above example, we can add a song recorded, only the designated time, regardless of title and level. As each record label (for example, Article 1-5 of the "songs"), you can repeat can also be different, you see, I added in the final in front of a completely different "last modified" tab. In short, XML syntax in the request at the same time legal staff to the development of adequate freedom, prior to use in a database table to record a few things, we can now all in a XML in the. Now, I step-by-step demonstration of how to read and write to any one element.

First of all, like to open the database, we have to open our XML document. Like this:
Set rootXML = server.CreateObject ( "MSXML.DOMDocument")
rootXML.load server.MapPath ( "XML / config.xml")

"MSXML.DOMDocument is the windows" of a standard component of each machine have, please rest assured that use.

Then, in order to document the operation of the data, we have to build a similar cursor variable root, and it points to the root of XML document tags (ie <song list>):
Set root = rootXML.documentelement

If we take the XML document as a level marked by a tree structure composed of, now, we said that the root object is the document root tag, which is the first level node (an XML document, root tag must be unique). Therefore, we set up such a root object is to facilitate access to the next level of nodes in order to achieve control of the XML.

For our example of the XML document, it is the root tag <song list>, which marked the next level there are six, are the first five <songs> name and the last one is <the last modified>, Although different names, but they are of the same nature, we can put them together with the XML tree structure as the second-level nodes. Since we have already point to the root object node of the first-class, well, now
Let us stand up the first level nodes to operate the second-level nodes.

For example, we want a node at a lower level of the text tag, like <the last time modified> No changes <last modified> in the "no changes" this text. We can the following steps:
Make sure the marking is at this level in the first few nodes, in this case 6; good, determined immediately after the action:
text = root.childNodes.item (5). text

"Root.childNodes.item (5). Text" This is a code should be well understood, the pseudo-code is translated "directory. Subdirectory collection. The collection of the first five markers. Markup text," because of the XML element index is started from 0, we obtain a location marker, the point to remember the location of a number of further steps by 1, otherwise you will cross the border. For example, we have to obtain a node under the tag of the first text, we should use "root.childNodes.item (0)", rather than "root.childNodes.item (1)"

For convenience, we will be "the next level of access to the contents of tags," written in function of the operation in order to reuse:
Function getNodeText (ByVal nNode)
getNodeText = root.childNodes.item (nNode). text
End Function

With this function, we then have to be "no changes" Just when the following code:
text = getNodeText (5)

Not only to avoid the error, but also reduce the work input. In contrast, a marked modification of the contents of the code is:
root.childNodes.item (nNode). text = the string to be modified, we also write a function --
Function setNodeText (ByVal nNode, ByVal strNewNodeText)
root.childNodes.item (nNode). text = strNewNodeText
End Function

Now, we want to "no changes" to "the first record has been changed", you can do:
setNodeText 5, "the first record has been changed"

(Please use the revised rootXML.Save (file name) method to save the file in order to make changes.)
OK! We have can easily tag the next level, read and modify, the good, the next target, marking the property.

We first take a look at our second-level node of the first content tag:
<Song title = "Yesterday" time = "1:53" rating = "5" />
This is a special way of expression, only the first tab <songs>, but not the end of label </ song>, nor between the two labels are in the text directly to a "/" end. Oh, I am lazy, so I am more biased towards the end of this label wording omitted. However, we omitted this time of writing, we will not be able to add labels in both data and can not use our definition of the two functions above. At this time, the attribute tag has provided us with the only data storage container, above the symbol in the 3 groups on the storage properties, let us try the following to access it:
text = root.childNodes.item (0). attributes.item (0). value

Text variables so that the code was given "Yesterday" the string, do not believe what you print. Explained by the pseudo-code:
"Directory. Under a directory. Section marked 0. Markup attribute set. Set the first attribute 0. Attribute value corresponding to"
Talk nonsense, written in function
Function getAttValue (ByVal nNode, ByVal nAtt)
getAttValue = root.childNodes.item (nNode). attributes.item (nAtt). value
End Function

Since it is easy to use text = getAttValue (0,0) be a node under the first paragraph 0 tag attribute value of 0,
Attribute is indexed from 0 to start, remember.

As for amending the property, it is simple, direct assignment can be written as function:
Function setAttValue (ByVal nNode, ByVal nAtt, ByVal strNewAttValue)
root.childNodes.item (nNode). attributes.item (nAtt). value = strNewAttValue
End Function

Well, once we found that "All You Need Is Love" after listening to this song so good, want to level it up into 5, you can do so
setAttValue 2,2, "5"

Finally, remember that the changes to take effect, which is in operation use rootXML.Save (file name) of this approach.

When an XML document to read and write the text content and attribute values, I think you should basically be able to operate with proficiency in ASP elements of XML documents, as soon as possible with this technology in your ASP in it! Well, wrote here today that the next time I write together with the deletion of attributes, create tags, add tags, delete tags, copy tags, replace tags, and other inquiries by the conditions of the operation did not finish.

I am learning java and c + + and if you are too vbscript my dishes, then I can be the definition of those functions directly into a file, using XML, they use include statement to include the.


Asp and XML related Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.