简体   繁体   中英

Java built-in XML

Does Java have a built in XML library for generating and parsing documents? If not, which third party one should I be using?

The Sun Java Runtime comes with the Xerces and Xalan implementations that provide the ability to parse XML (via the DOM and SAX intefaces), and also perform XSL transformations and execute XPath queries.

However, it is better to use the JAXP API to work on XML, since JAXP allows you to not worry about the underlying implementation used (Xerces or Crimson or any other). When you use JAXP, at runtime the JRE will use the service provider it can locate, to perform the needed operations. As indicated previously, Xerces/Xalan will be used since it is shipped with the Sun JRE (not others though), so you dont have to download and install a specific provider (say, a different version of Xerces, or Crimson).

A basic JAXP tutorial can be found in The J2EE 1.4 tutorial (Its from the J2EE tutorial, but it will help).

Do note that the Xerces/Xalan implementations provided by the Sun JRE, will not be found in the org.apache.xerces.* or org.apache.xalan.* packages. Instead, they will be present in the internal com.sun.org.apache.xerces.* and com.sun.org.apache.xalan.* packages.

By the way, JDOM is not an XML parser - it will use the parser provided to it by JAXP in order to provide you with an easier abstraction to work with.

Yes. It has a two options in the javax.xml package: DOM builds documents in memory, and SAX is an event-based approach.

You may also want to look at JDOM, which is a 3rd party library that offers a combination of the two, and can be easier to use.

Yes. Java contains javax.xml library. You can checkout some samples at Sun's Java API for XML Code Samples .

However, I personally like using JDOM library .

javax.xml package contains Java's native XML solution which is actually a special version of Xerces . You can do what you asked with it, however using 3rd party libraries such as JDOM makes the whole process a lot easier.

Have a look at JAX-B This is increasingly the "standard" way to do XML processing. Uses Java annotations to simplify the programming model. The reference gives sample code for reading and writing XML.

Java does come with a large set of packages and classes to handle XML. These are part of the Standard Edition JDK, and located under the javax.xml package.

Aside from reading XML and writing it with DOM or SAX , these packages also perform XSL transformations , JAX-B object marshalling and unmarshalling, XPath processing and web services SOAP handling. I advise you to read more about these online in Sun's excellent tutorials.

I can't tell you which one to use (few requirements specified, and there are a dozen libraries), but I would seriously consider XOM ( here ).

Written by Eliotte Rusty Harold, it is quite complete in terms of the XML spec, and generally excellent. I have found it very easy to use. See the link above for Harold's motivation and criticism of other solutions.

您可以查看javax.xml包,其中包含在Java中使用XML文档所需的一切...

Java API for XML Processing (JAXP) is part of standard library JavaSE. JAXP allows you to code against standard interface and lets you pick the parser implementation later if needed.

The Java API for XML Processing, or JAXP for short, enables applications to parse and transform XML documents using an API that is independent of a particular XML processor implementation. JAXP also provides a pluggability feature which enables applications to easily switch between particular XML processor implementations.

You can use StAX (streaming API for XML) http://en.wikipedia.org/wiki/StAX http://www.xml.com/pub/a/2003/09/17/stax.html https://sjsxp.dev.java.net/

StAx is optimized to process large xml files, without causing OOM (out of memory) problem :)

As is said above... Java's SDK now comes with Xerces and Xalan. Xalan only implements version 1.0 of the XSLT API, so if you want 2.0, you should look at Saxon from Michael Kay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM