XML now seems that any software development can not be separated from the XML technology support, in graphics, databases, encryption security, software engineering, network Education, E-commerce, voice technology has a place where XML, the tide of XML application has come.
XML Working Group founder member C.M. Sperberg-mcqueen that: "The biggest impact of XML is the rise of XML software: XML parsers, XML programming language libraries, XSLT processors, XSL FO processors, database acceptance xml-not only that, There are also Web browsers that accept XML. "It is also because of this, IBM, Microsoft, SUN, HP, Oracle and other large companies have entered the market."
In learned the Xgen and other object binding tools, I believe that we have been eager to use xgen to combat, to experience the advantages of XML object binding. Here's a look at some of the XML apps I use a lot.
1. XML configuration file
Each system may require a large or small configuration file, the configuration file to initialize the parameters of the system, the benefits are not described in detail. The general configuration file has the following formats:
1. Window System INI format file and the properties file used in the Java language
2. XML-formatted files
3. Other format documents
The first type of configuration file is a plain text file, the basic use of "key = value" format to record a variety of parameters, easy to write and read manually.
XML Schemas based XML files are easy to read, and they can show the hierarchical relationship and constraint relationship among different elements very well. A configuration file that uses XML format relative to the INI file format has the following advantages:
1.1. Configuration with Hierarchy
1.2. Check the validity of the value
1.3. Support linked lists, enumerations, complex data types
1.4. Configuration files can be nested
1.5. XML editing tools, such as XML Spy, are very handy to edit configuration files
1.6. There are a large number of Third-party XML object binding tools, and powerful, easy to develop. such as the Java language version of the Xgen, jaxb,c++ version of the tool Xbind
Now let's start to practice using XML as a configuration file for your program.
1.1. Design xsd files (XML Schema)
Xgen need to compile an XSD file, which is an outline file that describes the XML file of the specified type, and is a plain text file. The editing tool allows you to create and edit XSD files manually, but with some XML editing tools you can do the work of XSD writing with less effort. I've also used some XML editing tools, but only XML spy is the most powerful and easy to use.
Some features of XML Spy:
l have prompt input when editing XML, XSD files and so on, which is very convenient for you to choose.
L also have the validation function of XML file, which can determine whether the value of element value conforms to the definition of schema.
L Support DTD and XSD interchange
L provides sample XML instance file functionality for XSD
L also support java,c++,c# XML binding, can generate java,c++ and C # code, not too strong Oh!
, otherwise there will be an exception in the Unmarshal (InputStream, valid) method invocation. This setting represents the restriction of local elements and attributes, that is, to prefix each local element.
When elementFormDefault is set to qualified, it represents that in the instance of the syntax, all elements must be explicitly qualified with a prefix or by setting the {default namespace}. The unqualified setting means that only elements that are globally declared must be explicitly qualified, while elements of a locally declared element may not be qualified. In this case, it is an error to qualify a local declaration. Similarly, when you set attributeFormDefault to qualified, you must explicitly qualify all attributes in the instance document with a prefix.
1.2. Create Java Objects
Enter the Xgen installation directory, in order to save the Alertserver.xsd file to copy to the directory. Execute the following script:
Xgen alertserver.xsd
In the current directory, the Com\lianchuang\smartsecurer\alert\config\configfile_xsd directory is generated according to the urn path, and the newly created class file is saved in the directory. If custom complex type data is defined in the XSD, the configfile_xsd
Directory to create the \type directory, and put the relevant Java Class in the directory.
1.3. Write code
For defining XSD documents, the Elementa of each complex type will have Elementa and Elementatypecomplextype classes to map the Java class. You can get a reference to a complex object type that obtains Elementa directly from the Getelementatypecomplextype () method.
As in globe in an XSD file, you can get the Java object by Xxx.getconifgtypecomplextype (). Getglobe (), through Config.getconifgtypecomplextype (). Getglobe (). Getglobetypecomplextype () to get the actual object of that type.
For some basic type element. Xgen defines some of the classes that begin with X, such as int, expressed in Xint. You can construct a Xint object by using the new xint (int i).
The following is the code that reads the specified configuration file and returns the Java object
public static synchronized Config GetConfig (String fileName) throws
FileNotFoundException
{
Creating InputStream Objects
File File = new file (fileName);
FileInputStream ins = new FileInputStream (file);
Chainedentityresolver er = new Chainedentityresolver ();
Unmarshaller un = new Unmarshaller (er);
Globalelement GE = null;
Try
{
GE = Un.unmarshal (ins, false); Note that because I have a smattering of XML and XSD, the Unmarshal method tries it, and only use this method to convert the object of the XML file to normal. Don't know why ...
if (GE!= null && ge instanceof Config)
Configinstance = (Config) GE;
}
catch (Validationexception ex)
{
M_log.error (ex);
}
catch (Marshalexception ex)
{
M_log.error (ex);
}
catch (IOException ex)
{
M_log.error (ex);
}
return configinstance;
}
The following is a sample code for each property parameter of the Print object
2. Using XML format for message communication
When writing a custom socket communication program, you need to define a set of communication protocol specifications, especially heterogeneous systems. We can use XML documents as communication protocols, and combine XML-related object binding tools to convert XML-formatted messages into objects in languages such as Java and C + +.
The following is a combination of the Java language xgen using a sample to describe the flow of traffic.
2.1. Communication flow
L Send XML objects
Create related objects by program, and assign values. Transformed into a StringWriter object using the Unmarshal method, the GetBuffer (). ToString () method of StringWriter returns a converted string. Finally, use the socket to send the string.
L Receive XML objects
Once you receive the XML document, you can marshal to the Java object.
2.2. Specific technical
L Encryption in Communications
Because of the communication through an XML document, the packet is plaintext text, and appropriate encryption is required for some sensitive data. If DES is passed directly on the XMLW document, or a more secure encryption method is used depending on the situation.
L Send an XML document
UDP Socket
Datagramsocket discoverysocket = null;
Packets Sent
Datagrampacket DGP = null;
Converts an XML object to a StringWriter object, which translates to a string object
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.