📄 6_val.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><TITLE>Validating Parser</TITLE><STYLE TYPE=text/css></STYLE></HEAD><BODY BGCOLOR=#ffffff><HR><B>HtmlDiff: </B>Here is the <A href=#DIFF0>first difference.</A> There are 15 differences. <IMG src=../diffpics/oold.gif><STRIKE>is old.</STRIKE><IMG src=../diffpics/onew.gif><STRONG><I> is new. </I></STRONG><HR><TABLE WIDTH=100%><TR> <TD ALIGN=left> <A HREF=5d_dtd.html><IMG SRC=../images/PreviousArrow.gif WIDTH=26 HEIGHT=26 ALIGN=top BORDER=0 ALT="Previous | "></A><A HREF=7a_pe.html><IMG SRC=../images/NextArrow.gif WIDTH=26 HEIGHT=26 ALIGN=top 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><IMG SRC=../images/xml_TOC.gif WIDTH=26 HEIGHT=26 ALIGN=top BORDER=0 ALT="TOC | "></A><A HREF=../index.html><IMG SRC=../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=../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><H2><A name=DIFF0></A><A href=#DIFF1><IMG src=../diffpics/onew.gif></A><STRONG><I>6. </STRONG></I>Using the Validating Parser</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>Exercise Links</I></B></DT> </DL> <UL> <LI><A HREF=work/Echo10.java>Echo10.java</A></LI> <LI><A HREF=work/Echo10-01.log>Echo10-01.log</A></LI> <LI><A name=DIFF1></A><A href=#DIFF2><IMG src=../diffpics/onew.gif></A><STRONG><I><A HREF=work/Echo10-06.log>Echo10-06.log</A></STRONG></I></LI> <LI><A HREF=work/Echo10-07.log>Echo10-07.log</A></LI> </UL> <DL> <P><A name=DIFF2></A><A href=#DIFF3><IMG src=../diffpics/onew.gif></A><STRONG><I><B><I>API References</I></B></P> <UL> <LI><A HREF=../../api/javax/xml/parsers/SAXParserFactory.html>javax.xml.parsers.SAXParserFactory</A></STRONG></I></LI> </UL> <P><B><I></I></B></P> <DT><B><I>Glossary Terms</I></B></DT> </DL> <DL> <DD><A HREF=../glossary.html#DTD>DTD</A>, <A HREF=../glossary.html#error>error</A>, <A HREF=../glossary.html#XHTML>XHTML</A></DD> </DL> </TD> </TR></TABLE><P>By now, you have done a lot of experimenting with the nonvalidating parser. It's time to have a look at the validating parser and find out what happens when you use it to parse the sample presentation. <P>Two things to understand about the validating parser at the outset are: <OL> <LI TYPE=a>The <A HREF=../glossary.html#DTD>DTD</A> is required.</LI> <LI>Since the DTD is present, the <CODE>ignorableWhitespace</CODE> method is invoked whenever the DTD makes that possible.</LI></OL><H3><A name=DIFF3></A><A href=#DIFF4><IMG src=../diffpics/oold.gif></A><STRIKE>Changing Parsers </STRIKE><A name=DIFF3></A><A href=#DIFF4><IMG src=../diffpics/onew.gif></A><STRONG><I><A NAME=changing></A>Configuring the Factory</STRONG></I></H3><P>The first step is modify the Echo program so that it uses the validating parser instead of the nonvalidating parser.</P><BLOCKQUOTE> <BLOCKQUOTE> <P><B>Note:</B><BR> The code in this section is contained in <A HREF=work/Echo10.java><CODE>Echo10.java</CODE></A>.</P> </BLOCKQUOTE></BLOCKQUOTE><P>To use the validating parser, make the changes highlighted below:</P><BLOCKQUOTE> <PRE><A name=DIFF4></A><A href=#DIFF5><IMG src=../diffpics/oold.gif></A><STRIKE>// Get an instance of the non- validating parser. Parser parser; parser = ParserFactory.makeParser ( "com.sun.xml.parser. </STRIKE><A name=DIFF4></A><A href=#DIFF5><IMG src=../diffpics/onew.gif></A><STRONG><I>public static void main (String argv []){ if (argv.length != 1) { ... }<BR> <OLD><STRIKE>// Use the default (non-validating) parser</STRIKE></STRONG></I></OLD> <NEW><A name=DIFF5></A><A href=#DIFF6><IMG src=../diffpics/oold.gif></A><STRIKE>Validating </STRIKE><A name=DIFF5></A><A href=#DIFF6><IMG src=../diffpics/onew.gif></A><STRONG><I><B>// Use the validating parser</B></STRONG></I></NEW> <A name=DIFF6></A><A href=#DIFF7><IMG src=../diffpics/oold.gif></A><STRIKE>Parser" ); parser.setDocumentHandler ( new Echo() ); </STRIKE><A name=DIFF6></A><A href=#DIFF7><IMG src=../diffpics/onew.gif></A><STRONG><I>SAXParserFactory factory = SAXParserFactory.newInstance(); <NEW><B>factory.setValidating(true);</B></NEW> try { ...</STRONG></I></PRE></BLOCKQUOTE><A name=DIFF7></A><A href=#DIFF8><IMG src=../diffpics/onew.gif></A><STRONG><I>Here, you configured the factory so that it will produce a validating parser when <TT>newSAXParser</TT> is invoked. You can also configure it to return a namespace-aware parser using <TT>setNamespaceAware(true)</TT>. The reference implementation supports any combination of configuration options. If the combination of . </STRONG></I><H3><A name=DIFF8></A><A href=#DIFF9><IMG src=../diffpics/oold.gif></A><STRIKE>Using an Environment Variable </STRIKE><A name=DIFF8></A><A href=#DIFF9><IMG src=../diffpics/onew.gif></A><STRONG><I><A NAME=envVar></A>Changing the Environment Variable</STRONG></I></H3><P><A name=DIFF9></A><A href=#DIFF10><IMG src=../diffpics/oold.gif></A><STRIKE>You can also set things up so that you can change the parser at will, without having it hard coded into the program. The sample programs use this technique, which depends on the fact that the zero-argument method ParserFactory.makeParser() uses the environment variable com.sun.xml.parser , if it is defined. You can specify the value for the environment variable on the command line, like this: </STRIKE><A name=DIFF9></A><A href=#DIFF10><IMG src=../diffpics/onew.gif></A><STRONG><I>If no other factory class is specified, the default <CODE>SAXParserFactory</CODE> class is used. To use a different manufacturer's parser, you can change the value of the environment variable that points to it. You can do that from the command line, like this:</STRONG></I></P><BLOCKQUOTE> <PRE>> <A name=DIFF10></A><A href=#DIFF11><IMG src=../diffpics/oold.gif></A><STRIKE>java -Dorg.xml.sax.parser=com.sun.xml.parser.ValidatingParser ... </STRIKE><A name=DIFF10></A><A href=#DIFF11><IMG src=../diffpics/onew.gif></A><STRONG><I>java <B>-Djavax.xml.parsers.SAXParserFactory=<I>yourFactoryHere</I></B> ...</STRONG></I></PRE></BLOCKQUOTE><P><A name=DIFF11></A><A href=#DIFF12><IMG src=../diffpics/onew.gif></A><STRONG><I>The factory name you specify must be a fully qualified class name (all package prefixes included). For more information, see the documentation for the <A HREF=../../api/javax/xml/parsers/SAXParserFactory.html>SAXParserFactory</A> class. </STRONG></I></P><H3><A NAME=experimenting></A>Experimenting with Validation Errors</H3><P>To see what happens when the XML document does not specify a DTD, remove the <CODE>DOCTYPE</CODE> statement from the XML file and run the Echo program on it.</P><BLOCKQUOTE> <P><B>Note:</B><BR> The output shown here is contained in <A HREF=work/Echo10-01.log><CODE>Echo10-01.log</CODE></A>.</P></BLOCKQUOTE><P>The result you see looks like this:</P><BLOCKQUOTE> <PRE><?xml version='1.0' encoding='UTF-8'?><BR><B>** Warning, line 5, uri <A name=DIFF12></A><A href=#DIFF13><IMG src=../diffpics/oold.gif></A><STRIKE>file:... </STRIKE><A name=DIFF12></A><A href=#DIFF13><IMG src=../diffpics/onew.gif></A><STRONG><I>file: ... </STRONG></I>Valid documents must have a <!DOCTYPE declaration.** Parsing error, line 5, uri file: ... <A name=DIFF13></A><A href=#DIFF14><IMG src=../diffpics/onew.gif></A><STRONG><I>Element type "slideshow" is not declared.</STRONG></I></B></PRE></BLOCKQUOTE><P>So now you know that a DTD is a requirement for a valid document. That makes sense. <A name=DIFF14></A><A href=#DIFF0><IMG src=../diffpics/onew.gif></A><STRONG><I>(Note, though, that the lack of a type declaration only generates a warning, as specified in the standard. On the other hand, any attempt to actually parse the document is immediately greeted with an error! Oh well...)</STRONG></I></P><P>So what happens when you run the parser on your current version of the slide presentation, with the DTD specified?</P><BLOCKQUOTE> <P><B>Note:</B><BR> The output shown here is contained in <A HREF=work/Echo10-07.log><CODE>Echo10-07.log</CODE></A>.</P></BLOCKQUOTE><P>This time, the parser gives the following error message:</P><BLOCKQUOTE> <PRE>** Parsing error, line 28, uri file:... Element "slide" does not allow "item" here.</PRE></BLOCKQUOTE><P>This error occurs because the definition of the <CODE>slide</CODE> element requires a <CODE>title</CODE>. That element is not optional, and the copyright slide does not have one. To fix the problem, add the question mark highlighted below to make <CODE>title</CODE> an optional element:</P><BLOCKQUOTE> <PRE><!ELEMENT slide (image?, title<B>?</B>, item*)></PRE></BLOCKQUOTE><P>Now what happens when you run the program?</P><BLOCKQUOTE> <BLOCKQUOTE> <P></P> </BLOCKQUOTE> <P></P> <P><B>Note:</B><BR> You could also remove the copyright slide, which produces the same result shown below, as reflected in <A HREF=work/Echo10-06.log>Echo10-06.log</A>.</P> <P></P></BLOCKQUOTE><P>The answer is that everything runs fine, until the parser runs into the <CODE><em></CODE> tag contained in the overview slide. Since that tag was not defined in the DTD, the attempt to validate the document fails. The output looks like this:</P><BLOCKQUOTE> <PRE>... ELEMENT: <title> CHARS: Overview END_ELM: </title> ELEMENT: <item> CHARS: Why <B>** Parsing error, line 24</B>, uri file:...<B>Element "item" does not allow "em" -- (#PCDATA|item)</B>org.xml.sax.SAXParseException: Element "item" does not allow "em" -- (#PCDATA|item) at com.sun.xml.parser.Parser.error(Parser.java:2798)...</PRE></BLOCKQUOTE><P>The error message identifies the part of the DTD that caused validation to fail. In this case it is the line that defines an <CODE>item</CODE> element as <CODE>(#PCDATA | item)</CODE>. </P><BLOCKQUOTE> <P><B>Exercise: </B>Make a copy of the file and remove all occurrences of <CODE><em></CODE> from it. Can the file be validated now? (In the next section, you'll learn how to define parameter entries so that we can use <A HREF=../glossary.html#XHTML>XHTML</A> in the elements we are defining as part of the slide presentation.)</P></BLOCKQUOTE><H3><A NAME=errorHandling></A>Error Handling in the Validating Parser</H3><P>It is important to recognize that the only reason an exception is thrown when the file fails validation is as a result of the error-handling code you entered in the early stages of this tutorial. That code is reproduced below:</P><BLOCKQUOTE> <PRE>static class MyErrorHandler extends HandlerBase{<B> public void error (SAXParseException e) throws SAXParseException { throw e; }</B> ...} </PRE></BLOCKQUOTE><P>If that exception is not thrown, the validation errors are simply ignored.</P><BLOCKQUOTE> <P><B>Exercise: </B>Try commenting out the line that throws the exception. What happens when you run the parser now?</P></BLOCKQUOTE><P>In general, a SAX parsing <I>error</I> is a validation error, although we have seen that it can also be generated if the file specifies a version of XML that the parser is not prepared to handle. The thing to remember is that your application will not generate a validation exception unless you supply an error handler like the one above.</P><BLOCKQUOTE> <HR SIZE=4></BLOCKQUOTE><P><P> <TABLE WIDTH=100%><TR> <TD ALIGN=left> <A HREF=5d_dtd.html><IMG SRC=../images/PreviousArrow.gif WIDTH=26 HEIGHT=26 ALIGN=top BORDER=0 ALT="Previous | "></A><A HREF=7a_pe.html><IMG SRC=../images/NextArrow.gif WIDTH=26 HEIGHT=26 ALIGN=top 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><IMG SRC=../images/xml_TOC.gif WIDTH=26 HEIGHT=26 ALIGN=top BORDER=0 ALT="TOC | "></A><A HREF=../index.html><IMG SRC=../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=../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 + -