A small example of Java operation XML

Source: Internet
Author: User
Tags gettext

"Reproduced, convenient for later use"

The original operation of XML has been used this package: Xstream-1.4.2.jar. And then, in the way of annotations, it is convenient, as long as the definition of the bean's hierarchy is OK, the third-party package will automatically generate and parse the XML.

But today we found that this package has two problems: one is more complicated (I am a rookie, please don't spray, I really think he is quite complicated.) )。 For example, if you want to deal with both attributes and values of XML is more troublesome. (@XStreamConverter Note This method reported the error: com.thoughtworks.xstream.InitializationException). The search said it was a packet conflict.

XStream biggest problem personal feel is the need for too many classes ... Maybe our company's XML format is not very canonical ...  However, this is a legacy of history, I personally do not have the means. Like such an XML:

<a>    <b>        <c>            <d>                 xxx</d></c></b></a>

If you use XStream, it is necessary to declare 4 classes (he is not familiar with, if anyone knows can be a class to get the advice), and with dom4j words: e.addelement ("a"). AddElement ("B"). AddElement ("C"). AddElement ("D"). SetText ("xxx");

This article uses dom4j to implement a simple bean-to-XML, and XML-to-bean interchange.

First, it is the bean:

Class bean{        private String name;        Private String Nameparam;        Private String age;        Private String Ageparam;        The public Bean (string xml) {        //Here is the constructor method, which is converted from XML to Bean        } public        String ToXML () {               //Here is the method that the bean is converted to XML        } }

The XML is:

<Student>    <name namep= "This is the property of name" > Zhang San </name>    <age agep= "This is The Age attribute" >12</age> </Student>

The methods for converting XML into beans are:

{    //Get Document Object Document Document    = Documenthelper.parsetext (XML);    Gets the root node (that is, the student node)    Element el = document.getrootelement ();    Gets the node under the root node:    Element name = el.element ("name");    THIS.name = Name.gettext ();    This.nameparam = Name.attributevalue ("NAMEP");    Element age= el.element ("Age");    This.age = Age.gettext ();    This.ageparam = Age.attributevalue ("Agep");    }

ToXml Method:

Get Document Object        Document DOC = Documenthelper.createdocument ();        Create root node:        Element Rootele = doc.addelement ("Student");        Create child node:        Element name = rootele.addelement ("name");        Name.addattribute ("NAMEP", This.nameparam);        Name.settext (this.name);        Element age = rootele.addelement ("Age");        Age.addattribute ("Agep", This.ageparam);        Age.settext (this.age);        return Doc.asxml ();

Test code:

public void Test () {String xml = "<student><name namep=\" This is the property of name \ "> Zhang San </name><age agep=\" This is The age attribute \ ">12</age></Student>";        Bean B = new bean (XML);        System.out.println ("Name:\t" +b.getname () + "\tnamepra:\t" +b.getnameparam ());        System.out.println ("Age:\t" +b.getage () + "\tagepra:\t" +b.getageparam ());        System.out.println ("Regenerating into XML:");        System.out.println (B.toxml ());}

Results:

Name:    Zhang San    namepra:    This is the attribute of name    Age:    Agepra:    This is the aging attribute regenerated into Xml:<?xml version= "1.0" Encoding= "UTF-8"? ><student><name namep= "This is the attribute of name" > Zhang San </name><age agep= "This is The Age attribute" >12 </age></Student>

Personal Summary:

First, dom4j and XStream are very good third-party packages that deal with XML. They each have their own merits.

The advantage of XStream is that configuration can be used instead of code. XML does not have to be spelled out, so the odds of making a mistake are smaller (and without writing so long a spelling of the XML statement) but flexibility can be sacrificed.

The advantages of dom4j are simple, especially simple and flexible. XML is a self-spelling, and parsing is also its own node to parse. It's especially easy to use, but it's really all the XML you're spelling. It's more troublesome and error prone.

A small example of Java operation XML

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.