Java Operation Xml--jdom Use detailed __java

Source: Internet
Author: User
Java Operation Xml--jdom Use detailed

Jdom is an open source project, which is based on a tree structure that utilizes pure Java technology to parse, generate, serialize, and manipulate XML documents. Jdom Introduction

Jdom directly into a service for Java. It leverages the many features of the more powerful Java language (method overload, set concepts, and mappings) to effectively combine the functionality of Sax and DOM. Hide the complexity of the original XML process as much as possible with the design. Using Jdom to process XML documents is a simple and easy thing to do.

JDOM was developed by Brett McLaughlin and Jason Hunter in the spring of 2000 to make up for the shortcomings of Dom and sax in practical applications.

These deficiencies are mainly due to the fact that SAX does not have document modification, random access, and output functionality, and for DOM, Java programmers are not very convenient to use.

The main disadvantage of DOM is that the DOM is an interface Definition language (IDL), and its task is to achieve unity in different languages. is not specifically designed for Java. Jdom Package Overview

Jdom is composed of the following packages

Package Name explain
Org.jdom Java class that contains all the elements of an XML document
Org.jdom.adapters Contains Java classes that fit with the DOM
Org.jdom.filter Filter classes that contain XML documents
Org.jdom.input Contains classes that read XML documents
Org.jdom.output Contains a class that writes to an XML document
Org.jdom.transform Contains interfaces to convert Jdom XML document interfaces to other XML documents
Org.jdom.xpath Class containing XPath operations on XML documents
The case is generated by jdom XML
Package xmltest;

Import Java.io.FileOutputStream;

Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.output.Format;
Import Org.jdom.output.XMLOutputter;

public class generatejdom{public
    static void Main (string[] args) throws exeception{
        Document doc = new document () ;
        element root = new Element ("root");
        Doc.addcontent (root);

        element name = new Element ("name");
        Root.addcontent (name);
        Root.setattribute ("Author", "Yanzhelee"). setattribute ("url", "http://www.csdn.com");

        Name.addcontent ("Yanzhelee");

        Xmloutputter out = new Xmloutputter ();
        Format format = Format.getprettyformat ();
        Format.setindent ("  ");
        Out.setformat (format);
        Out.output (Doc, New FileOutputStream ("Jdom.xml"));
    }

The following is the generated XML

<?xml version= "1.0" encoding= "UTF-8"?> <root author=
"Yanzhelee" url= "http://www.csdn.com" >
    <name>yanzhelee</name>
</root>
parsing XML documents through Jdom
Package xmltest;
/** * @author ciacs * * Import java.io.File;
Import Java.io.FileOutputStream;

Import java.util.List;
Import Org.jdom.Attribute;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;

Import Org.jdom.output.XMLOutputter;
        public class parsejdom{public static void Main (string[] args) throws exeception{//parsing XML through Saxbuilder

        Saxbuilder builder = new Saxbuilder ();
        Document doc = builder.build (new File ("Jdom.xml"));

        Element root = Doc.getrootelement ();
        System.out.println (Root.getname ());

        String name = Root.getchild ("name"). GetText ();

        System.out.println ("Name:" +name);

        List attrs = Root.getattributes ();
            for (int i = 0; i < attrs.size (); i++) {String attrname;
            String AttrValue;
            Attribute attr = (attribute) attrs.get (i);
            Attrname = Attr.getname ();
            AttrValue = Attr.getvalue (); System.ouT.println (attrname+ ":" +attrvalue);

        //delete the property URL and save it to Jdom2.xml root.removeattribute ("url");
        Xmloutputter out = new Xmloutputter ();
    Out.output (Doc, New FileOutputStream ("Jdom2.xml")); }

}
maven Dependencies
<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
</dependency>
Reference Blog

Https://www.cnblogs.com/zhi-hao/p/4016363.html

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.