Conversion:
Convert string to document and then to XML
Convert from XML to document and then to string
View plaincopy to clipboardprint?
Convert XML to string:
Public static string xmlfile2string (string filename) throws saxexception, ioexception, parserconfigurationexception, transformerfactoryconfigurationerror, transformerexception
{
Documentbuilderfactory = documentbuilderfactory. newinstance ();
Inputsource = new inputsource (filename );
Document document = documentbuilderfactory. newdocumentbuilder (). parse (inputsource );
Stringwriter Sw = new stringwriter ();
Transformer serializer = transformerfactory. newinstance (). newtransformer ();
Serializer. Transform (New domsource (document), new streamresult (SW ));
Return Sw. tostring ();
}
Convert Doc to string:
Domsource = new domsource (DOC );
Stringwriter writer = new stringwriter ();
Streamresult result = new streamresult (writer );
Transformerfactory TF = transformerfactory. newinstance ();
Transformer transformer = TF. newtransformer ();
Transformer. Transform (domsource, result );
System. Out. println (writer. tostring ());
Convert Doc to XML:
Transformerfactory tfactory = transformerfactory. newinstance ();
Transformer transformer = tfactory. newtransformer ();
Domsource source = new domsource (DOC );
Fileoutputstream outstream = new fileoutputstream ("OUTFILE. xml ");
Streamresult result = new streamresult (outstream );
Transformer. Transform (source, result );
Doc initialization:
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = NULL;
Try {
DB = DBF. newdocumentbuilder ();
} Catch (parserconfigurationexception PCE ){
System. Err. println (PCE );
System. Exit (1 );
}
Document Doc = NULL;
Doc = dB. newdocument ();
Convert XML into string by reading the file:
Fileinputstream in = new fileinputstream ("datatest. xml ");
Bufferedinputstream Bufferin = new bufferedinputstream (in );
Byte C [] = new byte [1000];
Int n = 0;
Stringbuffer BS = new stringbuffer ();
While (n = Bufferin. Read (c ))! =-1)
{String temp = new string (C, 0, n );//
BS. append (temp );
}
Convert XML to string:
Public static string xmlfile2string (string filename) throws saxexception, ioexception, parserconfigurationexception, transformerfactoryconfigurationerror, transformerexception
{
Documentbuilderfactory = documentbuilderfactory. newinstance ();
Inputsource = new inputsource (filename );
Document document = documentbuilderfactory. newdocumentbuilder (). parse (inputsource );
Stringwriter Sw = new stringwriter ();
Transformer serializer = transformerfactory. newinstance (). newtransformer ();
Serializer. Transform (New domsource (document), new streamresult (SW ));
Return Sw. tostring ();
}
Convert Doc to string:
Domsource = new domsource (DOC );
Stringwriter writer = new stringwriter ();
Streamresult result = new streamresult (writer );
Transformerfactory TF = transformerfactory. newinstance ();
Transformer transformer = TF. newtransformer ();
Transformer. Transform (domsource, result );
System. Out. println (writer. tostring ());
Convert Doc to XML:
Transformerfactory tfactory = transformerfactory. newinstance ();
Transformer transformer = tfactory. newtransformer ();
Domsource source = new domsource (DOC );
Fileoutputstream outstream = new fileoutputstream ("OUTFILE. xml ");
Streamresult result = new streamresult (outstream );
Transformer. Transform (source, result );
Doc initialization:
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = NULL;
Try {
DB = DBF. newdocumentbuilder ();
} Catch (parserconfigurationexception PCE ){
System. Err. println (PCE );
System. Exit (1 );
}
Document Doc = NULL;
Doc = dB. newdocument ();
Convert XML into string by reading the file:
Fileinputstream in = new fileinputstream ("datatest. xml ");
Bufferedinputstream Bufferin = new bufferedinputstream (in );
Byte C [] = new byte [1000];
Int n = 0;
Stringbuffer BS = new stringbuffer ();
While (n = Bufferin. Read (c ))! =-1)
{String temp = new string (C, 0, n );//
BS. append (temp );
}
To convert string to document, see the following link.
Http://www.blogjava.net/hopeshared/archive/2006/07/06/56919.aspx
For information about how to convert an XSL file to XML, see:
Http://blog.csdn.net/luweifeng1983/archive/2009/03/25/4022431.aspx
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/luweifeng1983/archive/2009/03/26/4028250.aspx