FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Java_API_for_XML_Processing

Java API for XML Processing

DOM interface

edit

The DOM interface parses an entire XML document and constructs a complete in-memory representation of the document using the classes and modeling the concepts found in the Document Object Model Level 2 Core Specification.

The DOM parser is called a javax.xml.parsers.DocumentBuilderFactory. The org.w3c.dom.Node interface. Among the many different types of tree nodes, each representing the type of data found in an XML document, the most important include:

  • element nodes that may have attributes
  • text nodes representing the text found between the start and end tags of a document element.

SAX interface

edit
javax.xml.parsers.SAXParserFactory creates the SAX parser, called the ContentHandler, the EntityResolver interfaces. Most clients will be interested in methods defined in the ContentHandler interface that are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:

  • The XML Stylesheet Language for Transformations, or XSLT, allows for conversion of an XML document into other forms of data. JAXP provides interfaces in package TransformerFactory.newInstance(), TransformerFactory.newTemplates(Source source)), also Templates object to create a Transformer provides methods to set stylesheet parameters and serialization options (for example, whether output should be indented), and a method to actually run the transformation. (Result are defined to represent the input and output of the transformation. This is a somewhat unconventional use of Java interfaces, since there is no expectation that a processor will accept any class that implements the interface - each processor can choose which kinds of DOMSource, DOMResult, /* file src/examples/xslt/XsltDemo.java */ packageexamples.xslt; importjava.io.StringReader; importjava.io.StringWriter; importjavax.xml.transform.Transformer; importjavax.xml.transform.TransformerException; importjavax.xml.transform.TransformerFactory; importjavax.xml.transform.TransformerFactoryConfigurationError; importjavax.xml.transform.stream.StreamResult; importjavax.xml.transform.stream.StreamSource; publicclass XsltDemo{ publicstaticvoidmain(String[]args)throwsTransformerFactoryConfigurationError,TransformerException{ //language=xslt StringxsltResource=""" <?xml version='1.0' encoding='UTF-8'?> <xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' indent='no'/> <xsl:template match='/'> <reRoot><reNode><xsl:value-of select='/root/node/@val' /> world</reNode></reRoot> </xsl:template> </xsl:stylesheet> """; // language=XML StringxmlSourceResource=""" <?xml version='1.0' encoding='UTF-8'?> <root><node val='hello'/></root> """; StringWriterxmlResultResource=newStringWriter(); TransformerxmlTransformer=TransformerFactory.newInstance().newTransformer( newStreamSource(newStringReader(xsltResource)) ); xmlTransformer.transform( newStreamSource(newStringReader(xmlSourceResource)),newStreamResult(xmlResultResource) ); System.out.println(xmlResultResource.getBuffer().toString()); } }

It applies the following hardcoded XSLT transformation:


View original on Wikipedia | Back | FazBrowse Home