• Aucun résultat trouvé

An XPath Example

Dans le document Beginning JSP, JSF and Tomcat (Page 155-158)

So far, everything has been pretty dry and abstract. To spice things up a bit, we are going to write a JSP page that parses an XML file, selects its elements and attributes, and displays them in a HTML table.

Listing 5-9 shows the XML file we’ll play with, starfleet.xml. It is an expanded version of the file enterprises.xml (Listing 5-1) you have already encountered in the validation section of this chapter.

Listing 5-9. starfleet.xml

<?xml version="1.0" encoding="UTF-8"?>

<starfleet>

<starship name="Enterprise" sn="NX-01">

<class commissioned="2151">NX</class>

<captain>Jonathan Archer</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701">

<class commissioned="2245">Constitution</class>

<captain>James Tiberius Kirk</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701-A">

<class commissioned="2286">Constitution</class>

<captain>James T. Kirk</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701-B">

<class commissioned="2293">Excelsior</class>

<captain>John Harriman</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701-C">

<class commissioned="2332">Ambassador</class>

<captain>Rachel Garrett</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701-D">

<class commissioned="2363">Galaxy</class>

<captain>Jean-Luc Picard</captain>

</starship>

<starship name="USS Enterprise" sn="NCC-1701-E">

<class commissioned="2372">Sovereign</class>

<captain>Jean-Luc Picard</captain>

</starship>

</starfleet>

Notice that it doesn’t include the DOCTYPE element necessary for DTD validation or the namespace declarations necessary for schema validation. This is because in this example we are not going to do any validation. Listing 5-10 shows the JSP page that does the conversion to HTML, and Figure 5-3 shows its output as it appears in a web browser.

Listing 5-10. starfleet.jsp

01: <%@page language="java" contentType="text/html"%>

02: <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

03: <%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

04: <c:import url="starfleet.xml" var="sf"/>

05: <x:parse doc="${sf}" varDom="dom"/>

06: <html><head>

07: <title>Parsing starfleet.xml</title>

08: <style>th {text-align:left}</style>

09: </head>

10: <body>

11: <table border="1">

12: <tr><th>Name</th><th>S/N</th><th>Class</th><th>Year</th><th>Captain</th></tr>

13: <x:forEach var="tag" select="$dom//starship">

14: <tr>

15: <td><x:out select="$tag/@name"/></td>

16: <td><x:out select="$tag/@sn"/></td>

17: <td><x:out select="$tag/class"/></td>

18: <td><x:out select="$tag/class/@commissioned"/></td>

19: <td><x:out select="$tag/captain"/></td>

20: </tr>

21: </x:forEach>

22: </table>

23: </body>

24: </html>

In line 4, you load the XML file in memory, and in line 5, you parse it into an object of type org.apache.xerces.dom.DeferredDocumentImpl, which implements the standard interface

org.w3c.dom.Document of a Document Object Model (DOM). In lines 13–21, you loop through all the starship tags of the DOM, regardless of how “deep” they are in the structure. You can achieve this with the double slash. Inside the x:forEach loop, the variable tag refers in turn to each starship, and you can display the information contained in attributes and sub-elements. Notice that the select paths inside the loop always start with the slash. This is because the root element in each loop iteration is a starship tag, not starfleet, which is the root element of the document.

Figure 5-3. Starfleet information

x:parse

With starfleet.jsp, you have just seen an example of how to use x:parse and XPath to convert XML into HTML. Table 5-5 summarizes all attributes that x:parse supports. You will find a good reference for x:parse at http://www.ibm.com/developerworks/java/library/j-jstl0520/.

Table 5-5. x:parse Attributes

Attribute Description

doc Source XML document to be parsed

varDom Name of the EL variable to store the parsed XML data as an object of type org.w3c.dom.Document

scopeDom Scope for varDom

filter Filter of type org.xml.sax.XMLFilter to be applied to the source XML systemId System identifier for parsing the XML source. It is a URI that identify

the origin of the XML data, potentially useful to some parsers var Name of the variable to store the parse XML data (of

implementation-dependent type)

scope Scope for var

Instead of storing the XML source code in the attribute doc, you can also make x:parse a bodied action and store the source XML in its body.

Dans le document Beginning JSP, JSF and Tomcat (Page 155-158)