Operate data through DOM (lower)

Source: Internet
Author: User
Tags xsl
1. Dom parsing errors
Dom may cause various errors when parsing XML documents. You can learn the possible causes and related information of Errors Based on the attributes of the parseerror object.
Common attributes and their meanings are shown in the following table:
Attribute Description
Errorcode Error Code
Filepos Absolute character location of the error in the document
Line Row number of the row where the error is located
Linepos Character position of the row where the error is located
Reason Error cause
Srctext Source code of the row where the error is located
URL The URL of the latest XML document containing parsing errors
2. Access elements and attributes in the DOM tree
Dom also provides many methods for searching nodes. The search-based methods include:
● Search for elements by Tag name;
● Search nodes in XSL mode;
● Use the SET index to search nodes.
Taking books. XML as an example, the getelementsbytagname method in the document object is used to search for elements in the full text range based on the tag name in the parameter. The returned value is a nodelist object:
Set Doc = dsodetails. xmldocument
Set authors = Doc. getelementsbytagname ("author ")
The preceding query results contain all four authors in the document. If you call the getelementsbytagname method in the element object, except for narrowing the search scope to all subsequent nodes of the element, the other conditions are the same.
All types of nodes carry the selectnodes method. The unique parameter of this method is the XSL Pattern Rule, and the return value is the result set that matches the rule. To call this method, you can use the XSL pattern matching policy to find nodes. For example:
Set rootnode = doc.doc umentelement
Set cheapbooks = rootnode. selectnodes ("// book [price <10]")
In this example, all <book> elements whose prices are less than 10 RMB are returned. In addition, the selectsinglenodes method in the node has the same usage as selectnodes, but the returned result is the first node that meets the search criteria.
For element nodes, there are two methods to obtain the element Tag Name: anyelement. nodename and anyelement. tagname. The former is the attribute of the Node object, and the latter is the attribute of the element object.
If you want to obtain the text content in the element, for example, <price> 9.95
</Price> when accessing the nodevalue attribute in the element object, the returned result is null instead of the expected 9.95. All elements that contain text content contain a subnode of the text type. Therefore, only the nodevalue attribute in the Text object can access the text content.
To add an element, follow these steps:
● Create a text node and assign values;
● Create an element node;
● Hanging the text node under the element node as its subnode;
● Insert the element node to the appropriate location of the XML document.
To delete or replace an element node, you must first locate the operation object and then execute the removechild method and replaceChild method of the parent node of the object node.
The operations on the ATTR node are in principle the same as those on the element node. The ATTR object also inherits various methods and attributes of the Node object, and MSXML also provides the name and value attributes, allowing you to directly access the attribute information. In addition, you can access attributes through the relevant methods of the element to which the attribute belongs. For example, you can use the getattribute and setattribute methods to read or modify the attribute values, or use the getattributenode method to directly return the ATTR object.
The most direct way to create a new property is to use the setattribute method in the element object. You can also set the attribute value using the createattribute method in the Document Object, and then add the new node to the DOM tree using the setattributenode method in the element object. Similarly, the most direct method to delete an attribute is to call the removeattribute method in element. Another solution is to first use the getattributenode method to locate the operation object and then perform the removeattributenode operation.
From the above introduction, we can see that due to the inheritance relationship between nodes and the rich interfaces provided by various types of nodes, you can easily find an object operation solution that suits your needs.
3. Dom display
DOM technology can also be used to display XML data. The XSL style is a single-sided conversion of XML documents, which is used to convert the display format. Therefore, there are still some shortcomings in the display function:
● It is difficult to complete complex processing of XML data, such as converting all English letters into uppercase letters, intercepting strings of the specified length, and ignoring some specific punctuation marks;
● It is difficult to calculate values in XML data;
● An XSL usually acts statically on an XML document and cannot be combined and converted into an output result using one XSL.
Using Dom can solve the above problems well, and the scripts written can be executed on both the server side and the client side. On the client side, you can create a friendly user interface to interact with users. on the server side, you can use the script to use Dom to organize and send data as required by the client.
The following example converts an XML document into a table in HTML:
Dim outstr
Outstr = "<Table border = 1>"
Set listorderitem = Doc. selectnodes ("// orderitem ")
For each node in listorderitem
Outstr = outstr & "<tr> "&_
"<TD>" & node. getattribute ("title ")&_
"</TD> "&_
"<TD>" & node. getattribute ("ISBN ")&_
"</TD> </tr>"
Next
Outstr = outstr & "</table>"
In the following example, we perform a numerical operation on the XML data to calculate the average book price:
Totalprice = 0
Set listprice = Doc. selectnodes ("// price ")
For each node in listprice
Totalprice = totalprice + node. firstchild. nodevalue
Next
Avuplice = totalprice/listprice. Length
The last example shows the powerful function of Dom to combine and convert multiple XML documents:
<XML id = "dsoorders" src = "orders. xml"> </XML>
<XML id = "dsocustomers" src = "MERs. xml"> </XML>
Set docorders = dsoorders. xmldocument
Set doccustomers = dsocustomers. xmldocument
Outstr = "<p> Number of order items :"&_
Docorders. selectnodes ("// orderitem"). Length &_
"<P> Number of MERs :"&_
Doccustomers. selectnodes ("// customer"). Length

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.