sandygao 2002/11/21 09:32:57 Modified: java/docs faq-dom.xml faq-grammars.xml faq-write.xml faq-xs.xml Log: Fixing some indention problem in the sample source code. Revision Changes Path 1.4 +96 -109 xml-xerces/java/docs/faq-dom.xml Index: faq-dom.xml =================================================================== RCS file: /home/cvs/xml-xerces/java/docs/faq-dom.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- faq-dom.xml 8 Nov 2002 23:57:02 -0000 1.3 +++ faq-dom.xml 21 Nov 2002 17:32:56 -0000 1.4 @@ -67,60 +67,57 @@ <p> The following source code shows how to create the parser with JAXP: </p> - <source> - import java.io.IOException; - import javax.xml.parsers.DocumentBuilder; - import javax.xml.parsers.DocumentBuilderFactory; - import javax.xml.parsers.FactoryConfigurationError; - import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.Document; - import org.xml.sax.SAXException; - - ... - - String xmlFile = "file:///&parserdir;/data/personal.xml"; - try { - DocumentBuilderFactory factory = - DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document document = builder.parse(xmlFile); - } - catch (FactoryConfigurationError e) { - // unable to get a document builder factory - } - catch (ParserConfigurationException e) { - // parser was unable to be configured - catch (SAXException e) { - // parsing error - } - catch (IOException e) { - // i/o error - }</source> - <anchor name="domparser"/> + <source>import java.io.IOException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +... + +String xmlFile = "file:///&parserdir;/data/personal.xml"; +try { + DocumentBuilderFactory factory = + DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(xmlFile); +} +catch (FactoryConfigurationError e) { + // unable to get a document builder factory +} +catch (ParserConfigurationException e) { + // parser was unable to be configured +catch (SAXException e) { + // parsing error +} +catch (IOException e) { + // i/o error +}</source> + <anchor name="domparser"/> <p> The following source code shows how to create the parser using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump>: </p> - <source> - import org.w3c.dom.DOMImplementationRegistry; - import org.w3c.dom.Document; - import org.w3c.dom.ls.DOMImplementationLS; - import org.w3c.dom.ls.DOMBuilder; - - ... - - System.setProperty(DOMImplementationRegistry.PROPERTY, - "org.apache.xerces.dom.DOMImplementationSourceImpl"); - DOMImplementationRegistry registry = - DOMImplementationRegistry.newInstance(); + <source>import org.w3c.dom.DOMImplementationRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.DOMBuilder; + +... + +System.setProperty(DOMImplementationRegistry.PROPERTY, + "org.apache.xerces.dom.DOMImplementationSourceImpl"); +DOMImplementationRegistry registry = + DOMImplementationRegistry.newInstance(); - DOMImplementationLS impl = - (DOMImplementationLS)registry.getDOMImplementation("LS-Load"); +DOMImplementationLS impl = + (DOMImplementationLS)registry.getDOMImplementation("LS-Load"); - DOMBuilder builder = impl.createDOMBuilder( - DOMImplementationLS.MODE_SYNCHRONOUS, null); +DOMBuilder builder = impl.createDOMBuilder( + DOMImplementationLS.MODE_SYNCHRONOUS, null); - Document document = builder.parseURI("data/personal.xml"); - </source> +Document document = builder.parseURI("data/personal.xml");</source> <note>You can use DOM Level 3 Load/Save interfaces with the default Xerces distribution. To access the DOM Level 3 Core functionality you need to extract the code from CVS and build Xerces with the <strong>jars-dom3</strong> target.</note> @@ -133,47 +130,43 @@ <a> <p> You can serialize a DOM tree by using Xerces <code>org.apache.xml.XMLSerializer</code>: </p> - <source> - import org.apache.xml.serialize.OutputFormat; - import org.apache.xml.serialize.XMLSerializer; - import org.apache.xml.serialize.LineSeparator; - - ... - - OutputFormat format = new OutputFormat((Document)core); - format.setLineSeparator(LineSeparator.Windows); - format.setIndenting(true); - format.setLineWidth(0); - format.setPreserveSpace(true); - XMLSerializer serializer = new XMLSerializer ( - new FileWriter("output.xml"), format); - serializer.asDOMSerializer(); - serializer.serialize(document); - </source> + <source>import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; +import org.apache.xml.serialize.LineSeparator; + +... + +OutputFormat format = new OutputFormat((Document)core); +format.setLineSeparator(LineSeparator.Windows); +format.setIndenting(true); +format.setLineWidth(0); +format.setPreserveSpace(true); +XMLSerializer serializer = new XMLSerializer ( + new FileWriter("output.xml"), format); +serializer.asDOMSerializer(); +serializer.serialize(document);</source> <p>You can also serialize a DOM tree by using the DOM Level 3 Load and Save. <code>DOMWriter</code> performs automatic namespace fixup to make your document namespace well-formed. </p> - <source> - import org.w3c.dom.DOMImplementationRegistry; - import org.w3c.dom.Document; - import org.w3c.dom.ls.DOMImplementationLS; - import org.w3c.dom.ls.DOMWriter; - - ... - - System.setProperty(DOMImplementationRegistry.PROPERTY, - "org.apache.xerces.dom.DOMImplementationSourceImpl"); - DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); - - DOMImplementationLS impl = - (DOMImplementationLS)registry.getDOMImplementation("LS-Load"); - - ... - - DOMWriter builder = impl.createDOMWriter(); - writer.writeNode(System.out, document); - </source> + <source>import org.w3c.dom.DOMImplementationRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.DOMWriter; + +... + +System.setProperty(DOMImplementationRegistry.PROPERTY, + "org.apache.xerces.dom.DOMImplementationSourceImpl"); +DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); + +DOMImplementationLS impl = + (DOMImplementationLS)registry.getDOMImplementation("LS-Load"); + +... + +DOMWriter builder = impl.createDOMWriter(); +writer.writeNode(System.out, document);</source> </a> </faq> @@ -229,18 +222,15 @@ identify attribute nodes by their namespaceURI and localName. Because of this fundamental difference, mixing both sets of methods can lead to unpredictable results.</em></p> - <source> -import org.w3c.dom.Document; + <source>import org.w3c.dom.Document; import org.w3c.dom.ls.DOMBuilder; ..... -Document document = -builder.parseURI("data/personal.xml"); +Document document = builder.parseURI("data/personal.xml"); document.setErrorHandler(new MyErrorHandler()); document.setNormalizationFeature("validate", true); -document.normalizeDocument(); - </source> +document.normalizeDocument();</source> <p> For more information, please refer to the @@ -263,29 +253,26 @@ You can register an error handler on a <code>DocumentBuilder</code> created using JAXP like this: </p> - <source> - import javax.xml.parsers.DocumentBuilder; - import org.xml.sax.ErrorHandler; - import org.xml.sax.SAXException; - import org.xml.sax.SAXParseException; + <source>import javax.xml.parsers.DocumentBuilder; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; - ErrorHandler handler = new ErrorHandler() { - public void warning(SAXParseException e) throws SAXException { +ErrorHandler handler = new ErrorHandler() { + public void warning(SAXParseException e) throws SAXException { System.err.println("[warning] "+e.getMessage()); - } - public void error(SAXParseException e) throws SAXException { + } + public void error(SAXParseException e) throws SAXException { System.err.println("[error] "+e.getMessage()); - } - public void fatalError(SAXParseException e) throws SAXException { + } + public void fatalError(SAXParseException e) throws SAXException { System.err.println("[fatal error] "+e.getMessage()); - throw e; - } - }; - - DocumentBuilder builder = /* builder instance */; - builder.setErrorHandler(handler); + throw e; + } +}; - </source> +DocumentBuilder builder = /* builder instance */; +builder.setErrorHandler(handler);</source> <p>If you are using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump> you can register an error handler with the <code>DOMBuilder</code> by supplying a class which implements the <code>org.w3c.dom.DOMErrorHandler</code> 1.4 +10 -16 xml-xerces/java/docs/faq-grammars.xml Index: faq-grammars.xml =================================================================== RCS file: /home/cvs/xml-xerces/java/docs/faq-grammars.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- faq-grammars.xml 8 Nov 2002 20:27:21 -0000 1.3 +++ faq-grammars.xml 21 Nov 2002 17:32:56 -0000 1.4 @@ -186,8 +186,7 @@ ignored. </p> <source><!DOCTYPE myDoc SYSTEM "my.dtd"> - <myDoc ...>...</myDoc> - </source> +<myDoc ...>...</myDoc></source> <p> Using these heuristics, Xerces's default grammar caching implementation appears to do a reasonable job at matching @@ -250,8 +249,7 @@ loader. For DTD's, for instance, just call <code>registerPreparser</code> like: </p> - <source>grammarPreparser("http://www.w3.org/TR/REC-xml", - null)</source> + <source>grammarPreparser("http://www.w3.org/TR/REC-xml", null)</source> <p> Schema grammars correspond to the URI "http://www.w3.org/2001/XMLSchema"; both these constants @@ -314,12 +312,10 @@ <p> For SAX and DOM the case is simple. Just do: </p> - <source> - XMLParserConfiguration config = new - StandardParserConfiguration(); - config.setProperty("http://apache.org/xml/properties/internal/grammar-pool", myFullGrammarPool); - (SAX|DOM)Parser parser = new (SAX|DOM)Parser(config); - </source> + <source>XMLParserConfiguration config = new StandardParserConfiguration(); +config.setProperty("http://apache.org/xml/properties/internal/grammar-pool", + myFullGrammarPool); +(SAX|DOM)Parser parser = new (SAX|DOM)Parser(config);</source> <p> Now your grammar pool instance will be used by all validators created by this parser to validate your @@ -330,12 +326,10 @@ is a bit trickier. You'll need to do something like this: </p> - <source> - System.setProperty("org.apache.xerces.xni.parser.Configuration", - "org.apache.xerces.parsers.XMLGrammarCachingConfiguration"); - DocumentBuilder builder = // JAXP factory invocation - //parse documents and store grammars - </source> + <source>System.setProperty("org.apache.xerces.xni.parser.Configuration", + "org.apache.xerces.parsers.XMLGrammarCachingConfiguration"); +DocumentBuilder builder = // JAXP factory invocation +// parse documents and store grammars</source> <p> Note that this only supports the "passive" caching approach discussed in <jump href="#passive">above</jump>. The 1.21 +4 -7 xml-xerces/java/docs/faq-write.xml Index: faq-write.xml =================================================================== RCS file: /home/cvs/xml-xerces/java/docs/faq-write.xml,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- faq-write.xml 27 Aug 2002 03:01:57 -0000 1.20 +++ faq-write.xml 21 Nov 2002 17:32:56 -0000 1.21 @@ -9,8 +9,7 @@ XML Processing (JAXP). The following source code shows how: </p> - <source> -import java.io.IOException; + <source>import java.io.IOException; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -83,11 +82,9 @@ }; DocumentBuilder builder = /* builder instance */; -builder.setErrorHandler(handler); - -</source> -<p>If you are using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump> -you can register an error handler with the <code>DOMBuilder</code> by supplying +builder.setErrorHandler(handler);</source> + <p>If you are using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump> + you can register an error handler with the <code>DOMBuilder</code> by supplying a class which implements the <code>org.w3c.dom.DOMErrorHandler</code> interface. For more information, refer to <link idref="faq-dom">FAQ</link></p> <p> 1.3 +56 -56 xml-xerces/java/docs/faq-xs.xml Index: faq-xs.xml =================================================================== RCS file: /home/cvs/xml-xerces/java/docs/faq-xs.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- faq-xs.xml 8 Nov 2002 22:27:01 -0000 1.2 +++ faq-xs.xml 21 Nov 2002 17:32:56 -0000 1.3 @@ -16,19 +16,19 @@ Schema Part 1 section 4.3.2. Here is an example with no target namespace: </p> <source><document - xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' - xsi:noNamespaceSchemaLocation='document.xsd'> - ... - </document></source> + xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' + xsi:noNamespaceSchemaLocation='document.xsd'> + ... +</document></source> <p>Here is an example with a target namespace. Note that it is an error to specify a different namespace than the target namespace defined in the Schema.</p> <source><document - xmlns='http://my.com' - xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' - xsi:schemaLocation='http://my.com document.xsd'> - ... - </document></source> + xmlns='http://my.com' + xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' + xsi:schemaLocation='http://my.com document.xsd'> + ... +</document></source> <p>Review the sample file, 'data/personal.xsd' for an example of an XML Schema grammar.</p> @@ -79,16 +79,16 @@ information while in the scope of the document handler start/end element calls:</p> <source>import org.apache.xerces.xni.psvi.*; - ... +... - public void startElement(QName element, XMLAttributes attributes, - Augmentations augs) { - ElementPSVI elemPSVI = (ElementPSVI)augs.getItem("ELEMENT_PSVI"); - // get PSVI items of this element out of elemPSVI - short attemp = elemPSVI.getValidationAttempted(); - short validity = elemPSVI.getValidity(); - ... - }</source> +public void startElement(QName element, XMLAttributes attributes, + Augmentations augs) { + ElementPSVI elemPSVI = (ElementPSVI)augs.getItem("ELEMENT_PSVI"); + // get PSVI items of this element out of elemPSVI + short attemp = elemPSVI.getValidationAttempted(); + short validity = elemPSVI.getValidity(); + ... +}</source> <note>For more information, please refer to the API documentation of the PSVI interfaces.</note> @@ -103,23 +103,21 @@ <code>endElement</code> method for the validation root. When this method is called, information about various components can be retrieved by</p> <source>import org.apache.xerces.xni.psvi.*; - import org.apache.xerces.impl.xs.psvi.*; +import org.apache.xerces.impl.xs.psvi.*; - ... - - public void endElement(QName element, Augmentations augs) { - ElementPSVI elemPSVI = (ElementPSVI)augs.getItem("ELEMENT_PSVI"); - XSModel xsModel = elemPSVI.getSchemaInformation(); - // get a list of [namespace schema information information item]s, - // one for each namespace. - ObjectList nsItems = xsModel.getNamespaceItems(); - // get an element declaration of the specified name and namespace - XSElementDeclaration elemDecl = xsModel.getElementDeclaration - (name, namespace); - ... - - }</source> +... +public void endElement(QName element, Augmentations augs) { + ElementPSVI elemPSVI = (ElementPSVI)augs.getItem("ELEMENT_PSVI"); + XSModel xsModel = elemPSVI.getSchemaInformation(); + // get a list of [namespace schema information information item]s, + // one for each namespace. + ObjectList nsItems = xsModel.getNamespaceItems(); + // get an element declaration of the specified name and namespace + XSElementDeclaration elemDecl = xsModel.getElementDeclaration + (name, namespace); + ... +}</source> </a> </faq> @@ -136,22 +134,24 @@ <code>org.apache.xerces.xni.psvi.AttributePSVI</code>. </p> <source>import org.apache.xerces.xni.psvi.*; - import org.apache.xerces.impl.xs.psvi.XSModel; - import org.apache.xerces.impl.xs.psvi.XSNamedMap; - ... - Document document = parser.getDocument(); - Element root = document.getDocumentElement(); - - // retrieve PSVI for the root element - ElementPSVI rootPSVI = (ElementPSVI)root; - - // retrieve the schema used in validation of this document - XSModel schema = rootPSVI.getSchemaInformation(); - XSNamedMap elementDeclarations = schema.getComponents(XSConstants.ELEMENT_DECLARATION); - - // get schema normalized value - String normalizedValue = rootPSVI.getSchemaNormalizedValue(); - </source> +import org.apache.xerces.impl.xs.psvi.XSModel; +import org.apache.xerces.impl.xs.psvi.XSNamedMap; + +... + +Document document = parser.getDocument(); +Element root = document.getDocumentElement(); + +// retrieve PSVI for the root element +ElementPSVI rootPSVI = (ElementPSVI)root; + +// retrieve the schema used in validation of this document +XSModel schema = rootPSVI.getSchemaInformation(); +XSNamedMap elementDeclarations = + schema.getComponents(XSConstants.ELEMENT_DECLARATION); + +// get schema normalized value +String normalizedValue = rootPSVI.getSchemaNormalizedValue();</source> </a> </faq> @@ -166,14 +166,14 @@ element and its attributes. </p> <source>import org.apache.xerces.impl.xs.psvi.PSVIProvider; - import javax.xml.parsers.SAXParser; - import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +... - ... - SAXParserFactory factory = SAXParserFactory.newInstance(); - SAXParser parser = factory.newSAXParser(); - PSVIProvider psviProvider = (PSVIProvider)parser; - </source> +SAXParserFactory factory = SAXParserFactory.newInstance(); +SAXParser parser = factory.newSAXParser(); +PSVIProvider psviProvider = (PSVIProvider)parser;</source> </a> </faq> <faq title="Parsing and analyzing an XML schema">
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]