A detailed approach to Java parsing XML format data _java

Source: Internet
Author: User

Initially, the XML language was only intended to be used as a substitute for the HTML language, but as the language developed and perfected, more and more people found its advantages: for example, markup languages can be extended, strict grammatical rules can be used, and meaningful tags The advantages of content storage and performance separation are destined for the language from the date of birth will be brilliant. The XML language has entered a period of rapid development after becoming the standard of the consortium, of course, it has a series of advantages and advantages of the major technology manufacturers are destined to its preference, Java as a software industry development technology has responded quickly, there are a variety of XML support Tools, This article will introduce some of the mainstream Java processing XML technology in this perspective, hoping to help you.
What are some of the great libraries and tools in Java that make it easier for programmers to process XML?

Introduction to

XML three parsing methods
Famous DOM
say it's famous but it's not too much, DOM is the standard API for the process XML, and it is the basis for many other standards related to XML processing, not just Java, Other languages such as javascript,php,ms. NET and so on have implemented this standard and become the most widely used XML processing method. Of course, in order to provide more powerful features, Java has a lot of direct extensions to the DOM, such as many Java programmers are familiar with the jdom,dom4j, and so on, they are essentially extensions to the DOM interface functions, retaining a lot of Dom API features, many of the original do M programmers have mastered the use of the other two without any obstacle, intuitive, easy to operate the way to make it popular with the vast number of Java programmers.
Green sax
The emergence of Sax has its special needs and why it is green, because sax supports XML processing with minimal system resources and the fastest parsing. But the cumbersome search method also brings a lot of trouble to programmers, often a headache, and its support for XPath query function, make people love and hate it.
obscure digester:xml
digester is an open source project under the Apache Fund, and the author's understanding of it stems from a study of the Struts framework, whether there are many programmers Want a solution to the design of the major open source framework even if you want to write a powerful framework, you will encounter the dilemma: these various XML language tags in the framework configuration file, the framework is the bottom of what technology to parse it? DOM parsing time-consuming, SAX parsing is too cumbersome, and each parsing system overhead will be too large, so, we think of the need to use the XML structure corresponding to the JavaBean to load the information, thus the emergence of digester. Its appearance has brought the convenient operation interface for the XML transformation to the JavaBean object's demand, has made the more similar demand to obtain the more perfect solution, no longer needs the programmer to implement this kind of tedious analytic procedure. At the same time, SUN also launched the XML and JavaBean conversion Tool class JAXB, interested readers can learn by themselves.


To see an example of a Java DOM parsing xml:

Import java.io.*;  
Import org.w3c.dom.*;  
Import org.xml.sax.SAXException;  
Import javax.xml.parsers.*; public class Xml {public static void main (string[] args) {Documentbuilderfactory factory = Documentbuilderfact  
    Ory.newinstance ();  
      try {documentbuilder builder = Factory.newdocumentbuilder ();  
      Document doc = builder.parse (new File ("E:\\work\\test\\xml1.xml"));  
      NodeList nl = doc.getelementsbytagname ("book");  
        for (int i = 0; i < nl.getlength (); i++) {System.out.println (Nl.item (i) getattributes (). Item (0));  
        System.out.println (Doc.getelementsbytagname ("title"). Item (i). Getfirstchild (). Getnodevalue ());  
        System.out.println (Doc.getelementsbytagname ("title"). Item (i). GetAttributes (). Item (0));  
        System.out.println (Doc.getelementsbytagname ("author"). Item (i)-getfirstchild (). Getnodevalue ()); System.out.println (Doc.getelementsbytagname ("year"). Item (i) 
            . Getfirstchild (). Getnodevalue ());  
        System.out.println (Doc.getelementsbytagname ("price"). Item (i). Getfirstchild (). Getnodevalue ());  
      System.out.println ();  
    } catch (Parserconfigurationexception e) {e.printstacktrace ();  
    catch (Saxexception e) {e.printstacktrace ();  
    catch (IOException e) {e.printstacktrace ();  
 }  
  }  
}

Xml:

<bookstore> 
<book category= "Cooking" > 
 <title lang= "en" >everyday italian</title> 
 <author>giada De laurentiis</author> 
 <year>2005</year> 
 <price>30.00 </price> 
</book> 
<book category= "CHILDREN" > 
 <title lang= "en" >harry /title> 
 <author>j K. rowling</author> 
 <year>2005</year> 
 <price> 29.99</price> 
</book> 
<book category= "WEB" > 
 <title lang= "en" >learning XML </title> 
 <author>erik T. ray</author> 
 <year>2003</year> 
 <price >39.95</price> 
</book> 
</bookstore>

Results:

category= "Cooking"
Everyday Italian
lang= "en"
giada De Laurentiis
-
30.00

CHILDREN "
Harry Potter
lang=" en "
J. K. Rowling
29.99

category=" WEB "
Learning XML
lang= "en"
Erik T. Ray
2003
39.95

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.