Introduction to XML code generator--xmlfactory (i)

Source: Internet
Author: User

Introduction to XML code generator--xmlfactory (i)

Software development often to interact with third-party data, especially in the banking, telecommunications industry, this need is more essential, often a system and 35 other systems to interact with data, and data exchange messages are often in XML format. XML structure rigorous, conducive to human reading, but the format is more complex, with code parsing and assembling some cumbersome. Whether it's converting XML into an in-memory entity class object or serializing an entity class object to XML is a definitely work and time consuming. Fortunately, the problem always has a solution, here to introduce you an online code generator:xmlfactary. If you have a ready-made XML document, in most cases you don't need to manually write code to handle the conversion between XML and entity objects.

Xmlfactary This code generator, by parsing a given XML literal, called a sample XML, maps an element to an entity class, by summarizing all the sub-attributes (Attribute), child elements (element), and literal values (value) of each element To form the attributes of the entity class. An element with the same name will be mapped to an entity class. Here is an example of the general use of a brief introduction.

The first step is to prepare an XML document, you want to use the more familiar with the RSS format it. After parsing the following XML document with the code generator, three classes are generated by default: Rss,channel,item, which represents XML elements:<rss> <channel> <item>, respectively.

<rss version= "2.0" >
<channel>
<title> Reference message Electronic version </title>
<link>http://www.ckxx.info/</link>

<item>
<guid>http://www.ckxx.info/other1/201111/7-55584.html</guid>
<title> Iran nuclear issue near final showdown </title>
<description> Iran nuclear issue near final showdown </description>
<link>http://www.ckxx.info/other1/201111/7-55584.html</link>
<pubDate>2011-11-07</pubDate>
</item>
<item>
<guid>http://www.ckxx.info/other1/201111/7-55583.html</guid>
<title> the Japanese emperor hospitalized for illness </title>
<author>http://www.ckxx.info</author>
<description> the Japanese emperor hospitalized for illness </description>
<link>http://www.ckxx.info/other1/201111/7-55583.html</link>
</item>
</channel>
</rss>

I like the goal-driven way of learning, before learning a technology, it is best to know the results of the application of this technology and how to help, so I do not say how to do, but first to show the results of the finished to everyone.

Here: http://www.codingfactory.net/Page/XmlFactory/OutPut/XmlCSharp.aspx?id=8908 is the entire code generated from the above XML, it is best to first understand the code content , and then look at the following specific methods of operation is not too late.

In addition, this code generator is more flexible, each step has a lot of configurable items, this article does not expand, because the too trivial details will affect our understanding of the mainstream process. For a detailed description of the code generator, refer to the official Help documentation, which is for interested friends to know for themselves.

Help: Http://www.codingfactory.net/Page/XmlFactory/homepage/index.htm

Operation

1. Open this URL (is a flash file, more than 400 K, a little more than a moment): http://www.codingfactory.net/Page/XmlFactory/client/XmlFactory_Flex.html Glue the XML above to the sample XML .

2. Click next on the top right of the button, and the page jumps to the element/Sub-value tab. This page summarizes the sub-values of each element by element name, where you can adjust the element structure of the XML document.

The list of elements on the left shows all the XML elements (element), and the right-hand list shows all the sub-values under the selected element. Note that there are 2 <item> sub-elements under the <channel> element, so that its multiple columns are checked, and when the code is generated, this item is mapped to the class attribute in list<t> form. Of course, you can also put Title,link in front of the hook, so that when generating code, these two items will also become a list form.

The number of columns in the Child values list is the column header of the blue font, and the blue font indicates that the column can be edited . You can also adjust the sub-value structure of an XML element by adding sub-values , deleting sub-values , and so on, if an element is not needed at all, it is a direct point to Delete the element . For this example, we don't have to make any changes on this page.

3. Click NEXT to jump to the class name page. In this page, you can specify the entity class name for the XML element, which indicates that you want to map the element to the class of that name.

Note: Not all XML elements are mapped to entity classes. For example, the <title>,<link> element in this example, which contains only one sub-value: [value], of course, there is no need to define an entity class for this element, because the basic data type (int,bool,string ... ) to describe the value of this element. So, the name of the class is empty and nothing is filled out, which means that this element is not mapped to a class.

The three list fields in this tab are exactly the same, except for the following:

A In the top list, each element must be mapped to a class, meaning that a valid class name must be filled in the class name column.

b All elements in the bottom list contain only one child value, which is not necessarily mapped to a class, because the base data type (Int,double,string,datetime) can represent its contents. So the class name column should be empty.

C The elements in the intermediate list are suggested to be mapped to classes. You fill in the class name with the name of the class , and this element is mapped to a class; empty, this element will be shelled (the concept of shelling is somewhat complex, please refer to the official Help document for details).

This part is a bit complicated, but it doesn't matter, for our example, this page doesn't have to do anything, because the default setting is quite reasonable in general, and you just point to next .

4. Click NEXT to jump to the class properties page. This page you want to specify the corresponding property name and data type for the child value of the XML element.

The code generator provides the default data type based on the contents of the sample XML, and if it does not match your actual needs, you can expand the drop-down list of data type columns from which you want to select the base data type. You cannot change the data type of a child value that is already mapped to a class. For example, the element <item> in this example, which has been mapped to class item, so the class channel property items must be of item type and cannot be changed. Clicking the "item" cell in the property name column is manually changed to items, which indicates that the generated entity class has a list<item> items property in the channel.

5. Click NEXT to jump to the preview page. On this page, you can see the generated entity class definition. If there is any discrepancy between the findings and expectations, you can click on the previous tab to re-make the configuration.

There are several configurable items in the preview page, one for each:

Anemia mode The generated entity class contains only the attributes. The transformation functions of entity classes and XML are implemented in other classes (the class specified in the transformation class name)
Congestion mode The generated entity class contains not only attributes, but also entity classes and XML conversion functions
Package/Namespace The namespace to which the entity class belongs
Convert class name If you are using anemia mode, the conversion function of the entity class and XML will be implemented in the class specified here.

6. Leave the default configuration, the last time you click next , the generate code page pops up. This page has been seen before, it contains three parts of the code:

Entitydefine The entity class definition, if the congestion mode, also contains the conversion function.
Assistfunction consists of two auxiliary functions:loadfromfile and savetofile. If you look at the invocation, you know how the genetic code works.
XML Entity Convert entity objects and XML conversion functions (only content in anemia mode)

Summary

Well, finally finished. Summarize the general process in these steps:

1. input XML for analysis in sample XML tab

2. element/Sub-value tab for adjusting the configuration XML Structure

3. class name tab for specifying class name

4. Class Property page check for specifying class properties

5. Preview tab for configuring code generation items

Finally, talk about the inadequacy:

1. The generated code is based on the DOM model, which is not good for working with large files (but how big is it?). That depends on the level of hardware, hehe)

2. Currently beta, limited functionality, sample XML size limited to 2K, generate up to 6 classes at a time

Introduction to XML code generator--xmlfactory (i)

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.