JSP for XML file Manipulation Techniques Example Analysis _jsp programming

Source: Internet
Author: User
Tags xpath

This article illustrates the JSP's manipulation techniques for XML files. Share to everyone for your reference, specific as follows:

XML (extensible Markup Language) Extensible Markup Language, a basic knowledge that has been learned earlier. And this tutorial why put it under the Java EE, because he is also one of the 13 specifications of Java EE, although XML is the World Wide Web Organization consortium designated, and do the specification, so it is also a specification, we must follow the norms, and JDBC,SERVLET,JSP,EJB, and so on.

The preceding article has introduced some of the basics of XML, primarily to store and transmit data, but how do we get the data, which means how to parse XML? Here's a summary.

There are usually two ways to parse an XML file first: Dom parsing and sax parsing, first looking at the concepts and basics of both:

One, principle:

Dom parsing: When the program begins to execute, the entire XML file is loaded into memory, a DOM tree is formed in memory, and then any node in the tree is searched for by some programming language.
Sax parsing: An event-driven approach to parsing. Parsing is sequential, sequential: From left to right, from top to bottom.
Event-driven parsing does not require all of the XML files to be loaded into memory, so this approach does not consume a lot of memory,
Just parse the past nodes can not be resolved again, not flexible enough, if you want to resolve, can only start from the XML file header again.

Two, the advantages and disadvantages of the two:

DOM parsing: Advantages: flexibility. Because the entire tree is in memory, we can at any time to operate on a node, parsing the past nodes can be resolved again, more flexible.

Disadvantage: If the XML file is large, it consumes a lot of memory because the XML file is large and requires access to
And few nodes.
Summary: So the XML file is smaller and more nodes need to be resolved, so it's worth using DOM parsing.

Sax parsing: Benefits: It doesn't consume a lot of memory.
Disadvantage: Inflexible (we can use another technical XPath, which can be used to quickly locate an XML file to
Resolved node).
Summary: With XPath technology, sax parsing is what we use.

third, in Java, Java's JDK provides parsing of XML : org.w3c.dom.*, which is the implementation of Sun's code for the consortium. But efficiency is not high enough. Therefore, we often use third-party components, such as dom4j, and so on, relatively high efficiency.

Four, OK, let's take a look at the example:

1, using the Java JDK provided to parse read XML file: To see two blog written in detail: Java DOM parsing, Java sax parsing.

2, here to write, using dom4j parsing method, in fact very similar, but I feel dom4j method name, attribute name may be more easy to use:

A, read the XML file (this is based on the Sax parsing method):

public static void Main (string[] args) throws exception{ 
    //Create SAX Parser object 
    Saxreader reader = new Saxreader (); 
    Reading XML file Document 
    = Reader.read (new file ("Db-config.xml")); 
    Gets the root element 
    rootelement = Document.getrootelement (); 
    System.out.println ("The name of the root node:" + Rootelement.getname ()); 
    Gets the child nodes under the root node driver 
    Element driverelement = rootelement.element ("Driver"); 
    String Driver = Driverelement.gettext (); 
    SYSTEM.OUT.PRINTLN (driver); 
    Gets the child node URL under the root node 
    String url = rootelement.elementtext ("url"); 
    System.out.println (URL); 
    Gets the child node under the root node user 
    String user = Rootelement.elementtext ("user"); 
    SYSTEM.OUT.PRINTLN (user); 
    Gets the child nodes under the root node password 
    String password = rootelement.elementtext ("password"); 
    SYSTEM.OUT.PRINTLN (password); 
} 

b, write the file, is from the top down, we dom4j in the DOM parsing way:

public static void Main (string[] args) throws exception{ 
    //Create a Document object in memory first 
    Documenthelper.createdocument (); 
    Constructs the document tree 
    Element stuinfoelement = document.addelement ("Mathematics specialized book"); 
    Element stuElement1 = stuinfoelement.addelement ("book"); 
    Stuelement1.addattribute ("id", "the"); 
    Element nameElement1 = stuelement1.addelement ("title"); 
    Nameelement1.settext ("Modern Algebra"); 
    Element StuElement2 = stuinfoelement.addelement ("book"); 
    Stuelement2.addattribute ("id", "the"); 
    Element NameElement2 = stuelement2.addelement ("title"); 
    Nameelement2.settext ("Higher algebra"); 
    Set the character encoding mode 
    OutputFormat format = Outputformat.createprettyprint (); 
    Format.setencoding ("GB18030"); 
    Start writing 
    XMLWriter XMLWriter = new XMLWriter (New FileWriter ("Students.xml"), format); 
    Xmlwriter.write (document); 
    Xmlwriter.close (); 
} 

The last document written:

<?xml version= "1.0" encoding= "GB18030"?> 
 < math major books > 
  < id= "" > 
    < title > John </Title > 
  </> 
 < id= "the" > 
    < title > John </title > 
  </book > 
 </math Major book > 

In a nutshell, for XML parsing simple summary, of course, will encounter very complex XML file, we can try to write these parsing their files, of course, can also use others to write good, here just to learn more, understand their essence.

I hope this article will help you with JSP program design.

Related Article

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.