1. Trang chủ
  2. » Công Nghệ Thông Tin

WebSphere Studio Application Developer Version 5 Programming Guide part 38 doc

10 155 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

344 WebSphere Studio Application Developer Version 5 Programming Guide One thing that differs, however, is the response you get if you do not enter the requested data. For example, if you select to transfer money between two accounts, but you do not enter either the amount or the destination account number, the validate method of the transactionForm form bean will report these two errors and you will be returned to the accountDetails.jsp page to correct the problem (Figure 10-36). Figure 10-36 Sample run of the Struts application with errors Summary In this chapter we built a simple Web application using the Struts framework. We reused the model from the Web application we built in Chapter 7, “Developing Web applications” on page 179, but replaced the controller and view parts with the Struts framework. We tried to use Application Developer’s Struts tools and wizards as much as possible to show you what they can do. © Copyright IBM Corp. 2003. All rights reserved. 345 Chapter 11. Developing XML applications This chapter introduces you to Application Developer’s XML capabilities and discusses how to work with some of these XML features. Note that some parts of this chapter have been taken from an existing IBM Redbook: The XML Files: Development of XML/XSL Applications Using WebSphere Studio Version 5 , SG24-6586. This book provides more detailed information and examples of developing XML/XSL applications using Application Developer. This chapter describes the following topics:  XML overview and technologies  Application Developer XML tools 11 346 WebSphere Studio Application Developer Version 5 Programming Guide XML overview and technologies Today, the extensible markup language (XML) is starting to become a key piece of software infrastructure. The main idea is extremely simple. It is a text based language like HTML, but is rigidly enforced, and therefore, can be built upon easily. XML was designed to describe data and to focus on the data, unlike HTML which was designed to display data. XML was created to structure and store data. XML and XML processor XML is tag-based, but XML tags are not predefined in XML. You have to define your own tags. XML uses a document type definition (DTD) or an XML schema to describe the data. XML documents follow strict syntax rules. For more information regarding XML consult the W3C Web Site: http://www.w3.org/XML See the following Web Site for XML syntax rules: http://www.w3.org/TR/rdf-syntax-grammar/ However, to create, read and update XML documents, you need an XML processor or parser. At the heart of every XML application is an XML processor that parses an XML document, so that the document elements can be retrieved and transformed into a presentation understood by the target client. The other responsibility of the parser is to check the syntax and structure of the XML document. There are a number of validating or non-validating parsers. The following list shows some of the most common processors and parsers:  Xerces (XML parsers)  Xalan (XSLT style sheet processors)  IBM XML Parser for Java (XML4J)  SAX2  JAXP DTD and XML schema DTDs and XML schemas are both used to describe structured information, however, in the last two years the acceptance of XML schemas has gained momentum. Both DTDs and schemas are building blocks for XML documents and consist of elements, tags, attributes, and entities. Chapter 11. Developing XML applications 347 XML schema is more powerful than DTD. Here are some advantages of XML schemas over DTDs:  They define data types for elements and attributes, and their default and fixed values. Some of the data types can be of string, decimal, integer, boolean, date, time, or duration.  They apply restrictions to elements, by stating minimum and maximum values, (for example, on age from 1 to 90 years), or restrictions of certain values (for example, redbooks, residencies, redpieces with no other values accepted, such as in a drop-down list box). Restrictions can also be applied to types of characters and their patterns (for example, only accepting values ‘a’ to ‘z’ and also specifying that only three letters can be accepted). The length of the data can also be specified (for example passwords must be between 4 They and 8 characters.)  They specify complex element types. Complex types may contain simple elements and other complex types. Restrictions can be applied to the sequence and the frequency of their occurrences. These complex types can then be used in other complex type elements.  Since schemas are written in XML, they are also extensible. This also implies that the learning curve for learning another language has been eliminated, the available parsers need not be enhanced, transformation can be carried out using XSLT, and also, its manipulation can be carried out using XML DOM.  With XML schemas being extensible, they can be re-used in other schemas, we can reference multiple schemas from the same document, and we also have the ability to create our own data types from standard data types. Schema and style using XSLT and XSL The extensible style language (XSL) is a language defined by the W3C for expressing style sheets. It has three parts:  XSL transformations (XSLT), which is used for transforming XML documents.  the XML path language (XPath), which is a language used to access or refer to parts of an XML document.  XSL-FO, which is a vocabulary for specifying formatting semantics. A transformation in XSLT must be a well-formed document and must conform to the namespaces in XML, which can contain elements that may or may not be Note: DTDs consists of elements such as text strings, text strings with other child elements, and a set of child elements. DTDs offer limited support for types and namespaces, and the syntax in DTDs is not XML. 348 WebSphere Studio Application Developer Version 5 Programming Guide defined by XSLT. XSLT-defined elements belong to a specific XML namespace. A transformation in XSLT is called a style sheet. XSL uses an XML notation and works on two principles: pattern matching and templates. It operates on an XML source document and parses it into a source tree, it applies the transformation of the source tree to a result tree, and then it outputs the result tree to a specified format. In constructing the result tree, the elements can be reordered or filtered, and also, other structures can be added. The result tree can be completely different from the source tree. XML namespaces Namespaces are used when there is a need for elements and attributes of the same name to take on a different meaning depending on the context in which they are used. For instance, a tag called <TITLE> takes on a different meaning, depending on whether it is applied to a person or a book. If both entities (a person and a book) need to be defined in the same document, for example, in a library entry which associates a book with its author, we need some mechanism to distinguish between the two and apply the correct semantic description to the <TITLE> tag whenever it is used in the document. Namespaces provide the mechanism that allows us to write XML documents which contain information relevant to many software modules. XPath The XML path language (XPath) is used to address parts of an XML document. An XPath expression can be used to search through an XML document, and extract information from the nodes (any part of the document, such as an element or attribute) in it. There are four different kinds of XPath expressions:  Boolean—Expression type with two possible values  Node set—Collection of nodes that match an expression's criteria, usually derived with a location path  Number—Numeric value, useful for counting nodes and for performing simple arithmetic  String—Text fragment that may come from the input tree, processed or augmented with general text An XPath expression returns a certain node set, which is a collection of nodes. The following is a sample XPath expression: /ACCOUNT_TABLE/ACCOUNT/ACCID Chapter 11. Developing XML applications 349 This expression selects any elements named ACCID (account ID), which are children of ACCOUNT elements, which are children of ACCOUNT_TABLE elements, which are children of the document root. Application Developer XML tools Application Developer provides a comprehensive visual XML development environment. The tool set includes components for building DTDs, XML schemas, XML, and XSL files. The XML development tools of Application Developer are:  XML editor  XML schema editor  XSL editor  DTD editor  XPath expression wizard  XSL debugging and transformation  XML to XML mapping editor  XML and relational data  Relational database to XML mapping (visual DAD builder) Only some of these editors are described in these chapter. Refer to Application Developer’s help manual for more detailed information regarding XML tools and editors. Tools demonstrated in this chapter This chapter covers only a few XML tools of Application Developer. We demonstrate how to create XML files, and we introduce you to some of Application Developers XML generators. The following topics are discussed:  Creating a new XML file from scratch  Creating an XML schema  Generating a DTD from XSD  Generating an XML file from an XML schema  Creating an XSL file  Transforming XML files  Debugging XSL  Generating JavaBeans from DTD or XSD Note: We do not create a fully XML enabled application here. This chapter only shows some XML capabilities of Application Developer where we create and work with XML files. 350 WebSphere Studio Application Developer Version 5 Programming Guide Setting up your sample project To demonstrate some of Application Developer’s XML tools, we set up a new project. Therefore, we created a new Web project called ItsoProGuideXmlWeb, part of the ItsoProGuide enterprise application. You can consult Chapter 7, “Developing Web applications” on page 179 to learn how to create a new Web project. Once this is done, we create a Java package and name it itso.xml (see “Creating Java packages” on page 99 for more information). We also create a new folder in the WEB-INF folder and name the folder xml where we store an XML schema file. The new Web project skeleton displayed in the J2EE Navigator view should now look like shown in Figure 11-1. Figure 11-1 Sample project skeleton Creating a new XML file from scratch In this section we create a new XML file from scratch to demonstrate the capabilities of the XML editor. It can be used to edit the contents of new XML files, either created from scratch or from existing DTDs, or XML schemas. You can also use it to edit XML files, associate them with DTDs or schemas, and validate them. To create a new file from scratch, use the New XML wizard. Switch to the XML perspective, select File -> New -> XML and select the Create XML file from scratch option from the dialog and click Next (Figure 11-2). Chapter 11. Developing XML applications 351 Figure 11-2 Create new XML File dialog In the next dialog, make sure that the project ItsoProGuideXmlWeb with the folder Web Content\xml has been selected, type a name in the File name field (in this example we use the default name NewFile.xml) and click Finish . The XML file is created and the XML editor is automatically opened. Refer to “XML perspective” on page 67 for a description of the views, including the XML editor. The XML editor has two main tabs, Design and Source (Figure 11-3): Figure 11-3 XML editor views You can use the Source view to view and work with a file’s source code directly. The Source view has many text editing features, such as:  Syntax highlighting  Unlimited undo and redo of changes  Content assist  User-defined macros  Node selection indicator 352 WebSphere Studio Application Developer Version 5 Programming Guide The Design view displays the XML document as a tree, enabling you to manipulate the document by adding, removing, and editing tree nodes. This also makes navigation easier. In the left column of the Design view, we see the elements, attributes, and other nodes of the XML document's tree. The right column is used to display the values associated with these nodes. After you have created the new XML file, add some elements with values to the XML file. Figure 11-4 shows the code of the file including the root element named ACCOUNT and seven child elements. Figure 11-4 Sample XML code To add an element to an XML file, you can do any of these things:  Add the element by typing it into the source editor  Open the context menu from the document type declaration tag in the Design view and select Add After -> New Element  Open the context menu from the document type declaration tag in the Outline view and select Add After -> New Element Figure 11-5 shows the context menu of the document type declaration tag. Figure 11-5 Add New Element to the XML file <?xml version="1.0" encoding="UTF-8"?> <ACCOUNT> <ACCID>101-1001</ACCID> <BALANCE>80.00</BALANCE> <INTEREST>4</INTEREST> <ACCTYPE>CHECKING</ACCTYPE> <DISCRIMINATOR>C</DISCRIMINATOR> <OVERDRAFT>200.00</OVERDRAFT> <MINAMOUNT></MINAMOUNT> </ACCOUNT> Chapter 11. Developing XML applications 353 A new dialog box comes up and you have to enter an element name. In this example we enter ACCOUNT as our root element for this XML file (Figure 11-6). Figure 11-6 New Element dialog To add elements to the ACCOUNT element, select the element and Add Child -> New Element . Add the seven child elements to the ACCOUNT element. Once you have added all elements to the XML file, you can use the Design view to add its values. Figure 11-7 shows the Design view with the highlighted ACCID element. You can see that a value for this element has been entered in the second column. Figure 11-7 XML Design view for sample file Creating an XML schema In this section we create a new XML schema file based on the ACCOUNT table of the sample DB2 database. See also “Generate DDL and XML schema files” on page 151 for more information about creating XML schema files for data objects. . enabled application here. This chapter only shows some XML capabilities of Application Developer where we create and work with XML files. 350 WebSphere Studio Application Developer Version 5 Programming. selection indicator 352 WebSphere Studio Application Developer Version 5 Programming Guide The Design view displays the XML document as a tree, enabling you to manipulate the document by adding,. following topics:  XML overview and technologies  Application Developer XML tools 11 346 WebSphere Studio Application Developer Version 5 Programming Guide XML overview and technologies Today, the

Ngày đăng: 03/07/2014, 20:20

Xem thêm: WebSphere Studio Application Developer Version 5 Programming Guide part 38 doc