📄 parsing.xtp
字号:
<s1 title="JAXP Parsing"><p>The standard Java interface for XML parsing, JAXP, provides two methods forparsing XML: DOM and SAX. The DOM (document object model) parses the inputinto an XML tree using the org.w3c.xml.Node API. SAX doesn't create anyresult object tree, instead calling methods in a ContentHandler.</p><p>In general, DOM parsing is easier to understand, but SAX parsing canbe more efficient because many applications can avoid creating anintermediate XML tree.</p><s2 title="DOM parsing"><p>For strict XML parsing to the DOM, the best technique is to use thestandard JAXP API. That way, you can configure your application touse whichever XML parser is most convenient for you.</p><p>JAXP parsing uses the following steps:</p><ol><li>Create a DocumentBuilderFactory instance.<li>Set any parser flags or properties.<li>Create DocumentBuilder to create the parser.<li>Parse the document</ol><example title="Reading and Writing using the DOM">import java.io.*;import javax.xml.parsers.*;import org.w3c.dom.*;import com.caucho.xml.*;...// Create a new parser using the JAXP API (javax.xml.parser)DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder parser = factory.newDocumentBuilder();// Parse the file into a DOM Document (org.w3c.dom)Document doc = parser.parse("test.xml");// Create a new XML printer (com.caucho.xml)FileOutputStream os = new FileOutputStream("out.xml");XmlPrinter printer = new XmlPrinter(os);// Print the documentprinter.print(doc);os.close();</example><p>Printing DOM objects is easily done by using Resin's XmlPrinter API.</p></s2><s2 title="Configuration"><p>JAXP is a standard interface which supports pluggable XMLparser implementations. JAXP selects the parser based onsystem properties. You can set the properties to selecta different parser than the default one.</p><p>If you use libraries which include JAXP classes, those librariesmight default to another, probably slower, XML parser. You mayneed to configure the system properties to ensure that you'll useResin's fast parser.</p><deftable title="JAXP Properties for Resin"><tr><th>system property<th>Resin value<tr><td>javax.xml.parsers.DocumentBuilderFactory <td>com.caucho.xml.parsers.XmlDocumentBuilderFactory<tr><td>javax.xml.parsers.SAXParserFactory <td>com.caucho.xml.parsers.XmlSAXParserFactory<tr><td>javax.xml.transform.TransformerFactory <td>com.caucho.xsl.Xsl</deftable><deftable title="JAXP Properties for Xalan/Xerces"><tr><th>system property<th>Xerces value<tr><td>javax.xml.parsers.DocumentBuilderFactory <td>org.apache.xerces.jaxp.DocumentBuilderFactoryImpl<tr><td>javax.xml.parsers.SAXParserFactory <td>org.apache.xerces.jaxp.SAXParserFactoryImpl<tr><td>javax.xml.transform.TransformerFactory <td>org.apache.xalan.processor.TransformerFactoryImpl</deftable><p>The resin.conf and web.xml will let you configure system properties ona per-application basis. The configuration looks like:</p><example><web-app> <system-property javax.xml.parsers.DocumentBuilderFactory= "com.caucho.xml.parsers.XmlDocumentBuilderFactory"/> ...</web-app></example></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -