Translating Java objects and XML into each other

Source: Internet
Author: User

Castor-xml.jar Package

--castor can complete Java and the XML conversion of each other:

1) take advantage of mapping config, group Javaobject xml

we set it before mapping.xml xml Mapping.xml How are configuration files configured account What about this object

Mapping.xml configured as follows

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE mapping Public "-//exolab/castor mapping DTD Version 1.0//en" "Http://castor.org/mapping.dtd" >

<mapping>

<class name= "Com.hoo.entity.Account" auto-complete= "true" >

<map-to xml= "Account"/>

<field name= "id" type= "integer" >

<bind-xmlname= "id" node= "attribute"/>

</field>

<field name= "Name" type= "string" >

<bind-xml name= "name" node= "element"/>

</field>

<field name= "Email" type= "string" >

<bind-xml name= "Email" node= "element"/>

</field>

<field name= "Address" type= "string" >

<bind-xml name= "Address" node= "element"/>

</field>

<field name= "Birthday" type= "Com.hoo.entity.Birthday" >

<bind-xml name= " birthday " node= "element"/>

</field>

</class>

<class name= "Com.hoo.entity.Birthday" >

<map-to xml= "Birthday"/>

<field name= "Birthday" type= "string" >

<bind-xml name= "Birthday" node= "attribute"/>

</field>

</class>

</mapping>


2) Conversion Implementation Tool class


Import java.io.IOException;

Import Java.io.StringReader;

Import Java.io.StringWriter;


Import org.exolab.castor.mapping.Mapping;

Import org.exolab.castor.mapping.MappingException;

Import org.exolab.castor.xml.MarshalException;

Import Org.exolab.castor.xml.Marshaller;

Import Org.exolab.castor.xml.Unmarshaller;

Import org.exolab.castor.xml.ValidationException;

Import Org.springframework.core.io.ClassPathResource;

Import Org.springframework.core.io.Resource;

Import Org.xml.sax.InputSource;


Import Com.pinan.demo.example.dto.User;


public class Translatexmltools {


public static String convertdtotoxml (Object obj) throws Exception {

String returnstr = null;

try {

StringWriter SW = new StringWriter ();

Mapping Mapping = new Mapping ();

Resource input = new Classpathresource ("Castor-mapping-dto.xml");

Mapping.loadmapping (New InputSource (Input.getinputstream ()));

Marshaller Unmar = new Marshaller (SW);

Unmar.setmapping (mapping);

Unmar.marshal (obj);

Sw.flush ();

Returnstr = Sw.tostring ();

} catch (Exception ex) {

Ex.printstacktrace ();

}

return returnstr;

}


public static Object Convertxmltodto (String xmlstr) throws Exception {

if (null = = Xmlstr | | "". Equals (Xmlstr)) {

throw new Exception ("XML to DTO error, passed parameter is empty");

}

Mapping Mapping = new Mapping ();

Resource input = new Classpathresource ("Castor-mapping-dto.xml");

User user = null;

try {

Mapping.loadmapping (New InputSource (Input.getinputstream ()));

Unmarshaller Unmar = new Unmarshaller (user.class);

Unmar.setmapping (mapping);

user = (user) Unmar.unmarshal (new StringReader (XMLSTR));

return user;

} catch (Marshalexception ex) {

Ex.printstacktrace ();

} catch (Validationexception ex) {

Ex.printstacktrace ();

} catch (IOException ex) {

Ex.printstacktrace ();

} catch (Mappingexception ex) {

Ex.printstacktrace ();

} catch (Exception ex) {

Ex.printstacktrace ();

}

return user;

}


}


This article is from the "Small Li Guangzhi Blog" blog, please be sure to keep this source http://6817977.blog.51cto.com/6807977/1564303

Translating Java objects and XML into each other

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.