⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxpxslt5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>Generating XML from an Arbitrary Data Structure </title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JAXPXSLT4.html" />    <link rel="Next" href="JAXPXSLT6.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPXSLT4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPXSLT6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp64712"> </a><h2 class="pHeading1">Generating XML from an Arbitrary Data Structure </h2><a name="wp64713"> </a><p class="pBody">In this section, you'll use XSLT to convert an <span style="font-style: italic">arbitrary data structure</span> to XML. </p><a name="wp64714"> </a><p class="pBody">In general outline, then:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp64715"> </a><div class="pSmartList1"><li>You'll modify an existing program that reads the data, in order to make it generate SAX events. (Whether that program is a real parser or simply a data filter of some kind is irrelevant for the moment.) </li></div><a name="wp64716"> </a><div class="pSmartList1"><li>You'll then use the SAX &quot;parser&quot; to construct a <code class="cCode">SAXSource</code> for the transformation.</li></div><a name="wp64717"> </a><div class="pSmartList1"><li>You'll use the same <code class="cCode">StreamResult</code> object you created in the last exercise, so you can see the results. (But note that you could just as easily create a <code class="cCode">DOMResult</code> object to create a DOM in memory.)</li></div><a name="wp64718"> </a><div class="pSmartList1"><li>You'll wire the source to the result, using the transformer object to make the conversion.</li></div></ol></div><a name="wp64719"> </a><p class="pBody">For starters, you need a data set you want to convert and a program capable of reading the data. In the next two sections, you'll create a simple data file and a program that reads it.</p><a name="wp64721"> </a><h3 class="pHeading2">Creating a Simple File</h3><a name="wp64722"> </a><p class="pBody">We'll start by creating a data set for an address book. You can duplicate the process, if you like, or simply make use of the data stored in <code class="cCode">PersonalAddressBook.ldif</code>.</p><a name="wp64723"> </a><p class="pBody">The file shown below was produced by creating a new address book in Netscape Messenger, giving it some dummy data (one address card) and then exporting it in LDIF format.</p><hr><a name="wp68361"> </a><p class="pNote">Note: LDIF stands for LDAP Data Interchange Format. LDAP, turn, stands for Lightweight Directory Access Protocol. I prefer to think of LDIF as the &quot;Line Delimited Interchange Format&quot;, since that is pretty much what it is.</p><hr><a name="wp68358"> </a><p class="pBody"><a  href="JAXPXSLT5.html#wp65949">Figure 7-1</a> shows the address book entry that was created.</p><a name="wp65948"> </a><p class="pBody"></p><div align="left"><img src="images/px-addressbook.gif" height="343" width="301" alt="Netscape Address Book Entry" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="65949"> </a><strong><font >Figure 7-1    Address Book Entry</font></strong></p><a name="wp65950"> </a><p class="pBody">Exporting the address book produces a file like the one shown below. The parts of the file that we care about are shown in bold.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">dn: cn=Fred Flintstone,mail=fred@barneys.housemodifytimestamp: 20010409210816Zcn: Fred Flintstone<code class="cCodeBold">xmozillanickname: Fredmail: Fred@barneys.housexmozillausehtmlmail: TRUEgivenname: Fredsn: Flintstonetelephonenumber: 999-Quarryhomephone: 999-BedrockLanefacsimiletelephonenumber: 888-Squawkpagerphone: 777-pagercellphone: 555-cell</code>xmozillaanyphone: 999-Quarryobjectclass: topobjectclass: person<a name="wp85677"> </a></pre></div><a name="wp64731"> </a><p class="pBody">Note that each line of the file contains a variable name, a colon, and a space followed by a value for the variable. The <code class="cCode">sn</code> variable contains the person's surname (last name) and the variable <code class="cCode">cn</code> contains the DisplayName field from the address book entry.</p><a name="wp64734"> </a><h3 class="pHeading2">Creating a Simple Parser</h3><a name="wp64735"> </a><p class="pBody">The next step is to create a program that parses the data. </p><hr><a name="wp64736"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/AddressBookReader01.java" target="_blank">AddressBookReader01.java</a></code>. The output is in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/AddressBookReaderLog01.txt" target="_blank">AddressBookReaderLog01.txt</a></code>.</p><hr><a name="wp64737"> </a><p class="pBody">The text for the program is shown below. It's an absurdly simple program that doesn't even loop for multiple entries because, after all, it's just a demo!</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.io.*;<a name="wp64738"> </a>public class AddressBookReader { &nbsp;&nbsp;public static void main(String argv[])&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;// Check the arguments&nbsp;&nbsp;&nbsp;&nbsp;if (argv.length != 1) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Usage: java AddressBookReader filename&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit (1);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;String filename = argv[0];&nbsp;&nbsp;&nbsp;&nbsp;File f = new File(filename);&nbsp;&nbsp;&nbsp;&nbsp;AddressBookReader01 reader = new AddressBookReader01();&nbsp;&nbsp;&nbsp;&nbsp;reader.parse(f);&nbsp;&nbsp;}&nbsp;&nbsp;/** Parse the input */&nbsp;&nbsp;public void parse(File f) &nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Get an efficient reader for the file&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileReader r = new FileReader(f);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader br = new BufferedReader(r);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Read the file and display it&#39;s contents.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (null != (line = br.readLine())) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (line.startsWith(&quot;xmozillanickname: &quot;))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;nickname&quot;, &quot;xmozillanickname&quot;, line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;email&quot;,    &quot;mail&quot;,             line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;html&quot;,     &quot;xmozillausehtmlmail&quot;, line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;firstname&quot;,&quot;givenname&quot;,        line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;lastname&quot;, &quot;sn&quot;,               line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;work&quot;,     &quot;telephonenumber&quot;,  line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;home&quot;,     &quot;homephone&quot;,        line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;fax&quot;,      &quot;facsimiletelephonenumber&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;pager&quot;,    &quot;pagerphone&quot;,       line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = br.readLine();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output(&quot;cell&quot;,     &quot;cellphone&quot;,        line);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception e) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;} &nbsp;&nbsp;void output(String name, String prefix, String line) &nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;int startIndex = prefix.length() + 2;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 2=length of &quot;: &quot;&nbsp;&nbsp;&nbsp;&nbsp;String text = line.substring(startIndex);&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(name + &quot;: &quot; + text); &nbsp;&nbsp;} }<a name="wp67720"> </a></pre></div><a name="wp64740"> </a><p class="pBody">This program contains three methods:</p><a name="wp64741"> </a><p class="pDefinitionTerm">main</p><a name="wp64742"> </a><p class="pDefinition">The <code class="cCode">main</code> method gets the name of the file from the command line, creates an instance of the parser, and sets it to work parsing the file. This method will be going away when we convert the program into a SAX parser. (That's one reason for putting the parsing code into a separate method.)</p><a name="wp64743"> </a><p class="pDefinitionTerm">parse</p><a name="wp64744"> </a><p class="pDefinition">This method operates on the <code class="cCode">File</code> object sent to it by the main routine. As you can see, it's about as simple as it can get. The only nod to efficiency is the use of a <code class="cCode">BufferedReader</code>, which can become important when you start operating on large files.</p><a name="wp64745"> </a><p class="pDefinitionTerm">output</p><a name="wp68389"> </a><p class="pDefinition">The <code class="cCode">output</code> method contains the logic for the structure of a line. Starting from the right It takes three arguments. The first argument gives the method a name to display, so we can output &quot;html&quot; as a variable name, instead of &quot;<code class="cCode">xmozillausehtmlmail</code>&quot;. The second argument gives the variable name stored in the file (<code class="cCode">xmozillausehtmlmail</code>). The third argument gives the line containing the data. The routine then strips off the variable name from the start of the line and outputs the desired name, plus the data.</p><a name="wp64747"> </a><p class="pBody">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -