首页 > 代码库 > java对象和xml相互转换

java对象和xml相互转换

Castor-xml.jar

——Castor可以完成JavaXML的相互转换:

1)利用mapping配置,编组JavaObject、解组XML

在此之前我们设置过mapping.xml。如果不设置,肯定是不能转换成我们想要的XML的。那么,mapping.xml配置文件是怎么配置Account这个对象的呢

mapping.xml配置如下

<?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="生日" 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)转换实现工具类


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转dto报错,传入参数为空");

}

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;

}


}


本文出自 “小李广之博客” 博客,请务必保留此出处http://6817977.blog.51cto.com/6807977/1564303

java对象和xml相互转换