Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Java API for XML Processing

From Wikipedia, the free encyclopedia
(Redirected fromJAXP)
Java application programming interface
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(June 2013) (Learn how and when to remove this message)
This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources.
Find sources: "Java API for XML Processing" – news ·newspapers ·books ·scholar ·JSTOR
(June 2013) (Learn how and when to remove this message)
(Learn how and when to remove this message)

Incomputing, theJava API for XML Processing (JAXP) (/ˈæksp/JAKS-pee), one of theJava XMLapplication programming interfaces (APIs), provides the capability of validating and parsingXML documents. It has three basic parsing interfaces:

In addition to the parsing interfaces, the API provides anXSLT interface to provide data and structural transformations on an XML document.

JAXP was developed under theJava Community Process as JSR 5 (JAXP 1.0), JSR 63 (JAXP 1.1 and 1.2), and JSR 206 (JAXP 1.3).

Java SE versionJAXP version bundled
1.41.1
1.51.3
1.61.4
1.7.01.4.5
1.7.401.5
1.81.6[1]

JAXP version 1.4.4 was released on September 3, 2010. JAXP 1.3 was declaredend-of-life on February 12, 2008.

DOM interface

[edit]
Main article:Document Object Model

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 aDocumentBuilder, as it builds an in-memoryDocument representation. Thejavax.xml.parsers.DocumentBuilder is created by thejavax.xml.parsers.DocumentBuilderFactory.[2] TheDocumentBuilder creates anorg.w3c.dom.Document instance - a tree structure containing nodes in the XML Document. Each tree node in the structure implements theorg.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]
Main article:Simple API for XML

Thejavax.xml.parsers.SAXParserFactory creates the SAX parser, called theSAXParser. Unlike the DOM parser, the SAX parser does not create an in-memory representation of the XML document and so runs faster and uses less memory. Instead, the SAX parser informs clients of the XML document structure by invoking callbacks, that is, by invoking methods on anDefaultHandler instance provided to the parser. This way of accessing document is calledStreaming XML.

TheDefaultHandler class implements theContentHandler, theErrorHandler, theDTDHandler, and theEntityResolver interfaces. Most clients will be interested in methods defined in theContentHandler interface that are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:

  • startDocument() andendDocument() methods that are called at the start and end of a XML document.
  • startElement() andendElement() methods that are called at the start and end of a document element.
  • characters() method that is called with the text data contents contained between the start and end tags of an XML document element.

Clients provide a subclass of theDefaultHandler that overrides these methods and processes the data. This may involve storing the data into a database or writing it out to a stream.

During parsing, the parser may need to access external documents. It is possible to store a local cache for frequently used documents using anXML Catalog.

This was introduced with Java 1.3 in May 2000.[3]

StAX interface

[edit]
Main article:StAX

StAX was designed as a median between the DOM and SAX interface. In its metaphor, the programmatic entry point is a cursor that represents a point within the document. The application moves the cursor forward - 'pulling' the information from the parser as it needs. This is different from an event based API - such as SAX - which 'pushes' data to the application - requiring the application to maintain state between events as necessary to keep track of location within the document.

XSLT interface

[edit]
Main article:XSLT

TheXMLStylesheetLanguage forTransformations, orXSLT, allows for conversion of an XML document into other forms of data. JAXP provides interfaces in packagejavax.xml.transform allowing applications to invoke an XSLT transformation. This interface was originally called TrAX (Transformation API for XML), and was developed by an informal collaboration between the developers of a number of Java XSLT processors.

Main features of the interface are

Two abstract interfacesSource andResult 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 ofSource orResult it is prepared to handle. In practice all JAXP processors supports several standard kinds of Source (DOMSource,SAXSourceStreamSource) and several standard kinds of Result (DOMResult,SAXResultStreamResult) and possibly other implementations of their own.

Example

[edit]

The most primitive but complete example of XSLT transformation launching may look like this:

/* 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;publicclassXsltDemo{publicstaticvoidmain(String[]args)throwsTransformerFactoryConfigurationError,TransformerException{//language=xsltStringxsltResource="""                <?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=XMLStringxmlSourceResource="""                <?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:

<?xml version='1.0' encoding='UTF-8'?><xsl:stylesheetversion='2.0'xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:outputmethod='xml'indent='no'/><xsl:templatematch='/'><reRoot><reNode><xsl:value-ofselect='/root/node/@val'/>world</reNode></reRoot></xsl:template></xsl:stylesheet>

To the following hardcoded XML document:

<?xml version='1.0' encoding='UTF-8'?><root><nodeval='hello'/></root>

The result of execution will be

<?xml version="1.0" encoding="UTF-8"?><reRoot><reNode>helloworld</reNode></reRoot>

Citations

[edit]
  1. ^"The Java Community Process(SM) Program - JSRS: Java Specification Requests - detail JSR# 206".
  2. ^Horstmann 2022, §3.3 Parsing an XML Document.
  3. ^Compare theJava 1.2.1 API index with the1.3 index. The Java Specification Request (JSR) 5,XML Parsing Specification, was finalised on21 March, 2000.

References

[edit]

External links

[edit]
Jakarta EE specifications
Web app
Enterprise app
Web services
Other
Removed
Related
Retrieved from "https://en.wikipedia.org/w/index.php?title=Java_API_for_XML_Processing&oldid=1270782138"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp