Conversion Between XML documents of different formats (1) Basic Conversion

Source: Internet
Author: User

This article will try to discuss the topics of conversion between XML documents of different formats, such as how to convert formatted pure data into HTML documents, VML documents, or XML documents of other formats. Further, how to dynamically calculate the target data (element/text/attribute) from the source data (element/text/attribute) according to each specific requirement. Further, how to attach specific JS Code to the generated target data, further develop how to make the target data a part of its container elements and even the parent page, and achieve dynamic interaction. Finally, how to clean up the garbage, especially for containers with frequent data updates, can we promptly clear expired document structures and object references without affecting newly installed content and new objects.

An XML document conversion tool should have at least two functions, template and addressing. The template is used to define the structure of the target document. The addressing function is used to return data in the source document in a specified path, insert the addressing result into a specific location of the template, parse the template, and generate the target document, this process can achieve static data conversion. See the following code.

Source and template documents

 Student. SRC. xml

     1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <students>
3 <student Sn = "001">
4 <Name> Zhang San </Name>
5 <age> 19 </age>
6 </student>
7 <student Sn = "002">
8 <Name> Li Si </Name>
9 <age> 20 </age>
10 </student>
11 <student Sn = "003">
12 <Name> Wang Wu </Name>
13 <age> 18 </age>
14 </student>
15 </students>

 

   Student. TPL. xml

     1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <template rootname = "table">
3 <attribute name = "border" value = "1"/>
4 <element name = "TR">
5 <attribute name = "bgcolor" value = "# 6699ff"/>
6 <element name = "TD">
7 <text> Student ID </text>
8 </element>
9 <element name = "TD">
10 <text> name </text>
11 </element>
12 <element name = "TD">
13 <text> age </text>
14 </element>
15 </element>
16 <foreach base = "/student">
17 <element name = "TR">
18 <element name = "TD">
19 <text Path = "." ATTR = "Sn"/>
20 </element>
21 <element name = "TD">
22 <text Path = "./Name"/>
23 </element>
24 <element name = "TD">
25 <text Path = "./age"/>
26 </element>
27 </element>
28 </foreach>
29 </template>

 

 

Conversion code

 1 var srcdoc = $System.XML.XMLUtil.loadXMLFile("xmltest\\student.src.xml");
2  var tpldoc = $System.XML.XMLUtil.loadXMLFile("xmltest\\student.tpl.xml");
3  var tdoc = $System.XML.XMLUtil.XDOC.transformDoc(srcdoc , tpldoc);
4 $("div005").innerHTML = tdoc.xml;

 

 

Conversion Result

 

The attribute element in the template document is used to generate the attributes of specific elements in the target document. The foreach element is used to repeat the set of specific elements in the source document.

Run the test code, download the compressed package in the previous log, decompress the package, create the xmltest folder under the directory of the test001.hta file, and save the source file as student. SRC. XML, the target document is saved as student. TPL. XML, copy the conversion code to test001.hta, and run the command to view the conversion result.

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.