Java XML Operations (dom4j Modify XML + XPath technology + SAX parsing + XML constraints)

Source: Internet
Author: User
Tags tag name

1 XML Basics

1) The role of XML

1.1 as a software configuration file

1.2 as a small "database"

2)XML Syntax ( as defined by the WIPO)

Tags:

Tag names cannot begin with a number, and cannot have spaces in between, and are case-sensitive. Have and have only one root tag.

Property:

there can be multiple properties, but attribute values must be enclosed in quotation marks (single or double quotation marks), but cannot be omitted or single double mix.

Document declaration:

<?xml version= "1.0" encoding= "Utf-8"?>

encoding= "Utf-8": Encoding when opening or parsing an XML document

Attention:

The encoding and parsing of XML documents when saving XML documents should be consistent, in order to avoid Chinese garbled problem!

3)XML parsing

programs to read or manipulate XML documents

two ways to parse: DOM parsing vs SAX parsing

DOM Parsing principle: Once the XML document is loaded into The document tree, through the document The object gets the node object, accessing the XML document content (tags, attributes, text, comments) through the node object.

DOM4J Tool (based on DOM parsing principle):

To read an XML document:

Document doc = new Saxreader (). Read ("XML file ");

Node:

Nodeiterator (); all Nodes

Label:

Element (" name ") Specifies the name of the first child label object

Elementiterator (" name "); Specifies the name of all child label objects

Elements (); all child Label Objects

Property:

AttributeValue("name") specifies the name of the property value

Attribute (" name ") Specifies the name of the Property object

GetName () property name

GetValue() property value

Atributeiterator () all Property objects (Iterator)

Attributes () all Property objects (List)

Text:

GetText() Gets the text of the current label

Elementtext (" child tag name ") Gets the text of the child label

2 dom4j modifying an XML document 2.1 Writing content to an XML document

XMLWriter writer = new XMLWriter (OutputStream, Outputforamt)

Wirter.write (Document);

2.2 modifying an XML document's API

Increase:

Documenthelper.createdocument () Add a document

AddElement (" name ") add Tag

AddAttribute (" name ", "value") Add Property

Modify:

Attribute.setvalue (" value ") Modify the property value

Element.addatribute (" property name with the same name ", " value ") modifies the property value with the same name

Element.settext (" content ") Modify text content

Delete

Element.detach (); Delete a label

Attribute.detach (); Delete Property

3 XPath technology 3.1 Introduction

problem: When using dom4j query to compare deep hierarchies of nodes (tags, attributes, text), compare the trouble!!!

3.2 xPath effect

It is primarily used to quickly get the desired node object.

3.3 How to use xPath technology in dom4j

1) Import the xPath support jar package. Jaxen-1.1-beta-6.jar

2) using the XPath method

List<node> selectnodes ("XPath expression "); querying multiple node objects

Node selectSingleNode ("XPath expression "); Querying a Node object

3.4 xPath Syntax

/ absolute path represents The beginning of the root position of the XML or child element (a hierarchy)

A relative path represents a selection element that is not divided into any hierarchy.

* wildcard means matching all elements

[] condition indicates which element is selected under what conditions

The @ property represents the Select Attribute node

and relationships (equivalent to &&) of conditions

Text () literal indicates selection of text content

4 SAX parsing 4.1 Review DOM parsing

DOM Parsing principle: The XML document is loaded into memory at once, and then the document tree is built in memory .

The memory requirements are compared.

Disadvantages: not suitable for reading large-capacity XML files, which can lead to memory overflow.

SAX parsing principle: Load a little, read a little, handle a little. The memory requirements are relatively low.

4.2 SAX parsing tool

SAX Parsing tool - provided by Sun. Built into the jdk . org.xml.sax.*

core of API:

SAXParser class: For reading and parsing XML file Objects

Parse(file F, defaulthandler dh) method: parse xml file

parameter one: File: Represents the Read XML files.

parameter two: DefaultHandler: SAX event handler. Using subclasses of DefaultHandler

For example: {

1. Create a SAXParser object

SAXParser parser=saxparserfactory. newinstance (). Newsaxparser ();

2. Call the Parse method

Parser.parse (new File ("./src/contact.xml"), new Mydefaulthandler ());

} [ A class inherits the class class name (extends defaulthandler) When the call is created and passed in

API for the DefaultHandler class :

void Startdocument (): called when the document starts reading

void Enddocument () : Called when reading to the end of the document

void Startelement (String uri, String localname, String qName, Attributes Attributes) : Called when the start tag is read

void EndElement (String uri, String localname, String qName) : Called when the end tag is read

void characters (char[] ch, int start, int length) : Called when the text content is read

============dom parsing vs SAX parsing ========

DOM parsing

SAX parsing

Principle: Loading XML documents at once, not suitable for large-capacity file reads

Principle: Load a little, read a little, handle a little. Suitable for large-capacity file reading

DOM parsing can be arbitrarily deleted and changed to

SAX parsing can only read

DOM parsing arbitrary reading of any location data, even reading backwards

SAX parsing can only be read from the top down, sequentially, and not read backwards

DOM parsing Object-oriented programming methods (Node,Element,Attribute), Java The developer code is simple.

SAX parses event-based programming methods. Java Development coding is relatively complex.

Summarize:

1)dom4j Modify XML document

New Xmlwrier ();

......

2)xPath technology: Querying XML nodes quickly

SelectNodes ()

Selectsinglnode ();

XPath expression Language

3) SAX parsing

SAXParser Parse

Parser()

DefaultHandler class:

Startelement ();

characters ();

EndElement ();

Java XML Operations (dom4j Modify XML + XPath technology + SAX parsing + XML constraints)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.