Xml operations in java

Source: Internet
Author: User

Xml operations in java

1. XML Introduction
 

Xml is a scalable markup language. It is mainly used to mark data and define data types, and is very suitable for World Wide Web transmission.

Xml features:

Xml is a markup language. Similar to HTML xml, the design purpose is to transmit data, rather than display data. xml labels are not predefined, custom tag xml is designed to be self-descriptive. xml is the W3C recommendation standard xml and html comparison. XML is designed to transfer and store data, with the focus on data content. Html is designed to display data. Its focus is on the appearance of the data. Html is designed to display information, while xml is designed to transmit information. Example:
     
  
            JK.Rowing         
   
    2005
            
   
    35.2
       
  
 
An element can contain other elements, text, or a mixture of the two. The element can also have attributes. And All have element content, only text content, It also has attributes.   Ii. Reading xml using DomCreate a new java project and place a ages. xml file under the project root directory:
 
    
        
   
    java
         
   
    Eclipse
      
     
        
   
    Swift
         
   
    Xcode
      
     
        
   
    C#
         
   
    Visual Studio
      
  
 
Read xml files in Dom mode:
Import java. io. file; import java. io. IOException; import java. util. iterator; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; import org. xml. sax. SAXException; public class ReadXml {public static void main (String [] args) {try {// Dom DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse (new File ("language. xml "); Element root = document. getDocumentElement (); System. out. println ("cat =" + root. getAttribute ("cat"); NodeList list = root. getElementsByTagName ("lan"); for (int I = 0; I <list. getLength (); I ++) {Element lan = (Element) list. item (I); System. out. println ("------------------"); System. out. println ("id =" + lan. getAttribute ("id");/** Element name = (Element) * lan. getElementsByTagName ("name "). item (0); * System. out. println ("name =" + name. getTextContent (); Element * ide = (Element) lan. getElementsByTagName ("ide "). item (0); * System. out. println ("ide =" + ide. getTextContent (); */NodeList clist = lan. getChildNodes (); for (int j = 0; j <clist. getLength (); j ++) {Node c = clist. item (j); if (c instanceof Element) {System. out. println (c. getNodeName () + "=" + c. getTextContent () ;}}} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (SAXException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

Output:
cat=it------------------id=1name=javaide=Eclipse------------------id=2name=Swiftide=Xcode------------------id=3name=C#ide=Visual Studio
  3. Create XML in Dom Mode
 
Package ucas. yp. xml; import java. io. file; import java. io. stringWriter; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import javax. xml. transform. result; import javax. xml. transform. transformer; import javax. xml. transform. transformerConfigurationException; import javax. xml. transform. transformerException; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import org. w3c. dom. element; public class CreateXml {public static void main (String [] args) {try {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. newDocument (); Element root = document. createElement ("Ages"); root. setAttribute ("cat", "it"); Element lan1 = document. createElement ("lan"); lan1.setAttribute ("id", "1"); Element name1 = document. createElement ("name"); name1.setTextContent ("Java"); Element ide1 = document. createElement ("ide"); ide1.setTextContent ("Eclipse"); lan1.appendChild (name1); lan1.appendChild (ide1); // package Element lan2 = document from inside to outside. createElement ("lan"); lan2.setAttribute ("id", "2"); Element name2 = document. createElement ("name"); name2.setTextContent ("Swift"); Element ide2 = document. createElement ("ide"); ide2.setTextContent ("Xcode"); lan2.appendChild (name2); lan2.appendChild (ide2); Element lan3 = document. createElement ("lan"); lan3.setAttribute ("id", "3"); Element name3 = document. createElement ("name"); name3.setTextContent ("C #"); Element ide3 = document. createElement ("ide"); ide3.setTextContent ("Visual Studio"); lan3.appendChild (name3); lan3.appendChild (ide3); root. appendChild (lan1); root. appendChild (lan2); root. appendChild (lan3); document. appendChild (root); TransformerFactory trFactory = TransformerFactory. newInstance (); Transformer transformer = trFactory. newTransformer (); StringWriter writer = new StringWriter (); transformer. transform (new DOMSource (document), new StreamResult (writer); // ----- string output System. out. println (writer. toString (); // ----- generate the xml file transformer. transform (new DOMSource (document), new StreamResult (new File ("newxml. xml ");} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (TransformerConfigurationException e) {e. printStackTrace ();} catch (TransformerException e) {e. printStackTrace ();}}}
  4. Use Dom4j to operate xmlDom4j is an excellent java xml library with excellent performance, powerful functionality, and extremely easy to use features. : Http://www.dom4j.org/dom4j-1.6.1/
Package ucas. yp. xml; import java. io. fileWriter; import java. io. IOException; import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. export enthelper; import org. dom4j. io. XMLWriter; public class TestDom4j {public static void main (String [] args) {String xmlString ="
 
  
   JK. Rowing
   
    
2005
   
   
    
35.2
   
  
 "; Try {Document document = incluenthelper. parseText (xmlString); // print System. out. println (document. asXML (); // generate the xml file XMLWriter writer = new XMLWriter (new FileWriter ("book. xml "); writer. write (document); writer. close ();} catch (incluentexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
Refresh the project directory to see the newly generated book. xml.

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.