JAVA-XML and Database Exchange study Note 3

Source: Internet
Author: User

Requirements

    Required knowledge

    Basic JAVA knowledge, basic XML knowledge, and basic database operations.

  • Development Environment

    MyEclipse10/MySql5.5

  • Data download

    Source code download

 

Databases have absolute advantages in data query, modification, storage, and security compared with other data processing methods. XML files are tag-based text files with good compatibility and ease of organization, parse and exchange data. In some cases, we need to introduce XML data into the database to give full play to the database's advantages in data management. On the other hand, we need to introduce the data in the database into the XML file, in order to interact with other systems, take advantage of the advantages of XML files in data exchange. Next I will introduce the common DOM and Dom4J, two ways to achieve mutual conversion between XML and database.

 

Dom4j implements mutual conversion between XML and databases:

English 6 vocabulary xml File Download

===== "Insert into 'xmlanddb'. 'dict '('id', 'word', 'meaning', 'lx') VALUES (NULL ,?, ?, ?); "= Dom4j. load ("dict. xml "= root. elements ("word"); String wordStr = "", meaningStr = "", lxStr = "" (Iterator I = (Element) I. next (); (Iterator k = word. elementIterator (); k. hasNext ();) {Element element = (element. getName (). equals ("name" = (element. getName (). equals ("mean" = (element. getName (). equals ("lx" = 123

 

Dom4j (Click here if you do not know dom4j) to import the database records to the xml file: DatabaseToXML. java

   ====="SELECT * FROM  `dict`"== document.addElement("dict");                         === root.addElement("word"= word.addElement("name""word"=word.addElement("mean""meaning"=word.addElement("lx""lx"= XMLWriter( FileWriter( File("dict2.xml"  

 

 

Common Dom (Click here if you do not know about the dom parser in java) to implement mutual conversion between XML and database:

Student2.xml

===== "Insert into 'xmlanddb '. 'Student '('number', 'name', 'date', 'height') VALUES (?,?,?,?); "=); DocumentBuilder domParser = domParser. parse (File ("student2.xml"); Element root = root. getElementsByTagName ("student ID" = root. getElementsByTagName ("name" = root. getElementsByTagName ("Date of Birth" = root. getElementsByTagName ("height" size = list1.getLength (); String [] number = String [4 = String [4 = String [4 [] height = [4 (k = 0; k <size; k ++ ======== 1234 ==== "SELECT * FROM 'student '" = 1) + rs. getString (2) + rs. getString (3) + rs. getDouble (4

 

Dom (the dom parser in java does not know, you can click here) to import the database records to the xml file: DatabaseToXMLWithDom. java

= {"" = "" = "SELECT * FROM 'student '" = recordAmount = rs. getRow (); number = k = 0 = rs. getString (1 = rs. getString (2 = rs. getString (3 = String. valueOf (rs. getDouble (4 ++ =); = domParser. newDocument (); document. setXmlVersion ("1.0"); Element root = document. createElement ("student information" (k = 0; k <name. length; k ++ = document. createElement ("student" = document. createElement ("student ID" = document. createElement ("name" = document. createElement ("Date of Birth" = document. createElement ("height" = TransformerFactory. newInstance (); Transformer transformer = transFactory. newTransformer (); DOMSource domSource = File ("newXML2.xml" =


For more information, see:

JAVA and DOM parser basic learning notes JAVA and DOM parser improved (DOM/SAX/JDOM/DOM4j/XPath) Learning Note 2

 

Author: Li-Cheng Source: The copyright of this article is shared by the author and the blog Park. You are welcome to reprint it. However, you must keep this statement without the author's consent and provide a connection to the original text on the article page, otherwise, you are entitled to pursue legal liability.

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.