Package com. vmm. xml;
Import java. Io. bytearrayoutputstream;
Import java. Io. stringreader;
Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. parsers. parserconfigurationexception;
Import javax. xml. Transform. result;
Import javax. xml. Transform. source;
Import javax. xml. Transform. transformer;
Import javax. xml. Transform. transformerconfigurationexception;
Import javax. xml. Transform. transformerexception;
Import javax. xml. Transform. transformerfactory;
Import javax. xml. Transform. Dom. domsource;
Import javax. xml. Transform. Stream. streamresult;
Import org. W3C. Dom. Document;
Import org. xml. Sax. inputsource;
Public class xmlfactory {
Private Static documentbuilderfactory domfac;
Private Static documentbuilder dombuilder;
Private Static xmlfactory instance;
Private Static transformerfactory tranfactory;
Private Static transformer atransformer;
Private xmlfactory (){
Domfac = documentbuilderfactory. newinstance ();
Tranfactory = transformerfactory. newinstance ();
Try {
Dombuilder = domfac. newdocumentbuilder ();
Atransformer = tranfactory. newtransformer ();
} Catch (parserconfigurationexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (transformerconfigurationexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public static xmlfactory getinstance (){
If (instance = NULL ){
Instance = new xmlfactory ();
}
Return instance;
}
Public String getxmlstring (document DOC ){
String ret;
Source src = new domsource (DOC );
Bytearrayoutputstream outputstream = new bytearrayoutputstream ();
Result DEST = new streamresult (outputstream );
Try {
Atransformer. Transform (SRC, DEST );
} Catch (transformerexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
Return NULL;
}
Ret = outputstream. tostring ();
If (Ret. startswith ("<? ")){
Return ret. substring (Ret. indexof (">") + 1 );
}
Return ret;
}
Public String getvalue (string xmlstring, string tagname ){
Try {
Stringreader sr = new stringreader (xmlstring );
Inputsource is = new inputsource (SR );
Document Doc = dombuilder. parse (is );
Return Doc. getelementsbytagname (tagname). Item (0). gettextcontent ();
} Catch (exception e ){
// Todo: handle exception
Return NULL;
}
}
}
Package com. vmm. Socket;
Import java. Io. bytearrayoutputstream;
Import java. Io. stringreader;
Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. Transform. result;
Import javax. xml. Transform. source;
Import javax. xml. Transform. transformer;
Import javax. xml. Transform. transformerfactory;
Import javax. xml. Transform. Dom. domsource;
Import javax. xml. Transform. Stream. streamresult;
Import org. W3C. Dom. Document;
Import org. W3C. Dom. element;
Import org. W3C. Dom. node;
Import org. xml. Sax. inputsource;
Public class vmmsocket {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Try {
Documentbuilderfactory domfac = documentbuilderfactory
. Newinstance ();
// Obtain the DOM parser from the DOM Factory
Documentbuilder dombuilder = domfac. newdocumentbuilder ();
Document document = dombuilder. newdocument ();
Element R1 = Document. createelement ("Av ");
Element E = Document. createelement ("B ");
Document. appendchild (R1 );
R1.appendchild (E );
// System. Out. println ("XML:" + xmlfactory. getinstance (). getxmlstring (document ));
Transformerfactory tranfactory = transformerfactory. newinstance ();
Transformer atransformer = tranfactory. newtransformer ();
Source src = new domsource (document );
Bytearrayoutputstream outputstream = new bytearrayoutputstream ();
Result DEST = new streamresult (outputstream );
Atransformer. Transform (SRC, DEST );
System. Out. println ("----:" + outputstream. tostring ());
// Convert the XML document to be parsed into an input stream so that the DOM parser can parse it
Stringreader sr = new stringreader ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Xml1 A = 'G' B = 'gf '> <B> DG </B> <A> <B> ccedd </B> </a> </xml1> ");
Inputsource is = new inputsource (SR );
// Convert document
String xmlstring = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Xml1 A = 'G' B = 'gf '> <B> DG </B> <A> <B> ccedd </B> </a> </xml1> ";
// System. Out. println (xmlfactory. getinstance () getvalue (xmlstring, "B "));
// Parse the input stream of the XML document to obtain a document
Document Doc = dombuilder. parse (is );
// Get the root node (books) of the XML document)
Element root = Doc. getdocumentelement ();
System. Out. println ("root:" + root. tostring ());
System. Out. println ("B:" + root. getelementsbytagname ("B"). Item (0). gettextcontent ());
// Obtain all attribute names and values of the Root Node
System. out. println (root. getchildnodes (). item (0 ). getchildnodes (). item (0) + "," + root. getattributes (). getlength ());
If (0 <root. getattributes (). getlength ()){
System. Out. println ("root node attribute information ..........");
For (int A = 0; A <root. getattributes (). getlength (); A ++ ){
System. Out. println (root. getattributes (). Item ()
. Getnodename ()
+ ":" + Root. getattributes (). Item (a). getnodename ());
}
}
// System. Out. println (socketrequest. getresult ("AFD City requires bcedgdsf city rich merchant jia "));
System. Out. println ("over ");
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}