Currently, the Java 2 Platform Enterprise (J2EE) architecture is highly respected in the manufacturer market and developer community. As a tool, the Scalable Markup Language (XML) simplifies data exchange, inter-process messages exchange this class, and thus gradually attractive to developers and begins to popularize. Naturally, the idea of accessing or integrating an XML solution in the J2EE architecture is also attractive. Because this will be a combination of powerful system architecture with highly flexible data management solutions. The application of XML seems to be endless, but they can be roughly divided into three categories: ● Simple data representation and exchange (simple API (SAX) and document object model (DOM) grammar parsing, different document types Definition (DTDS) and Summary (Schemas)) ● Batter-oriented calculation (XML-RPC (remote procedure call), SOAP protocol, electronic service XML (EBXML)) ● User interface related, representation related context (scalable style sheet Language (XSL), Extended Style Table Language Transformation (XSLT) These types of applications just in the J2EE architecture: data representation and switching function is part of the persistence service in the EJB component model, based on The message is handled by the Java Message Service (JMS) API, while the interface represents the Java Server page (JSP) and Java Servlets take advantage of the good play. In this article, we will see how the XML is applied in the previous J2EE-based applications, as well as how these applications will develop in future versions of the relevant standards. Basics: The contents of data representation and exchange of prototype XML applications are usually: Data is stored in XML format, in order to display, modify or even write an XML document, often read into an object. In the model.
As an example, it is assumed that we are working with multiple types of media (drawings, videos, text documents, etc.), and describe the metadata of these media with the following simple XML DTD: - Media assets area. Assets Are Also Hierarchical - They Can Contain Other assets. -> The following is an XML document based on the above media DTD describes the content related to a course lecture: XML Version = "1.0"?> <> < DOCTYPE Media-asset public "- // Jim Farley // DTD Media assets // en" "http://localhost/articles/sun/dtds/media.dtd">
These resources may come from a relational database system, a static XML document on a moving media server or a web server, and so on. If you want to load these data into the Java app, we can use one from the current numerous Java language XML parser. By loading XML data into a DOM document, finally traversing the document, converting all of these data to our application Object model. Below is a simple DOM-based resolution program, the above media DTD can be parsed. The parser is used by apache Xerces: package jaf.xml; import java.util. *; Import java.ioException; import org.w3c.dom. *; Import org.xml.sax. *; // XML document parser Using the above media DTD. Public class mediaparser imports errorhandler {/ ** Using Apache Xerces Resolution * / Org.Apache.XERCES.PARSERS.DOMPARSER MPARS.PARSERS.DOMPACHE.XERCES.PARSERS.DOMPARSER (); / ** Construction Function * / public mediaParser () {// tells the parser to verify and resolve the document Try {mParser.setfeature ("http://xml.org/sax/features/Validation", true);} catch (saxException e) {system .out.println ("Error Setting Validation on Parser:"); E.PrintStackTrace ();} // Setting the error handler of the parser MPARSER.SETERRORHANDLER (this);} / ** Resolution The specified URL, return to find XML document * / public document parse (String url) throws SAXException, IOException {mParser.parse (url); document mediaDoc = mParser.getDocument (); return mediaDoc;} / ** specified URL parsing XML documents, converting the contents to MediaAsset object * / public collection loadassets (String URL) throws saxception, ioException {document doc = parse (URL); Collection assets = new linkedList (); nodelist assetnodes = Doc.getElementsBytagname ("Media-asset"); for (int i = 0; i The following is an example of using MediaAsset classes: package jaf.xml; import java.util *; public class MediaAsset {// resource metadata private String mName = ""; private String mDesc = ""; private Collection mChildren = new LinkedList. (); private vector mtypes = new vector (); private string murn = ""; protected mediaasset (org.w3c.dom.node assetnode) {// For the sake of simplicity, the back code is omitted..}} Sketch The detailed code of the MediaAsset class, but the application model is still clear. The Node of the MediaAsset class traverses the document, when it comes to different child nodes, it fills your own member data with the contents of the child node. If it discovers a nested sub-resource node, it only needs to create a new MediaAsset object and then populate the data of the sub-resource node to the member data of the new object. Realize the number of methods for the above treatment. We can also use other parsers or parser architectures such as Java API for XML Parsing (JAXP). In addition to using the DOM model, event-driven SAX models can also be used to parse XMLs. Similar programs can also be used to generate XML data - premise is to generate new data objects (in this example is MediaAsset), which can insert their corresponding XML entities into the DOM, then output DOM to a stream ( Such as a file, a socket, or an HTTP connection ...). There are also other higher levels of standards that can be mapped to the Java object process (or simplified). For example, using an XML Summary (Schema) and XML Binding Processing Engine, you can transform XML data that meets an XML profile into Java data objects. Representative engines are CASTOR, which is a product of an open source project by the Exolab team. The simple example of the above Xerces Dom is merely demonstrated the underlying model of this process. The above examples show that it is very convenient to resolve or produce XML in a Java environment, which is not necessarily associated with J2EE. Formatted to XML data can flow or output from any level of the application, which makes the integration with the external system unlimited. But can we set XML data sources into a J2EE architecture in a more direct way? Driving message J2EE architecture contains access to the JMS (Java Message Service) API to implement message-oriented communication (J2EE 1.2.1) only JMS API, JMS in J2EE 1.3 is basically fixed, at this time must be A JMS API Provider that is compatible with a J2EE platform. Asynchronous interaction (relative to: local or remote method calls) is proven to be very useful in certain applications. At some point, interaction only needs to be implemented by indirect requests or answers, ie, in some cases, it is impossible to receive a reply immediately after issuing a message, but we still hope that when the message makes the message, make sure he can accept Go to the reply information. One of the actual applications of the message system is the loose integration between enterprises. Similar to document exchange in the era (electronic document exchange) era, two companies exchange messages due to the needs of the business, and this time does not interact between the two remote methods such as RPC or RMI, CORBA, DCOM. Tightly. Message systems such as JMS API allow both parties to exchange JMS API messages, provided that both parties provide compatible JMS API services when they are sessions. The current still existing is: whether the two sides can respect the same format or agreement. This is the time when XML is very show. XML is explicitly designed to solve such data exchange issues - Lower Dan is "Message-Oriented Communication Scheme), which is based on a two parties identified DTD or Schema, swaps the message load with XML format. JMS API supports several messages, which represent text messages. A simple and effective XML message exchange scheme is to insert our XML document into TextMessage at one end, then unfold the data with homemade XML resolution (such as the previous MediaParser) and convert it to Java object. This allows us to use JMS API's publicly subscribed message model, and you can also send an XML message with a point-to-point message model supported by JMS API. The above method has some limitations because the content of XML is basically opaque for JMS runtime processing. For example, the JMS API allows the use of a specific message header based route. This is easy to understand, especially when we want the XML message to take different trends according to its content. For example, in our MediaAsset example, we hope to open the seminars, but just send specific content to those who book a course, or transmitted to those who can accept certain media formats (such as video streams). In order to play the value of the JMS API to achieve the above-mentioned message-based message routing, we need to resolve critical information from the XML data, then insert this information when constructing a standard JMS API message. This is feasible, but to implement XML information, we have to write a lot of code (both parties to exchange messages). In order to make a bridge between XML and JMS API, some vendors provide custom JMS extensions to directly support the XML message mechanism. For example, the BEA system company is based on J2EE-based WebLogic application servers, providing XMLMESSAGE subclasses for TextMessage, allowing XML messages to be filtered with XPath expressions. However, this is a proprietary extension, which requires that both parties to exchange messages must handle such messages. To this end, Sun is currently developing Java API (JAXM) for XML messages. Its goal is to provide a high-level standard service to achieve the synthesis and delivery of EBXML messages. A JAXM service provider can map such messages to the appropriate physical message system (such as JMS API). Let XML see how XML integrate user interfaces with a web system is clearly a useful attempt. Most interface programs, whether it is still based on whether it is still not based on the Web, is converted to the user with the readable format. Store data with such "easy digestion", such as XML, simplifies the above work, and it also greatly improves the managementability of the content, and then we can see this. But first, a big book is that XML is based on the development of the web interface layer in the development of JSP technology. Everyone has always hoped that the representation of the Web application and the underlying object model, the JSP framework was born in these efforts (including early JHTML attempts). The JSP framework allows the Java code to embed the HTML content, so that dynamic content can be implemented, and the Java Servlets can be modified frequently. The way to include Java technology in the page is through the JSP tag (JSP Tags), which appears in an XML style. In JSP, the Java program exists in a code snippet, the server-side JavaBeans component, and triggers the contacted form of opaque tags (standard or custom) or the like in the server side. When a user requests a JSP page through a browser, a Java application server resolves the JSP page and compiles it into a Java Servlet, and then executes the servlet to generate a reply page. Methods to direct XML data sources into the JSP interface is to load XML into the JavaBeans component (like we do in the MediaAsset example) and then reference these JavaBeans components directly in JSP. Below is an example of an embedded Java code snippet: Media assets for lecture 14: h3> center>