xmlutilities.java
来自「本程序是有IBM开发的一个基于数据表格的组件,里面有相关的例子和DOC,本站资料」· Java 代码 · 共 68 行
JAVA
68 行
package com.ibm.j2x.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;
/**
* The XMLUtilities provides helper methods for dealing with the Castor XML framework.
* @author MAbernethy
*
*/
public class XMLUtilities
{
/**
* Returns an Object from the specified xmlFileName, using the Castor XML
* parsing framework.
* @param xmlFileName the XML file containing the XML information
* @param mappingFileName the Castor mapping file used for decoding
* @param cls the base class to be decoded
* @return the Object created by the Castor XML parsing
*/
public static Object decode(String xmlFileName, String mappingFileName, Class cls)
{
try
{
InputStream stream = XMLUtilities.class.getClassLoader().getResourceAsStream(mappingFileName);
Mapping mapping = new Mapping();
mapping.loadMapping(new InputSource(stream));
stream.close();
Unmarshaller un = new Unmarshaller(cls);
un.setMapping(mapping);
InputStreamReader reader = new InputStreamReader(new FileInputStream(xmlFileName));
Object obj = un.unmarshal(reader);
reader.close();
return obj;
}
catch (MappingException ex)
{
ex.printStackTrace();
}
catch (MarshalException ex)
{
ex.printStackTrace();
}
catch (ValidationException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?