XML Tools Class (Java)

Source: Internet
Author: User
Xml

XML Tool Classes
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;
Need to refer to Castor.jar file
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;
/**
* Class Name: Xmlfilemanager
* Content Summary: This class is an XML tool class
*/
public class Xmlfilemanager {
/**
* Method Name: objectlisttoxmlstring
* Content Summary: The object is composed of XML
* @param XML string for mappingxmlstring mapping
* @param containerclass Grouping class
* @return String returns 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 "";
}
Prepare mapping
Mapping Mapping = new Mapping ();
Reader reader = new StringReader (mappingxmlstring);
InputSource is = new InputSource (reader);
Mapping.loadmapping (IS);
Prepare writer
StringWriter writer = new StringWriter ();
Marshaller Marshaller = new Marshaller (writer);
Start grouping
Marshaller.setmapping (mapping);
Marshaller.setencoding ("gb2312");
Marshaller.marshal (Containerclass);
StringBuffer bf = Writer.getbuffer ();
Writer.close ();
return bf.tostring ();
}
/**
*
* Method Name: Xmltoobjectlist
* Content Summary: The XML Solution composition object
* @param XML string for mappingxmlstring mapping
* @param xmlstring XML string describing the data
* @return Object
* @throws IOException
* @throws mappingexception
* @throws marshalexception
* @throws validationexception
*/
public static Object Xmltoobjectlist (
String mappingxmlstring,
String xmlstring)
Throws IOException, Mappingexception, Marshalexception, validationexception {
Prepare mapping
StringReader mapingreader = new StringReader (mappingxmlstring);
InputSource is = new InputSource (Mapingreader);
Mapping Mapping = new Mapping ();
Mapping.loadmapping (IS);
Prepare reader
Reader reader = new StringReader (xmlstring);
Start Solution Group
Unmarshaller Unmarshaller = new Unmarshaller (mapping);
Object Containerclass = Unmarshaller.unmarshal (reader);
Reader.close ();
return containerclass;
}
/**
*
* Method Name: Savetoxmlfile
* Content Summary: The object is compiled into an XML file
* @param xmlfilename file name
* @param mappingfilename Mapping file name
* @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
}
Prepare mapping
Mapping Mapping = new Mapping ();
Mapping.loadmapping (Mappingfilename);
Prepare writer
File File = new file (xmlfilename);
Writer Writer = new FileWriter (file);
Marshaller Marshaller = new Marshaller (writer);
Start grouping
Marshaller.setmapping (mapping);
Marshaller.setencoding ("gb2312");
Marshaller.marshal (Containerclass);
Writer.close ();
}
/**
*
* Method Name: Loadfromxmlfile
* Content Summary: The XML file to solve the composition of the object
* @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 {
Prepare mapping
Mapping Mapping = new Mapping ();
Mapping.loadmapping (Mappingfilename);
Prepare reader
Reader reader = new FileReader (xmlfilename);
Start Solution Group
Unmarshaller Unmarshaller = new Unmarshaller (mapping);
Object Containerclass = Unmarshaller.unmarshal (reader);
Reader.close ();
return containerclass;
}
/**
*
* Method Name: readertostring
* Content Summary: Turn the data in the reader stream into a string
* @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 automatically generate catch blocks
E.printstacktrace ();
}
return sb.tostring ();
}
/**
* Method Name: objectlisttoxmlstring
* Content Summary: The object is composed of XML
* @param mappingfilename Mapping file name
* @param containerclass Grouping class
* @return String returns 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 "";
}

Prepare mapping
Mapping Mapping = new Mapping ();
Mapping.loadmapping (Mappingfilename);
Prepare writer
StringWriter writer = new StringWriter ();
Marshaller Marshaller = new Marshaller (writer);

Start grouping
Marshaller.setmapping (mapping);
Marshaller.setencoding ("gb2312");
Marshaller.marshal (Containerclass);
StringBuffer bf = Writer.getbuffer ();
Writer.close ();

return bf.tostring ();
}
/**
*
* Method Name: Xmltoobjectlist
* Content Summary: The XML Solution composition object
* @param mappingfilename Mapping file name
* @param xmlstring XML string describing the data
* @return
* @throws IOException
* @throws mappingexception
* @throws marshalexception
* @throws validationexception
*/
public static Object Xmlstrtoobjectlist (
String Mappingfilename,
String xmlstring)
Throws IOException, Mappingexception, Marshalexception, validationexception {
Prepare mapping
Mapping Mapping = new Mapping ();
Mapping.loadmapping (Mappingfilename);
Prepare reader
Reader reader = new StringReader (xmlstring);
Start Solution Group
Unmarshaller Unmarshaller = new Unmarshaller (mapping);
Object Containerclass = Unmarshaller.unmarshal (reader);
Reader.close ();
return containerclass;
}
/**
* Method Name: Xmltoobjectlist
* Content Summary: Get the absolute path file name of the resource file
* @param sresourcename Resource Name
* @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 ();
}
}
}



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.