A small example of Java operation XML

Source: Internet
Author: User
Tags gettext

The last two days the company is more, these two days to deal with the main XML, and today, a little bit of Java to manipulate XML a small example.

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:

classbean{PrivateString name; PrivateString Nameparam; PrivateString age; PrivateString Ageparam;  PublicBean (String xml) {//here is the constructor method, which is converted from XML to Bean        }         PublicString ToXML () {//Here's how the bean transforms into XML        }}

The XML is:

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

The methods for converting XML into beans are:

{    //Get Document ObjectDocument document =documenthelper.parsetext (XML); //Gets the root node (that is, the student node)Element el =document.getrootelement (); //get 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 ObjectDocument doc =documenthelper.createdocument (); //To create the root node:Element Rootele = doc.addelement ("Student"); //To Create a 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); returnDoc.asxml ();

Test code:

 Public void  = "<student><name namep=\" This is the property of name \ "> Zhang San </name><age agep=\" This is the age attribute \ ">12</age ></Student> ";         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 property of name" > Zhang San </name><age agep= " This is the attribute of age ">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.