/**
*
*/
Package com. City. util;
Import java. Beans. propertydescriptor;
Import java. Io. bytearrayinputstream;
Import java. Io. unsupportedencodingexception;
Import java. Lang. Reflect. field;
Import java. Lang. Reflect. method;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;
Import java. util. Map;
Import java. util. Set;
Import java. util. Map. entry;
Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. parsers. parserconfigurationexception;
Import org. dom4j. Document;
Import org. dom4j. documenthelper;
Import org. dom4j. element;
Import org. JUnit. test;
Import com. City. pojo. basebean;
Import com. City. pojo. userinfo;
/**
* @ Author chendz 04:50:08
*
*/
Public class buildxml implements buildfile {
Public <t extends basebean> string Builder (t obj ){
List <basebean> List = new arraylist <basebean> ();
List. Add (OBJ );
Return this. Builder (list );
}
/* (Non-javadoc)
* @ See COM. itrus. Remote. buildfile # Builder (Java. util. List)
*/
Public <t extends basebean> string Builder (list <t> List ){
Document document = incluenthelper. createdocument ();
Element root = Document. addelement ("root ");
For (Object OBJ: List)
{
Element ele = root. addelement ("user ");
// Add a node
Try {
This. addelement (Ele, OBJ );
} Catch (exception e ){
Throw new runtimeexception (E );
}
}
Return root. asxml ();
}
Private void addelement (element ele, object OBJ) throws exception {
Class <?> Clazz = obj. getclass ();
Field [] FS = clazz. getdeclaredfields ();
For (field: FS) // traverses the field
{
Propertydescriptor Pd = new propertydescriptor (field. getname (), clazz );
Method getmethod = Pd. getreadmethod ();
Element element = ELE. addelement (field. getname ());
// If the attribute of the pojo object is not empty, add it to the text of the node.
If (getmethod. Invoke (OBJ )! = NULL)
{
Element. addtext (getmethod. Invoke (OBJ). tostring ());
}
}
}
/** Parse XML
* @ Author wangxinying 10:37:38 pm
*
*/
Public map parsexml (string XML) throws exception {
Document Doc = incluenthelper. parsetext (XML );
Element El = (document) Doc). getrootelement ();
Map map = new hashmap ();
If (El! = NULL ){
List <element> jiedians = El. Elements ();
For (Int J = 0; j <jiedians. Size (); j ++ ){
Element url = jiedians. Get (j );
System. Out. println ("name:" + URL. getname () + "value:" + URL. gettexttrim ());
Map. Put (URL. getname (), URL. gettexttrim ());
If ("uainfo". Equals (URL. getname (). Trim ())){
List <element> accounts1 = URL. Elements ();
For (int t = 0; t <accounts1.size (); t ++ ){
Element ACC = accounts1.get (t );
System. Out. println (Acc. getname () + "and" + acc. gettexttrim ());
Map. Put (Acc. getname (), ACC. gettexttrim ());
}
}
If ("svccont". Equals (URL. getname (). Trim ())){
List <element> accounts = URL. Elements ();
For (INT I = 0; I <accounts. Size (); I ++ ){
Element ACC = accounts. Get (I );
System. Out. println (Acc. getname () + "and" + acc. gettexttrim ());
Map. Put (Acc. getname (), ACC. gettexttrim ());
}
}
}
}
System. Out. println (MAP );
Return map;
}
Public String createxml (userinfo user ){
Document document = incluenthelper. createdocument ();
Element userselement = Document. addelement ("ecity ");
// Userselement. addattribute ("operate", "AA"); // Add attributes of the Root Node
Element ele1 = userselement. addelement ("msgname ");
Ele1.addtext ("userregisterresp ");
Element ele2 = userselement. addelement ("msgversion ");
Ele2.addtext ("1.0 ");
Element ele3 = userselement. addelement ("transactionid ");
Ele3.addtext ("admin ");
Element ele4 = userselement. addelement ("result ");
Ele4.addelement ("rspcode"). addtext ("0 ");
Ele4.addelement ("rspdesc"). addtext ("");
Element ele5 = userselement. addelement ("svccont ");
Ele5.addelement ("userid"). addtext (user. getuserid ());
Ele5.addelement ("password"). addtext (user. GetPassword ());
Return document. asxml ();
}
Public String createxmlinfo (MAP map ){
Set entries = map. entryset ();
Document document = incluenthelper. createdocument ();
If (entries! = NULL ){
Iterator = entries. iterator ();
While (iterator. hasnext ()){
Map. Entry entry = (entry) iterator. Next ();
Object key = entry. getkey (). tostring ();
Object value = entry. getvalue (). tostring ();
// Element userselement = Document. addelement ("ecity ");
Element ele1 = Document. addelement (string) Key );
Ele1.addtext (string) value );
}
}
Return document. asxml ();
}
Public static void main (string [] ARGs) throws exception {
// String xml = "<ecity> <msgname> userregisterreq </msgname>" +
// "<Msgversion> 1.0 </msgversion> <transactionid> 1 </transactionid> <uainfo> <UA> nokian97 </UA> </uainfo>" +
// "<Userid> </userid> <msgsender> mmclient </msgsender>" +
// "<Svccont> <regid> 13912345678 </regid> <regtype> 0 </regtype> </svccont> </ecity> ";
// Map u = new buildxml (). parsexml (XML );
Map map = new hashmap ();
Map. Put ("token", "token ");
String Mapu = new buildxml (). createxmlinfo (MAP );
System. Out. println (Mapu );
}
}