Home  >  Article  >  Backend Development  >  Introduction to Java&Xml Tutorial (1)

Introduction to Java&Xml Tutorial (1)

黄舟
黄舟Original
2017-02-22 14:27:051352browse

XML is a technology widely used for data transmission and storage. The Java language provides a variety of APIs to parse XML, such as DOM, SAX, StAX, and JAXB. There are also other APIs for parsing XML, such as JDOM. The purpose of this tutorial is to explore using different types of APIs to parse XML and learn to perform some common tasks using XML.

DOM XML parsing

DOM parsing is the simplest parsing method. Load the XML file into memory, and then we can parse the XML by traversing the Node nodes. DOM parsing is suitable for small files with less content. When the files are larger, the parsing speed is very slow and takes up a lot of memory.
Reading xml files
This article introduces how to use DOM to parse XML files and convert XML content into Java objects.
Generate XML file
This article describes how to convert Java objects into XML files and store them on the local disk.
Modify XML files
The DOM parsing mechanism can also be used to modify XML data. This article introduces how to use the DOM parsing mechanism to add elements, delete elements, modify element values, modify attributes and other operations.

SAX XML parsing

Java SAX parsing mechanism provides an API for parsing XML documents. The SAX parsing mechanism is different from DOM. It does not load all XML into memory, but reads it in sequence. Get the XML document. It is event-based parsing. We need to implement a custom Handler class and use its callback method to parse the XML file. It is more efficient than the DOM parsing mechanism when parsing large files.
Reading XML files
Learn how to create our callback Handler class to read XML content into a List.

StAX XML parsing

The full name of Java StAX is Java Streaming API for XML, which provides a specific implementation of parsing XML. StAX consists of two sets of APIs - cursor-based API and iterator-based API.
Read XML files using StAX Iterator API
In this tutorial we will learn how to use StAX to iteratively read XML.
Generating XML files using the StAX Iterator API
In this tutorial we will learn how to generate XML using the iterator-based StAX API.
Use StAX Cursor API to read XML files
This article introduces how to use StAX Cursor API to read XML data into Java objects.
Generating XML files using the StAX Cursor API
The Java StAX Cursor API is very straightforward in creating XML and outputting data in it. We need to create an XMLStreamWriter object and then write data to the object.

JDOM XML parsing

JDOM provides a very excellent API to make reading, modifying, and generating XML files easier. It provides wrapper classes for you to choose specific implementations from SAX parsing, DOM parsing, and StAX event parsing.
The advantage of using JDOM is that you can switch parsers from SAX and DOM STAX, and you can provide factory methods for client applications to choose to implement.
JDOM reads XML files
In this tutorial, we will learn how to use JDOM to read XML files.
JDOM generates XML files
In this tutorial, we will learn to use JDOM to write XML files. JDOM Document provides convenient methods for creating elements and attributes. The XMLOutputter class can be used to write Document objects to any OutputStream or Writer object.

JAXB

The full name of JAXB is Java Architecture for XML Binding. Using the API it provides, you can easily convert Java objects into XML, or convert XML content into Java objects. JAXB existed as a separate project, but in Java 6 it became part of the JDK.
JAXB Tutorial
Using JAXB is very simple, it uses annotations, we need to use annotations on the Java object to indicate the creation of XML, then we need to create a Marshaller to convert the object to XML, and use Unmarshaller to convert the XML to a Java object. In this tutorial, we will learn most of the JAXB annotations and learn to use Marshaller to convert objects to XML and Unmarshaller to convert XML to objects.

JiBX

JiBX is a very powerful framework for converting XML and Java objects to and from each other. It is widely used in multi-application integration that uses XML as a data transmission format, such as Web Service and system integration based on the message model.
JiBX Tutorial
There are many frameworks available for XML conversion, such as JAXB and XMLBeans, but JiBX uses different methods for XML binding and conversion process. JiBX generates Ant scripts at compile time through tool classes to perform these tasks. . This approach significantly reduces processing time by moving away from the traditional two-step process.

XPath

XPath provides syntax to define parts of XML documents, and XPath expressions use query strings to select parts of XML documents. Using XPath expressions, we can find any node.
XPath Tutorial
javax.xml.xpath package provides support for XPath. XPathExpression can be created through the factory method provided by the XPath API. This tutorial mainly introduces the use of XPath expressions to find XML nodes.

Some XML related tasks

Generate general XML from XSD in Eclipse
If you are engaged in Web Service related work, you need to use XSD and test WebService, you need to generate XML files from XSD files . Eclipse provides a very simple way to help you complete it.
Use XSD to verify XML legality
Java XML Validation API can use XSD to verify XML files. The javax.xml.validation.Validator class will be used in this tutorial.
Java XML properties file
Usually the configuration parameters of our java application are stored in properties files. Java's properties file can be a properties file of key-value pairs or an XML file. In this example, we introduce how to use XML as a properties file and obtain relevant information from the properties file.
SOAP XML
Soap is an XML-based transmission protocol. The full name of Soap is Simple Object Access Protocol. Soap is a lightweight data exchange mechanism, based on XML and cross-platform.
Format XML document
Use practical utility classes to print XML and convert XML Document objects into strings.
String and Document conversion
Sometimes in Java programming, we obtain a string whose content is in XML data format. If we need to process it, we need to convert it into a Document object. Or sometimes for debugging purposes, you need to convert the Document object into a string output. There are two useful methods for converting between String and Document objects. This article will introduce it.

Original address: http://www.php.cn/

XML is a technology widely used for data transmission and storage. The Java language provides a variety of APIs to parse XML, such as DOM, SAX, StAX, and JAXB. There are also other APIs for parsing XML, such as JDOM. The purpose of this tutorial is to explore using different types of APIs to parse XML and learn to perform some common tasks using XML.

DOM XML parsing

DOM parsing is the simplest parsing method. Load the XML file into memory, and then we can parse the XML by traversing the Node nodes. DOM parsing is suitable for small files with less content. When the files are larger, the parsing speed is very slow and takes up a lot of memory.
Reading xml files
This article introduces how to use DOM to parse XML files and convert XML content into Java objects.
Generate XML file
This article describes how to convert Java objects into XML files and store them on the local disk.
Modify XML files
The DOM parsing mechanism can also be used to modify XML data. This article introduces how to use the DOM parsing mechanism to add elements, delete elements, modify element values, modify attributes and other operations.

SAX XML parsing

Java SAX parsing mechanism provides an API for parsing XML documents. The SAX parsing mechanism is different from DOM. It does not load all XML into memory, but reads it in sequence. Get the XML document. It is event-based parsing. We need to implement a custom Handler class and use its callback method to parse the XML file. It is more efficient than the DOM parsing mechanism when parsing large files.
Reading XML files
Learn how to create our callback Handler class to read XML content into a List.

StAX XML parsing

The full name of Java StAX is Java Streaming API for XML, which provides a specific implementation of parsing XML. StAX consists of two sets of APIs - cursor-based API and iterator-based API.
Read XML files using StAX Iterator API
In this tutorial we will learn how to use StAX to iteratively read XML.
Generating XML files using the StAX Iterator API
In this tutorial we will learn how to generate XML using the iterator-based StAX API.
Use StAX Cursor API to read XML files
This article introduces how to use StAX Cursor API to read XML data into Java objects.
Generating XML files using the StAX Cursor API
The Java StAX Cursor API is very straightforward in creating XML and outputting data in it. We need to create an XMLStreamWriter object and then write data to the object.

JDOM XML parsing

JDOM provides a very excellent API to make reading, modifying, and generating XML files easier. It provides wrapper classes for you to choose specific implementations from SAX parsing, DOM parsing, and StAX event parsing.
The advantage of using JDOM is that you can switch parsers from SAX and DOM STAX, and you can provide factory methods for client applications to choose to implement.
JDOM reads XML files
In this tutorial, we will learn how to use JDOM to read XML files.
JDOM generates XML files
In this tutorial, we will learn to use JDOM to write XML files. JDOM Document provides convenient methods for creating elements and attributes. The XMLOutputter class can be used to write Document objects to any OutputStream or Writer object.

JAXB

The full name of JAXB is Java Architecture for XML Binding. Using the API it provides, you can easily convert Java objects into XML, or convert XML content into Java objects. JAXB existed as a separate project, but in Java 6 it became part of the JDK.
JAXB Tutorial
Using JAXB is very simple, it uses annotations, we need to use annotations on the Java object to indicate the creation of XML, then we need to create a Marshaller to convert the object to XML, and use Unmarshaller to convert the XML to a Java object. In this tutorial, we will learn most of the JAXB annotations and learn to use Marshaller to convert objects to XML and Unmarshaller to convert XML to objects.

JiBX

JiBX is a very powerful framework for converting XML and Java objects to and from each other. It is widely used in multi-application integration that uses XML as a data transmission format, such as Web Service and message-based System integration of models.
JiBX Tutorial
There are many frameworks available for XML conversion, such as JAXB and XMLBeans, but JiBX uses different methods for XML binding and conversion process. JiBX generates Ant scripts at compile time through tool classes to perform these tasks. . This approach significantly reduces processing time by moving away from the traditional two-step process.

XPath

XPath provides syntax to define parts of XML documents, and XPath expressions use query strings to select parts of XML documents. Using XPath expressions, we can find any node.
XPath Tutorial
javax.xml.xpath package provides support for XPath. XPathExpression can be created through the factory method provided by the XPath API. This tutorial mainly introduces the use of XPath expressions to find XML nodes.

Some XML related tasks

Generate general XML from XSD in Eclipse
If you are engaged in Web Service related work, you need to use XSD and test the WebService, you need to generate it from the XSD file Generate XML file. Eclipse provides a very simple way to help you complete it.
Use XSD to verify XML legality
Java XML Validation API can use XSD to verify XML files. The javax.xml.validation.Validator class will be used in this tutorial.
Java XML properties file
Usually the configuration parameters of our java application are stored in properties files. Java's properties file can be a properties file of key-value pairs or an XML file. In this example, we introduce how to use XML as a properties file and obtain relevant information from the properties file.
SOAP XML
Soap is an XML-based transmission protocol. The full name of Soap is Simple Object Access Protocol. Soap is a lightweight data exchange mechanism, based on XML and cross-platform.
Format XML document
Use practical utility classes to print XML and convert XML Document objects into strings.
String and Document conversion
Sometimes in Java programming, we obtain a string whose content is in XML data format. If we need to process it, we need to convert it into a Document object. Or sometimes for debugging purposes, you need to convert the Document object into a string output. There are two useful methods for converting between String and Document objects. This article will introduce it.

The above is the introduction to Java&Xml Tutorial (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn