XML工具類(Java)

來源:互聯網
上載者:User
xml

XML工具類
package com.company.cpc.offlinelog.dao;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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();
  }
 }
}

 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.