Junior College Java This path----review XML Learning (3.21-3.22) __xml

Source: Internet
Author: User

It may be that the basics of some of the objects that have been previously available------the overall feeling that this lesson is not difficult.

Although the things that I have learned are the things that I used to touch, I still feel that I have learned a lot of good things, especially the ability to look up documents in an important way, and I am aware of that. Because Java is the reason for open source software, all have a lot of open source tools, and only parse XML commonly used is: dom,sax,jdom,dom4j. Not to mention the other Java open source tools, there are hundreds of them. So if it's impossible to remember by one person, therefore, the ability to learn is particularly important, so the teacher stressed for students is very necessary, so I am gratified that they did not ask us to remember, but with our step-by-step learning to consult the document, how to access can quickly get what you want. This is experience. Before I saw English documents do not know what I am, perhaps because in this industry has to do the choice, now the ability to read English documents in the unconscious has been elevated a large section.

Summarize

Use of XML

1. Access data 2. Configure properties from previous experience in the project (in Chongqing x software company, the data is read from one data, then the XML document is generated, and the other server is populated to the datasheet based on the value that reads the XML. XML is used primarily for configuring XML to pass data.

DOM,SAX,JDOM,DOM4J parse XML's Common pros and cons:

DOM reads all of the content into memory based on the tree, so when the XML file is large, it consumes a small amount of memory data

SAX is based on the event-type needs to realize the large amount of data from itself

JDOM is an open source project that uses pure Java technology to parse, generate, serialize, and manipulate XML documents based on a tree-structured architecture. The purpose of Jdom is to become a Java-specific document model that simplifies interaction with XML and is faster than using DOM

DOM4J features excellent performance, powerful features and extreme ease of use, and it is also an open source software. Now you can see that more and more Java software is using dom4j to read and write XML, and it is particularly worth mentioning that even Sun's JAXM (Java API for XML messaging) is also using DOM4J. Many open source projects currently use DOM4J, such as the famous hibernate also use dom4j to read XML configuration files. Comprehensive Yong Superior

Our main study is DOM4J parsing XML technology, other understanding


Several steps to read XML data:
Locate the file-------new files (filepath);
Open the file-------new FileInputStream (files);
A machine that turns inputstream into a tree-like thing-----Document New XMLReader (). read (in).
Find Roots---users document.getrootelement ();
Find a branch---user
Look for leaves----id,username the corresponding value (label body)
Closes the stream close ();

study here, not to memorize this step, but a thought.

If I want to write a tool to parse XML files myself, I am sure to operate on the file, and I will use the stream, so I will use the flow to locate the file. Then read the characters in the file to judge. And here, DOM4J has provided a class to manipulate XML files, and we just have to call ready-made methods.


Describes the properties of a user entity:
Id,username,password,gender,birth,address

<?xml version= "1.0" encoding= "utf-8j"?>

<users count= "2" >-----------------> equivalent to table name

<user>---------------> A record in the table

<id></id>--------> values in records

<username></username>

<passowrd></passwordl>

</user>

<user>

<id></id>

<username></username>

<passowrd></passwordl>

</user>

</users>

As can be seen from the above, it is possible to store data in an XML file and get the effect of data storage.
Several storage modes:
1. General TXT file------------access inconvenience
2. Database------------not necessary
3. XML technology-------------structure and clarity




Problem:
1. Query All Users
2. Query for the existence of the specified name user. If there is a return to the user complete information, the prompt does not.
3. Fuzzy query on user name
4. A fuzzy query on any field

Analysis:
return value:
The User--------NULL indicates that it does not exist.
Int,boolean----cannot represent a complete object.

Iuserdao:
User Getuserbyusername (String username)
List<user> Getalluser ();
list<user> getuserlistbykeyword (String keyword);

Userdao




Write XML data
1. Get a tree: Create from memory (empty tree)
2. Hang the tree root users 3. Hang the tree root to the tree roots user
4. Hang the branch of the branch Id,username,birth,gender
5. Set the value of the branch to the child
6. Save the tree on the hard drive.

Flush: Forces refreshing the buffered content to the hard drive.


Problem:
How to read properties.
Element.attribute (property name). GetValue ()



How to format the output XML file.
Set the standard of the output


Output format:
XMLWriter to FileWriter
Outputformat.createprettyprint
SetIndent

How to export Chinese.

Solve Chinese problem: replace Writer with OutputStream




Preparation: Java classes are introduced to encapsulate user data or to map relationships to XML user tags.

Documentmanager:
Unified Management Document Object

Convert to date format:
Date.valueof (String)

Problem: Use XML as a single table.


1. Read all the user information
2. Read the information of the user who specified the ID number.
3. Read the Count property to obtain the number of users. Attribute
4. Sort the Reading elements
Comparator to sort Objects collection
Interface: Comparator-----Set a sorting standard, compare
First: Turn the types of objects that are actually compared
Second: Remove the value of the true sort attribute.
-1----Ascending, +1----Descending
The sorted object can be a List, directly from the query result.
Collections. Sort (); associates the sorting criteria with the object being sorted.

Optimization: Random Control lifting sequence

Optimization: Sorting any field
5. Determine if the specified ID value exists.
6. Insert a user into the XML.
Analysis:
Find the Roots.
To determine whether a user exists by ID
If present, throw an exception and exit the program.
Building branches (based on user property values)
Hanging branches to the roots
Modify Tree root attribute Count
Write the tree back to the hard drive
Introduction of Userexistsexception
7. The primary key uses the self increasing way. (Get the maximum value plus 1)
Get the roots.
Get the user branch
Get all IDs, find maximum value
Id plus 1
8. Add a user, the primary key is self growth.
Get root, generate primary key, set user ID value
Get the Roots,
Building branches
Hanging branches to the roots
Gets the Count property and adds a
Write back the tree to reverse the hard drive.
9. Modify the Count property
Gets the value of the Count property in the root element
Determine whether to increase or decrease according to the needs of the external caller
Reset Count Property
10. Delete a user based on ID (find + DELETE)
Using the root element, locate the user element with the ID
Remove the user element from the root element
Get the Count property and write it back to the hard drive
The entire delete operation is considered successful only if it is deleted from the root and the disk is successfully saved.
11. Modify all attributes except IDs by ID. (Find + Modify)
Analysis:
Get the root element to find the corresponding user element based on the ID
Replaces all values except the ID.
Write back to the hard drive.

Summary:

As can be seen from the summary above. In fact, to achieve the above function is not difficult. I feel the biggest collection is the worry of the code. This is what I did before the project did not experience, at that time just learned the basis on the array, did not really want to make the project to reuse code. Today from Miss Lai's written code to see, if the code reuse properly, you can make the structure of classes to become hierarchical, one is to play the role of reuse, but also to make the code more readable.

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.