• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

XML工具类(Java)_xml

xml 搞代码 7年前 (2018-06-15) 164次浏览 已收录 0个评论

xml工具类
package com.company.cpc.offlinelog.dao;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

http://www.gaodaima.com/33862.htmlXML工具类(Java)_xml

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
//需要引用castor.jar文件
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.xml.sax.InputSource;
import com.zte.ecc.util.tracer.Debug;
/**
 *  类 名 称:XmlFileManager
 *  内容摘要:该类是XML工具类
 */
public class XmlFileManager {
 /**
  * 方法名称:objectListToXMLString
  * 内容摘要:把对象编组成XML
  * @param mappingXMLString 映射的XML串
  * @param containerClass   编组类
  * @return String 返回XML
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static String objectListToXMLString(
  String mappingXMLString,
  Object containerClass)
  throws IOException, MappingException, MarshalException, ValidationException {
  if (containerClass == null) {
   Debug.println(“containerClass  is NULL!!!!!”);
   return “”;
  }
  //准备Mapping
  Mapping mapping = new Mapping();
  Reader reader = new StringReader(mappingXMLString);
  InputSource is = new InputSource(reader);
  mapping.loadMapping(is);
  //准备Writer
  StringWriter writer = new StringWriter();
  Marshaller marshaller = new Marshaller(writer);
  //开始编组
  marshaller.setMapping(mapping);
  marshaller.setEncoding(“gb2312”);
  marshaller.marshal(containerClass);
  StringBuffer bf = writer.getBuffer();
  writer.close();
  return bf.toString();
 }
 /**
  *
  * 方法名称:XmlToObjectList
  * 内容摘要:把XML解组成对象
  * @param mappingXMLString 映射的XML串
  * @param xmlString 描述数据的XML串
  * @return Object
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static Object XmlToObjectList(
  String mappingXMLString,
  String xmlString)
  throws IOException, MappingException, MarshalException, ValidationException {
  //准备Mapping
  StringReader mapingReader = new StringReader(mappingXMLString);
  InputSource is = new InputSource(mapingReader);
  Mapping mapping = new Mapping();
  mapping.loadMapping(is);
  //准备Reader
  Reader reader = new StringReader(xmlString);
  //开始解组
  Unmarshaller unmarshaller = new Unmarshaller(mapping);
  Object containerClass = unmarshaller.unmarshal(reader);
  reader.close();
  return containerClass;
 }
 /**
  *
  * 方法名称:saveToXMLFile
  * 内容摘要:把对象编组成XML文件
  * @param xmlFileName 文件名
  * @param mappingFileName  映射文件名
  * @param containerClass 
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static void saveToXMLFile(
  String xmlFileName,
  String mappingFileName,
  Object containerClass)
  throws IOException, MappingException, MarshalException, ValidationException {
  if (containerClass == null) {
   Systen.out.println(“containerClass  is NULL!!!!!”);
   return;
  }
  //准备Mapping
  Mapping mapping = new Mapping();
  mapping.loadMapping(mappingFileName);
  //准备Writer
  File file = new File(xmlFileName);
  Writer writer = new FileWriter(file);
  Marshaller marshaller = new Marshaller(writer);
  //开始编组
  marshaller.setMapping(mapping);
  marshaller.setEncoding(“gb2312”);
  marshaller.marshal(containerClass);
  writer.close();
 }
 /**
  *
  * 方法名称:loadFromXMLFile
  * 内容摘要:把XML文件解组成对象
  * @param xmlFileName
  * @param mappingFileName
  * @return
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static Object loadFromXMLFile(
  String xmlFileName,
  String mappingFileName)
  throws IOException, MappingException, MarshalException, ValidationException {
  //准备Mapping
  Mapping mapping = new Mapping();
  mapping.loadMapping(mappingFileName);
  //准备Reader
  Reader reader = new FileReader(xmlFileName);
  //开始解组
  Unmarshaller unmarshaller = new Unmarshaller(mapping);
  Object containerClass = unmarshaller.unmarshal(reader);
  reader.close();
  return containerClass;
 }
 /**
  *
  * 方法名称:readerToString
  * 内容摘要:把Reader流中的数据变为字符串
  * @param reader
  * @param bfferSize
  * @return
  */
 public static String readerToString(Reader reader, int bfferSize) {
  StringBuffer sb = new StringBuffer();
  char[] b = new char[bfferSize];
  int n = 0;
  try {
   while ((n = reader.read(b)) > 0) {
    System.out.println(“read:” + n);
    sb.append(b, 0, n);
   }
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  return sb.toString();
 }
 /**
  * 方法名称:objectListToXMLString
  * 内容摘要:把对象编组成XML
  * @param mappingFileName 映射文件名
  * @param containerClass   编组类
  * @return String 返回XML
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static String objectListToXMLStr(
  String mappingFileName,
  Object containerClass)
  throws IOException, MappingException, MarshalException, ValidationException {
  if (containerClass == null) {
   Debug.println(“containerClass  is NULL!!!!!”);
   return “”;
  }
 
  //准备Mapping
  Mapping mapping = new Mapping();
  mapping.loadMapping(mappingFileName);
  //准备Writer
  StringWriter writer = new StringWriter();
  Marshaller marshaller = new Marshaller(writer);
 
  //开始编组
  marshaller.setMapping(mapping);
  marshaller.setEncoding(“gb2312”);
  marshaller.marshal(containerClass);
  StringBuffer bf = writer.getBuffer();
  writer.close();
 
  return bf.toString();
 }
 /**
  *
  * 方法名称:XmlToObjectList
  * 内容摘要:把XML解组成对象
  * @param mappingFileName 映射文件名
  * @param xmlString 描述数据的XML串
  * @return
  * @throws IOException
  * @throws MappingException
  * @throws MarshalException
  * @throws ValidationException
  */
 public static Object XmlStrToObjectList(
  String mappingFileName,
  String xmlString)
  throws IOException, MappingException, MarshalException, ValidationException {
  //准备Mapping
  Mapping mapping = new Mapping();
  mapping.loadMapping(mappingFileName);
  //准备Reader
  Reader reader = new StringReader(xmlString);
  //开始解组
  Unmarshaller unmarshaller = new Unmarshaller(mapping);
  Object containerClass = unmarshaller.unmarshal(reader);
  reader.close();
  return containerClass;
 }
 /**
  * 方法名称:XmlToObjectList
  * 内容摘要:得到资源文件的绝对路径文件名
  * @param sResourceName 资源名称
  * @return String
  */
 public static String getResourceFilePath(String sResourceName) {
  if (!sResourceName.startsWith(“/”)) {
   sResourceName = “/” + sResourceName;
  }
  java.net.URL classUrl = XmlFileManager.class.getResource(sResourceName);
  if (classUrl == null) {
   System.out.println(
    “/nResource ‘”
     + sResourceName
     + “‘ not found in /n'”
     + System.getProperty(“java.class.path”)
     + “‘”);
   return null;
  } else {
   System.out.println(
    “/nResource ‘”
     + sResourceName
     + “‘ found in /n'”
     + classUrl.getFile()
     + “‘”);
   return classUrl.getFile();
  }
 }
}

 

欢迎大家阅读《XML工具类(Java)_xml》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:XML工具类(Java)_xml
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址