XML (Extensible Markup Language) is a set of specifications created by the World Wide Web Alliance (W3C), used to organize and publish various information on the web. It not only meets the demand for rapidly growing network applications, but also has good reliability and interoperability when the network is interactive.
XML's syntax is similar to HTML, which is used to describe data. The HTML tag is fixed, we can only use and cannot be modified. XML is different, it does not have a predefined tag that can be used, but the label is defined according to the designs. XML is a meta-language that can derive many protocols and specifications according to different industries and semantics. The current basic protocol has XSL, XLINK / XPOINTER, XPATH, XHTML, Schema, etc. Different industries and areas can make their own XML specification for horizontal and vertical information exchange and data transmission. The XML specification has a large smaller, larger in the world, like EBXML, BizTalk, Eco; small is a Mathml, which is specifically used for the e-wallet, and is MATHML for multimedia. Synchronous SMIL, like XSL is specifically used to display, which makes many labels with semantics; and XLink is a language describing the relationship between documents. Like Micrsoft's CDF (Channel Definition Format) Push Channel "Push" technology application, WML (Wireless Market Language) wireless business applications, etc. are all specific applications of XML technology. XML gives powerful features and flexibility to web-based applications, so it brings many benefits to developers and users. Local calculation and processing can be easily performed using XML users. After the data of the XML format is sent to the customer, the customer can use the application software to parse the data and edit and process the data. Users can process data in different ways, not just to display it. XML Document Object Mode (DOM) allows data to be handled with scripts or other programming languages, and data calculations can be done back to the server. XML can be utilized to separate the interface of the user to view the data, using a simple and flexible format, you can create powerful applications to the Web.
The related features and specifications for XML core functions are as follows:
1. Good format (Well-Formed) XML document;
2. Document Type Definition (DTD);
3. XML uses styles-CSS (Cascading style Sheet, laminated style form) and XSL (Extensible Style Language, Extensible Style Language);
4. Document Object Model (DOM) -XML Programming Interface;
5. XML Simple API (SAX) -XML programming interface;
6. XML Namespace (Namespace) and Mode (SCHEMA);
7. Link (XLINK) and query;
8. XPath (XML Path Language) and XPointer (XML Pointer Language);
9. Convert XML-XSL and XSLT (XSL Transformation);
10. XML and database;
11. Used for server protocol - XML-RPC, SOAP (Simple Object Access Protocol, Simple Object Access Protocol), and WDDX (Web Distributed Data Exchange, Distributed Web Data Exchange).
XML is a language with a description of the data function, which is very suitable as a representation of the knowledge representation or as a component and file format. It also allows data to be processed in different sources, in accordance with general syntax rules. Java is used for Internet, suitable for distributed environments, providing a cross-platform language. The main reason for XML and Java is based on XML-based syntax provides a flexible, standard, robust Java programming, while Java also applies a universal semantic set to XML data. Since Java and XML are portable standards, the results of these two techniques are also portable, and data and transplant behavior can be reused. All the potentials you can get separately from XML or Java alone have not been fully grasped, combined with them into a huge, new country. Two main applications that apply XML to Java are intermediate-oriented intermediate technologies that are expressed in publications and enterprises. Java and XML are naturally matched when developing a web program for different customers. From traditional calls to the latest smart refrigerator, the information used and generated is exchanged on a server of different system platforms. The convenience and ductility of XML and Java make them more features on the new Web:
1. Structured - establish a data model with any complex level;
2. Scalability - Define a new sign as needed;
3. Verify - Check the correctness of the data in structure;
4. Independence & Media - Publish content in a variety of ways;
5. Independent vendors and platforms - using standard commercial software, even text tools to handle documents that meet (XML standards).
The above functions allow XML technology to be desirable to provide a network-oriented application, especially in the field of data exchange. Java with XML makes a new generation web application possible.
Java XML PACK
Since XML information encodes easy to read, process, and generate, XML is a preferred technology in many information exchange. Java can think of XML's "colleague", because both languages have the same historical background (C , SGML), which have simplicity, convenient and portability, and continue in industrial, academic development ( W3C, JCP). It is for example, Java has become a preferred language in the development of the server and client XML program. In order to establish an XML-based program, the Java software platform has fully collected the API core specification, with 6 (JDK1.4). They are JAXP (Java Api for XML Process), JAXB (Java Architecture for XML Binding), JAXM (Java Api for XML Messaging, JAX-RPC (Java Api For XML-Based RPC), JAXR (Java API for XML Registries) ), JAVA DOM. Description is as follows: 1. JAXP supports processing XML documents using DOM, SAX, and XSLT. JAXP allows program analysis and conversion XML documents independent of a special XML process process. Based on the needs of the procedure, developers can be simultaneously exchanged between XML processing, without changing program code. In this way, program and tool developers can quickly and easily write Java applications that can support XML. The implementation of the JAXP parameter includes a conversion engine that supports SAX and DOM, high quality parsers and a conversion engine that supports XSLT. The latest version of JAXP 1.1 allows the most recent SAX-2 and Dom Level 2, as well as the XSLT based on the TRAX frame (for XML conversion API). The initial version JAXP 1.0 supports SAX-1 and Dom Level 1. JAXP 1.1 can be used as a, optional package for JDK 1.1.8 and higher platforms, perhaps in the future, in J2SE (Java 2 Platform Standard Edition) 1.4 and J2EE (Java 2 Platform Enterprise Edition) 1.3 . 2. JAXB provides an API tool that automatically establishes mapping between XML documents and Java objects. JAXB can make XML easy to use one or more classes that compile into an XML plan. The class that has been generated provides details related to XML parsing and format, which guarantees for forced constraints in the plan. In many events, it is very effective using a SAX (Simple API for XML) parser or performing a DOM (Document Object Model) API. JAXB 1.0 will become a selectable package for a Java 2 platform. 3. JAXM's optional package allows programs to send and receive documents that use pure Java API to direct XML. The SOAP (Simple Object Access Protocol) 1.1 and attachments of the JAXM tool allow developers to set, send, receive, and decompose packets for their programs, which replaces low-level XML communication programs. This version of JAXM specification includes Messaging Profiles concept. Its purpose is to establish a foundation that allows it to support a high-level standard-based information protocol. An example of a profile is to perform EBXML transmission, route, and packaged information processing services, or XMLP based on JAXM-based W3C. Therefore, Java technology developers will have the ability to hang XML information when using JAXM. These high-level agreements often achieve some additional functions outside the basic SOAP information. This additional function is often useful for security, availability, and integrity of information. JAXM 1.0 will be used as a selectable package for J2SE. 4. JAX-RPC provides an independent transmission API for standard XML based on RPC protocol.
JAX-RPC 1.0 will be used as a selectable package for J2SE. JAX-RPC will include J2SE release or J2EE in the future. JAX-RPC has recently been recognized by Executive Committee of the Java Community ProcessSM and has established an expert group. The JAX-RPC specification will be used as JSR-101 to develop JCP processes. 5.jaxr provides a unified and standard Java API to deliver different kinds of XML Registries. XML REGISTRIES can establish, configure, and discover web servers. The current XML Registries specification is ISO 11179, Oasis, Eco Framework, EBXML, and UDDI. JAXR API allows Java programmers to use a simple and easy to use an abstract API through different XML Registries. The JAXR information pattern of the JAXR API makes it easy to log content and data in XML Registries. JDK1.4 JAXR API specification includes details established between the JAXR information model with EBXML Registry and UDDI Registry. The JAXR API works in a core specification associated with Java APIS for XML, such as JAXP, JAXB, JAX-RPC, and JAXM to allow web servers to include J2EE platforms. JAXR 1.0 will be used as a selectable package for J2SE, which will be included in J2EE in the future. 6.jdom is an API for Java read, write and operating an XML document. Jdom is compatible with current SAX and DOM standards, providing Java programmers with a simple, lightweight XML document operation method. It is to be pointed out that JDOM is developed for Java programmers, so many Java language excellent features, such as method overload, collection (Collections, this is the data operation type in Java2) and class mapping. JDOM is an unique Java kit for quickly to quickly develop XML applications. It can replace the org.w3c.dom package to operate XML documentation. In fact, Jdom and DOM can becomes concurrent. Standard DOM files consist of text nodes, element nodes, processing command nodes, and many other nodes. However, these nodes have caused a loose DOM object, which has also caused the difficulties in the application. Since JDOM has no hierarchical features, it builds an XML file into a Java object, which makes it easy to use XML in Java, and solve difficulties in DOM applications. Second, JDOM uses class drive, does not need to perform factory interface configuration like DOM, so JDO is straightforward. Since the JDM object is like using Document, Element, and Attribute these class of direct instances, create a new JDOM object is as easy as using the New operator in the Java language. Java and XML combined application
Establish a well-final XML document and verification instance for an XML file that complies with the XML basic syntax specified in Well-Formed, must be checked by the XML analyzer. More common XML analyzers have Xerces, JAXP, and MSXML. An XML document is significant. Below is an instance of an XML document, and the code is as follows:
XML Version = "1.0" encoding = "GB2312"?>
People>
People>
People
If an XML document meets some of the features exhibited by the above document, it can be referred to as an integrity XML document. These features are as follows: 1. Each element must have initiation and end tags; 2. There is only one root element; 3. Correctly format the empty element; 4. Start and end labels must match (case sensitive); 5. The element must be nested correctly; 6. The attribute value must be enclosed with quotation marks. Isxmlavailable.java is a Java program for performing an XML document. The code is as follows:
//Iisxmlavailable.java
Import java.io.file;
Import javax.xml.parsers. *;
Import org.w3c.dom. *;
Import org.xml.sax. *;
Public class isxmlavailable {
Public static void main (String Argv []) {
IF (argv.length! = 1) {
System.out.println ("Parameter Error!");
System.exit (1);
}
Try {
/ / Get a new instance of factory class
DocumentBuilderFactory DBF =
DocumentBuilderFactory.newInstance ();
// Create a DocumentBuilder instance by static method
// Prepare the Document object
DocumentBuilder DB = dbf.newdocumentbuilder ();
// Establish an XML document object
Document doc = db.parse (New file (Argv [0]));
/ / Format the document
Doc.getdocumentelement (). Normalize ();
System.out.println ("XML file structure is correct!");
}
Catch (SAXPARSEEXCEPTION E) {
// Error prompt, systemid is the full path name
System.out.println ("XML in the first"
E.GETLINENUMBER () "line error error, file information: E.GetsystemId ());
System.out.println ("/ n" E.getMessage ());
}
Catch (SAXEXCEPTION EX) {
EX.PrintStackTrace ();
}
Catch (Throwable TH) {
Th.PrintStackTrace ();
}
}
}
Run the "Java Isxmlavailable People.xml" command, if the program is wrong, the first line is wrong and gives possible error prompts; if the program is correct, the "XML file structure is correct!" To create an effective XML document ( DTD) and verification instance good XML document only illustrate its syntax correct and does not resolve semantic issues. XML describes data with certain meanings, which can specify rules that contain data. With this specified rule, you can verify that the validity of the XML document that matches the rules, that is, the DTD - document type definition. DTD ensures that the application receives a valid XML document and then designs an application interface for a certain class XML. This allows the XML document that meets a certain DTD, can be parsed by the application, or use DTD to develop its own document rules, and this rule can be utilized by other applications. DTD has two ways to declare an XML file: (1) The external DTD declaration is as follows:
(2) Internal DTD declaration (PeopleDTD.xml) is as follows:
XML Version = "1.0" encoding = "GB2312"?>
>
]>
People>
People>
People
From the above file, you can see the embedded DTD
Mark. First declared the root element of the XML document
, Then specify
There must be one or more in the root element.
Elements (represented by people). The next sentence is defined
Elements must be included
3 elements. The content type of these three elements is #pcdata, indicating that the content is a text type. Then the definition of the attribute, indicating that the PeopleInfo must contain the peopleID property. #Required (must be included) The default can be omitted, and the CDATA indicates that the attribute value is a string type. To check if an XML file is valid if it is checked if it is valid when parsing the XML file. First, use the statement to set a validity check before creating the DocumentBuilder object, the statement is as follows: dbf.setvalidating (true); // Set effective verification of the XML file
Because SAX is parsing the XML file, encountering the corresponding event to trigger the corresponding event, but also define the class that implements the event interface in the program. The static internal class of commonly used processing events is used here, which must be implemented in this class as follows:
Public void Warning (SaxParseException Spe) THROWS SAXEXCEPTON
Public Void Error (SAXPARSEEXCEPTION SPE "THROWS SAXEXCEPTON
Public void Fatalerror (SAXPARSEEXCEPTION SPE) THROWS SAXEXCEPTON
Error events of different levels will trigger one of these three methods. In Isxmlavailabledtd.java, a source program for validity verification of XML documents is provided. You can see the Writing method of the XML program of the SAX event driver http :.
Run the "Java ISXML AVAILALALALALALAL command, if the XML file is valid in line with DTD definition, the program prompts" XML file is valid! ". There are two methods and XSLT 1 displaying XML data. Call the CSS file (1) Write a Hell.css style sheet; write a statement as "Hello {Display: block; font-size: 48pt; font-weight: bold; font-style: italic;}", where Hello is The name of the style, the subsequent attribute Display, Font-size, Font-Weight and Font-Style indicate that the text is displayed, the size is 48 pounds, the text is bold, the font is a slope. (2) Write an XML file. In the XML file, such as 1.xml, join a line of code for
1.xml source code code as follows:
XML Version = "1.0" Standalone = "YES"?>
xml-stylesheet type = "text / css" href = "hello.css"?>
Hello, The World!
hello>
2. Call the XSL file through the statement
XML-Stylesheet Type = "text / xsl" href = "2.xsl"?>
"Tune 2.xsl file in the XML file. The" Type = "Text / XSL" property defines the file type, "HREF =" 2.xsl "property is used to define the path to the reference file. The statement format is as follows:
h1>
xsl: for-energy>
among them"
"Is an empty mark, used to display elements"
"The contents of the .2.xml file is as follows:
XML Version = "1.0" Standalone = "YES"?>
XML-Stylesheet Type = "text / xsl" href = "2.xsl"?>
Hello, The World!
hello>
2. The source code for the xsl file is as follows:
XML Version = "1.0"?>
h1>
xsl: for-energy>
body>
html>
xsl: template>
xsl: stylesheet>
3. XSLT XSLT (XSL Transformations) is primarily used to process and output an XML document. The processing function refers to the conversion of the XML document, extracts, sort the elements in the XML document. Output function refers to an XML document with some way. By using the XSLT developer, a method of establishing a new structured document from an existing XML document can be described, and this method is implemented by the XSLT engine. XSLT is not a programming language. Its implementation method is to give an example instead of describing the execution process. XSLT is implemented in conjunction with the pattern (Template) with template. The mode matches the elements in the source tree. The template is instantiated to generate a portion of the result tree, and the tree is separated from the source tree, and the structure of the tree can be different from the source tree. The source tree can be filtered and re-sort in the construction of the result tree, and any structure can be increased. Below we build our own XSLT, first set the needed package to the program. MyTransformer's structure and DOM and SAX are very similar to XXXFactory and corresponding errors. In addition to basic objects, MyTransformer needs to load DOMSource and StreamResult, which are used as the input and output of Transformer. The source code of MyTransformer is as follows:
//Mytransformer.java
Import javax.xml.parsers.documentbuilder;
Import javax.xml.parsers.documentBuilderFactory;
Import javax.xml.parsers.FactoryConfigurationError;
Import javax.xml.parsers.Parser ConfigurationException;
Import javax.xml.transform.transformer;
Import javax.xml.transform.transformerfactory;
Import javax.xml.transform.transformerexception;
Import Javax.xml.Transform.TransformerFactory ConfigurationError; import javax.xml.transform.dom.domsource;
Import javax.xml.transform.Stream.streamResult;
Import java.io.file;
Import org.w3c.dom.document;
Public class myTransformer {
Public static void main (String [] args) {
IF (args.length! = 1) {
System.out.println ("Java MyTransformer [Destination File]");
Return;
}
DocumentBuilderFactory DomFactory =
DocumentBuilderFactory.newInstance ();
Domfactory.setNamespaceaware (TRUE);
Domfactory.setValidating (true);
Try {
DocumentBuilder Dompival = DOMFACTORY. NewDocumentBuilder ();
Document Doc = DOMPARSER.PARSE (ARGS [0]);
TransformerFactory Factory =
Transformerfactory.newinstance ();
TRANSFORMER TRANSFORMER =
Factory.NewTransformer ();
Domsource Source = New Domsource (DOC);
StreamResult Target = new streamResult (system.out);
Transformer.Transform (Source, Target);
} catch (FactoryConfigurationError FCE) {
System.err.Println ("DomFactory Setup Error"
fce.getMessage ());
} catch (ParserConfigurationException PCE) {
System.err.Println ("DomBuilder Setup Error"
PCE.getMessage ());
} catch (exception e) {
System.err.Println ("Other Errors"
E.getMessage ());
E.PrintStackTrace ();
}
}
}
XML programming interface - Dom and SAX 1. The DOM programming DOM is a set of APIs designed for legitimate Well-Formed files, which defines logical structures, access, and operation methods of these files. In the DOM specification, the definition of the file is wide, including HTML and XML. Therefore, DOM can also be used to operate XML and HTML files, or use DOM to create files, browse file structures, and some contents of increasing or deleting files. The DOM is developed by W3C. The goal is to provide an API that can be used in various programming languages, operating systems, and applications, so DOM has high compatibility, regardless of CORBA, COM, Java, C , etc., the same DOM interface can be used. . Also because of this cross-platform demand, DOM uses a CORBA standard when defined, so it can provide exactly the same interface for each language. DOM is a document object model. XML will organize the data into a tree, and the DOM is a description of the object of this tree. It is popular, that is, by parsing the XML document, the XML document is logically established a tree model, and the node of the tree is an object. We can access the contents of the XML document by accessing these objects. There are 5 basic objects of the DOM, which are Document, Node, NodeList, Element, and Attr. Here, an example of observing the DOM object via a graphical interface. There are JTREEs to express the concept of trees in Java's standard bag, combined with Java's window JFrame, complete a DOM object viewer similar to the IE browser feature. DetreeView.java's source code is shown in journals of http://linux.ccidnet.com Browse No. 11. 2. SAX Programming SAX (Simple Application Interface for XML) is an API that is defined for an event XML parser, free, with platforms and language, which allows programs and scripts to dynamically access and update document content, structures, and styles. . Therefore, it is used as an middle part layer to interpret the data in the XML document into a corresponding Java event. SAX This interface specification is the XML analyzer and processor, which can provide applications with greater flexibility. SAX is an event-driven interface. Its basic principle is that the user provides a defined processor, and XML analysis encounters a specific event, and the processing function of a specific event in the processor is called. General SAX is a Java interface, but C can also use SAX interfaces, only C analyzer is relatively small. SAX requires the user to provide the following three processor classes: (1) DocumentHandlerxml: Processor for file events; (2) DTDHandler: DTD processor; (3) Errorhandler: error processor. Here, an example will be taken, it implements a number of ContentHandler interfaces for processing XML files, and how SAX analyzes XML data. The source code code of MySaxParser.java is as follows (section section):
//Mysaxparser.java
Import java.io.ioException;
Import java.io.fileinputstream;
Import java.util.properties;
Import org.xml.sax.attribute;
Import org.xml.sax.contenthandler;
Import org.xml.sax.errorhandler;
Import org.xml.sax.locator;
Import org.xml.sax.saxexception; import org.xml.sax.saxpivalseException;
Import org.xml.sax.xmlreader;
Import org.xml.sax.helpers.xmlreaderfactory;
/ **
* SaxParser code> You can read an XML file from the command line and use the SAX standard mechanism to analyze the file.
* /
Public class mysaxparser {
/ **
*
* We will process XML files using standard SAX processing mechanisms here.
* p>
* @Param URI URI of XML files to analyze
* /
Public void Parsexml (String Uri) {
Try {
System.out.println ("" XML file is being analyzed: " URI " / N ");
XmlReader Parser =
XmlReaderFactory.createxmlReader
"Org.apache.xerces.Parsers.saxparser");
// Xmlreader Parser = new saxparser ();
// Register the designed content processor
Parser.SetContentHandler (New MyContentHandler ());
// Register Error Processor
Parser.setdler (new myerrorhandler ());
// Analysis file
Parser.Parse (URI);
} catch (ioexception ie) {
System.out.println ("File Read Error:" IOE.GetMessage ());
} catch (saxexception SAXE) {
System.out.println ("XML Analysis Error:" SAXE.GETMESSAGE ());
}
}
/ **
*
* The main program uses the parameters of the command column to specify the XML file to be analyzed.
* p>
* /
Public static void main (String [] args) {
/ / If the number of parameters is wrong, the output instructions are output, and the program is ended.
IF (args.length! = 1) {
System.out.println ("Please enter the name of the file called:" "Java EXAMPLE3_1 [XML URI]");
System.exit (-1);
}
String Uri = args [0];
Mysaxparser myparser = new mysaxparser ();
MyParser.Parsexml (URI);
}
}
/ **
* ContentHandler code> Real
org.xml.sax.
* ContentHandler code> interface and provides a related callback method.
* All analytical processes related to the content of XML file are resolved here.
* /
Class mycontenthandler imports contenthandler {
........
........
Java and XML --JAXP programming applications are in many ways, and the Java language has become a very suitable programming language for XML selection. In JAXP (Java API for XML syntax analysis) includes 3 packages: 1. Org.w3c.dom -w3c recommended Java tool for XML standard planning document object model; 2. ORG.XML.SAX- Simple API for event drivers for syntax analysis of XML; 3. Javax.xml.Parsers - Factory tools allow application developers to get and configure special syntax analyzer tools. JAXP is used as a public interface of Java applications and XMLs to ensure complete compatibility of XML analytics such as Sun, IBM, and Micrsoft. JAXP is a standard XML language development interface provided by Sun, and the highest version is 1.1. JAXP1.1 contains two parts: one is part of the Parser, located in the javax.xml.parsers package, is responsible for parsing the XML document; the other is the Transform (TRAX) section, located in the java.xml.transform package, responsible for XSLT conversion work . The JAXP1.1 contains 3 JAR files, named jaxp.jar, crimson.jar and Xalan.jar. JAXP.jar provides a range of interfaces and abstraction classes that are interfaces used during development; CRIMSon.jar defines the parser that actually used; the TRANSFORM processor is defined in Xalan.jar. The analyzer of JAXP1.1 provides SAX2 and DOM2 support, and supports XML1.0 second edition, also supports namespaces. The TRAX section of JAXP1.1 provides support for the XSLT recommended standard 1.0 version. The following examples are to obtain DOCUMENT Builder to create a DOM object set file using JAXP, respectively, and obtain an SAX syntax analyzer using JAXP to analyze an XML file. (1) DomDemo.java Source Code For details, please refer to Journal of Journals of Http://linux.ccidnet.com. (2) SAXDEMO.JAVA Source Code See the Journal of Http://linux.ccidnet.com Section 11. There is not much to say in JAXB and JDOM programming. Combination meaning