Using the org.xml.sax.DefaultHandler Attribute to Parse a Document Using the org.w3c.dom.Document Attribute to Parse a Document Validating and Non-Validating Parsers

3-4 Programming XML for Oracle WebLogic Server For more information on servlet filters, see Filters in Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server.

3.2.4 Using the org.xml.sax.DefaultHandler Attribute to Parse a Document

The following code example shows how to use the setAttribute method: import weblogic.servlet.XMLProcessingException; import org.xml.sax.helpers.DefaultHandler; ... public void doPostHttpServletRequest request, HttpServletResponse response throws ServletException, IOException { try { request.setAttributeorg.xml.sax.helpers.DefaultHandler, new DefaultHandler; } catchXMLProcessingException xpe { System.out.printlnError in processing XML; xpe.printStackTrace; return; } ... You can also use the org.xml.sax.HandlerBase attribute to parse an XML document, although it is deprecated: request.setAttributeorg.xml.sax.HandlerBase, new HandlerBase;

3.2.5 Using the org.w3c.dom.Document Attribute to Parse a Document

The following code example shows how to use the getAttribute method. import org.w3c.dom.Document; import weblogic.servlet.XMLProcessingException; ... public void doPostHttpServletRequest request, HttpServletResponse response throws ServletException, IOException { try { Document doc = request.getAttributeorg.w3c.dom.Document; } catchXMLProcessingException xpe { System.out.printlnError in processing XML; xpe.printStackTrace; return; } ... Note: This code example shows a simple way to parse a document using SAX and the setAttribute method. This method of parsing a document is a WebLogic Server convenience feature, and it is not supported by other servlet vendors. Therefore, if you plan to run your application on other servlet platforms, do not use this feature. Developing XML Applications with WebLogic Server 3-5

3.2.6 Validating and Non-Validating Parsers

As previously discussed, a well-formed document is one that is syntactically correct according to the rules outlined in the W3C Recommendation for XML 1.0. A valid document is one that follows the constraints specified by its DTD or schema. A non-validating parser verifies that a document is well-formed, but does not verify that it is valid. To turn on validation while parsing a document assuming you are using a validating parser, you must: ■ Set the SAXParserFactory.setValidating method to true, as shown in the following example: SAXParserFactory factory = SAXParserFactory.newInstance; factory.setValidatingtrue; ■ Ensure that the XML document you are parsing includes either in-line or by reference a DTD or a schema.

3.2.7 Handling Entity Resolution While Parsing an XML Document