📄 2_anydata.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><!--NewPage--><html><head><title>XML from Data</title><style type="text/css"><!----></style></head><body BGCOLOR="#ffffff"><table width="100%"> <tr> <td align=left> <ahref="1_read.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><a href="3_display.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=bottom border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=bottom border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=bottom border=0 alt="Top | "></a> </td> <td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a> </td> </tr></table><p> <center> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202" HEIGHT="25" NATURALSIZEFLAG="3"> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202" HEIGHT="25" NATURALSIZEFLAG="3"> </center><blockquote> <blockquote> <hr size=4> </blockquote></blockquote><p> <h2> 2. Generating XML from an Arbitrary Data Structure </h2><table width="40%" border="1" align="right"> <tr> <td> <div align="center"><b><i>Link Summary</i></b></div> </td> </tr> <tr> <td> <dl> <dt><b><i>Local Links</i></b></dt> </dl> <ul> <li><a href="../overview/3_apis.html#ProjectX">The Project X Reference Implementation</a></li> <li><a href="../sax/2a_echo.html">Echoing an XML File with the SAX Parser</a> </li> <li><a href="../sax/2b_echo.html">Adding Additional Event Handlers</a> </li> </ul> <dl> <dt><b><i>API Links</i></b></dt> </dl> <ul> <li><a href="../../api/org/xml/sax/DocumentHandler.html">DocumentHandler</a></li> <li><a href="../../api/org/xml/sax/Parser.html">org.xml.sax.Parser</a></li> <li><a href="../../api/internal/com/sun/xml/tree/XmlDocumentBuilder.html">com.sun.xml.tree.XmlDocumentBuilder</a></li> <li><a href="../../api/internal/com/sun/xml/tree/XmlDocument.html">com.sun.xml.tree.XmlDocument</a></li> <li><a href="../../api/internal/com/sun/xml/parser/SAXParserFactoryImpl.html">com.sun.xml.parser.SAXParserFactoryImpl</a></li> <li><a href="../../api/internal/com/sun/xml/parser/SAXParserImpl.html">com.sun.xml.parser.SAXParserImpl</a></li> <li><a href="../../api/javax/xml/parsers/SAXParser.html">javax.xml.parsers.SAXParser</a></li> </ul> </td> </tr></table><p>In the last section, you saw how your knowledge of SAX parsing came in handy when you were handling parser errors. In this section, you'll use that knowledge to simplify the process of converting an <i>arbitrary data structure</i> to XML. <blockquote> <p><img src="../images/sun.gif" width="62" height="29"> <b>Note:</b><br> The material in this section is specific to Project X, Sun's reference implementation for the JAXP standard. The material in this section is <i>not</i> part of the standard. Instead, it represents helpful functionality that you may need to take advantage of until some equivalent mechanism is standardized. Because it is not part of the JAXP standard, the functionality described here may very well <i>not</i> exist in other JAXP-standard parsers. In fact, as standards evolve, future versions of the JAXP reference implementation could employ different mechanisms to achieve the same goals. </p></blockquote><h3><a name="how"></a>How It Works</h3><p>Recall from <a href="../overview/3_apis.html#ProjectX">The Project X Reference Implementation</a> in the Overview part of the tutorial that Sun's reference implementation for the JAXP APIs uses a SAX parser to read in XML data when building a DOM. In this section, you'll see how to take advantage of that fact to convert an existing data set to XML.</p><p>In general outline, then, you're going to:</p><ol> <li type="a"> Modify an existing program that reads the data and modify it to generate SAX events. (Whether that is a real parser or simply a data filter of some kind is irrelevant for the moment. We'll keep calling it a "parser", in quotes, just so we're clear that it could be either one.) </li> <li>With the SAX "parser" in hand, wire it to a document builder to create a DOM.</li> <li>Use the reference implementation's <tt>write</tt> method to produce XML.</li></ol><p>For starters, of course, we have to assume that you have some program which is capable of reading the data. Assuming you have a data set that you want to convert, however, odds are good that you have some application lying around that can read it. That "parser" is the starting point. </p><blockquote> <p><b>Note:</b><br> This is an outline of the procedure you'll need to use. The code has not been tested. Any feedback you can provide will be valuable. Please send it to <a href="mailto:xml-feedback@eng.sun.com">xml-feedback@eng.sun.com</a>.</p> <p></p> <p> </blockquote><h3><a name="modify"></a>Modify The "Parser" to Generate SAX Events</h3><p>The next step is to modify the "parser" to generate SAX events. Start by extending <a href="../../api/javax/xml/parsers/SAXParser.html">javax.xml.parsers.SAXParser</a>. See <a href="../../api/internal/com/sun/xml/parser/SAXParserImpl.html">com.sun.xml.parser.SAXParserImpl</a> for an example. </p><p>Generating a SAX event means invoking one of the <a href="../../api/org/xml/sax/DocumentHandler.html">org.xml.sax.DocumentHandler</a> methods. You saw most of these methods described in <a href="../sax/2a_echo.html">Echoing an XML File with the SAX Parser</a> and <a href="../sax/2b_echo.html">Adding Additional Event Handlers</a>. Here is the minimum set of events the "parser" needs to generate for some DocumentHandler, <tt>d</tt>::</p><blockquote> <pre><new></new>d.<b>startDocument</b>()d.<b>endDocument</b>()d.<b>startElement</b>(String name, AttributeList attrs)d.<b>endElement</b>(String name)d.<b>characters</b>(char buf [], int offset, int len)</pre> <p><b>Note:</b> <br> Since each of these methods can throw a SAXException, the "parser" will have to be prepared to handle them.</p></blockquote><p>Here are the DocumentHandler events you will most likely want to ignore:</p><blockquote> <pre><new>setDocumentLocator(Locator l)</new>ignorableWhitespace(char buf [], int offset, int len)processingInstruction(String target, String data) </pre></blockquote><p>The data file won't have processing instructions, so it's easy to see why you would ignore that one. And <tt>ignorableWhitespace</tt> will generate exactly the same XML as plain old <code>characters</code> call, so that one can be ignored, too. That leaves <tt>setDocumentLocator</tt>. </p><p>The <code>setDocumentLocator</code> event is only useful for an application that is going to interpret the data in an XML file, identify a filename relative to the current location, and retrieve that file. But the events generated by your "parser" are not going to lead to any such processing -- they are going to a DocumentHandler, which will build a DOM tree using your data. Whether that data happens to name a file it wants to reference is irrelevant -- the data is not going to be interpreted, and the file is not going to be accessed, so the <tt>setDocumentLocator</tt> event is irrelevant, as well.</p><h3><a name="implement"></a>Implement the org.xml.sax.Parser Interface</h3><p>Once the "parser" can generate SAX events, it needs to be told where to send them. To do that it, it must implement the <a href="../../api/org/xml/sax/Parser.html">org.xml.sax.Parser</a> interface and, at a minimum define the <code>setDocumentHandler()</code> method with a non-null implementation.</p><p>Here is a list of the methods in that interface. You may choose to provide null implementations for many of them, or may choose to implement some, like <tt>setErrorHandler</tt>, in the interests of creating a more robust application.</p><blockquote> <pre>parse(InputSource source) parse(java.lang.String systemId) setDocumentHandler(DocumentHandler handler) setDTDHandler(DTDHandler handler) setEntityResolver(EntityResolver resolver) setErrorHandler(ErrorHandler handler) setLocale(java.util.Locale locale)</pre></blockquote><h3><a name="factory"></a>Create a Factory</h3><p>Extend SAXParserFactory and override the <tt>newSAXParser</tt> method to return an instance of your "parser". See <a href="../../api/internal/com/sun/xml/parser/SAXParserFactoryImpl.html">com.sun.xml.parser.SAXParserFactoryImpl</a> for an example.</p><h3><a name="wire"></a>Wire Your "Parser" to an XmlDocumentBuilder</h3><p>Next, use code like that shown below to wire your SAX parser to a document builder, and proceed to "parse" the data. (The highlights show the Sun-specific parts of the code.)</p><blockquote> <p></p> <pre>import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.SAXParser;<br>import org.xml.sax.Parser;import com.sun.xml.parser.Resolver;<br>import javax.xml.parsers.DocumentBuilder;import <b>com.sun.xml.tree.XmlDocumentBuilder</b>;import <b>com.sun.xml.tree.XmlDocument</b>;<br>SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();Parser parser = saxParser.getParser();builder = <b>new XmlDocumentBuilder()</b>;<i>builder.setIgnoringLexicalInfo(true); // Skip comments, entity refs, etc.</i>parser.setDocumentHandler(builder); parser.parse(Resolver.createInputSource(new File(argv[0])));<b>XmlDocument</b> document = builder.getDocument();</pre></blockquote><p>In this code, you obtain an instance of your SAX parser factory, use that to get your "parser", and then create an instance of the reference implementation's XmlDocumentBuilder. That class implements the DocumentHandler interface, which lets you connect it to your "parser". (The italicized line is optional. It customizes the XmlDocumentBuilder so that it creates a simplified DOM that doesn't have comments and which has the text of entities included inline, rather than having entity-reference nodes. For more information, see the <a href="../../api/internal/com/sun/xml/tree/XmlDocumentBuilder.html">XmlDocumentBuilder</a> APIs.)</p><p>You then invoke the "parser's" parse method, assuming you implemented that, or whatever method fires it off. As it parses the data, it generates SAX events. The XmlDocumentBuilder reacts to those events and builds a DOM in the process. You retrieve that DOM with the <tt>getDocument</tt> method, specifying the class name (XmlDocument) rather than the general interface (Document) so you can use XmlDocument's output method.</p><blockquote><p> </blockquote><h3><a name="write"></a>Write It Out</h3><p>As the last step in your program, you write out the DOM as an XML document using the <a href="../../api/internal/com/sun/xml/tree/XmlDocument.html">XmlDocument</a> <code>write</code> method you learned about in the last section. </p><h3><a name="run"></a>Run It</h3><p>Finally, specify the full path to your parser factory on the command line as a system property, using the -D flag, like this:</p><blockquote> <pre>-Djavax.xml.parsers.SAXParserFactory=<i>fully.qualified.name.of.parserFactory</i></pre></blockquote><p>Now run the app. Congratulations! You have now successfully converted an existing data structure to XML with a bare minimum of effort. Well, ok. It was a lot of effort. But you did it! (And it was a lot easier than it could have been.) Congratulations!</p><blockquote> <hr size=4></blockquote><p> <p> <table width="100%"> <tr> <td align=left> <ahref="1_read.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><a href="3_display.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top border=0 alt="Top | "></a> </td> <td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../TOC.html#intro"><strong><em></em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a></td> </tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -