/**
* Convert map to XML, default root tag is packet;
* Because the list element does not have a name, by default the name of the list is replaced by the _list> section with _info>, which is the name of the element in the list;
* Do not consider the list element is also the list of cases.
* @param version For example 1.0
* @param encoding such as UTF-8 or GBK
* @param packettype such as request or response
* @param requesttype such as N01
* @param the contents of the Param body body
* @return
*/
public static string Maptoxml (string version, String encoding,
String Packettype, String RequestType, map<string, object> param) {
StringBuffer sb = new StringBuffer ("<?xml version=\");
Sb.append (Version). Append ("\" Encoding=\ ""). Append (encoding)
. Append ("\" ><packet type=\ ""). Append (Packettype). Append ("\" Version=\ "")
. Append (Version). Append ("\" > "). Append (" <HEAD><USER> "). Append (" Ebaobj_test ")
. Append ("</USER><PASSWORD>"). Append ("111111"). Append ("</PASSWORD><REQUEST_TYPE>")
. Append (RequestType). Append ("</REQUEST_TYPE></HEAD>"). Append ("<BODY>");
if (param! = null &&!param.isempty ()) {
Parsemaptoxml (SB, param);
}
Sb.append ("</BODY></PACKET>");
return sb.tostring ();
}
/**
* Convert XML to map, take only what is inside the body;
* Do not consider the list of elements or list of the case.
*
* @param xml
* @return
* @throws jdomexception
* @throws IOException
*/
public static map<string, object> xmltomap (String XML)
Throws Jdomexception, IOException {
if (XML = = NULL | | xml.length () = = 0) {
return new hashmap<string, object> ();
}
StringReader reader = new StringReader (XML);
InputSource Source = new InputSource (reader);
Saxbuilder sax = new Saxbuilder ();
Document doc = sax.build (source);
Element root = Doc.getrootelement ();
List nodes = Root.getchildren ();
Return (map<string, object>) Parsexmltomap (nodes). Get ("BODY");
}
To convert an XML string to a map
private static map<string, object> Parsexmltomap (List subnodes) {
map<string, object> subMap = new hashmap<string, object> (Subnodes.size ());
List Subsubnodes;
Element Subel = null;
String Subelementname;
if (subnodes = = NULL | | subnodes.isempty ()) {
return SUBMAP;
}
for (Object subnode:subnodes) {
Subel = (Element) subnode;
Subelementname = Subel.getname ();
Subsubnodes = Subel.getchildren ();
if (Subelementname.endswith ("_list")) {
The current element is a list
Submap.put (Subelementname, Parsexmltolist (subsubnodes));
} else if (!subsubnodes.isempty ()) {
The current element is a map
Submap.put (Subelementname, Parsexmltomap (subsubnodes));
} else {
The current element is a simple element
Submap.put (Subelementname, Subel.gettext ());
}
}
return SUBMAP;
}
//Convert XML string to MAP, regardless of element in list or list
private static list<map<string, object>> Parsexmltolist (List subnodes) {
list<map<string, object>> sublist = new ArrayList<Map <string, object>> (
subnodes.size ());
if (subnodes = = NULL | | subnodes.isempty ()) {
return sublist;
}
list subsubnodes;
element Subel = null;
for (Object subnode:subnodes) {
subel = (Element) subnode;
subsubnodes = Subel.getchildren ();
if (Subsubnodes.isempty ()) {
//empty element item
Sublist.add (New hashmap<string, object> ());
} else {
sublist.add (Parsexmltomap (subsubnodes));
}
}
return sublist;
}
To convert a Map object to XML
private static void Parsemaptoxml (StringBuffer sb, map<string, object> param) {
if (param = = NULL | | param.isempty ()) {
Return
}
String key;
Object value;
For (entry<string, object> entry:param.entrySet ()) {
Key = Entry.getkey ();
Value = Entry.getvalue ();
if (value instanceof List) {
Sb.append ("<");
Sb.append (key);
Sb.append (">");
Parselisttoxml (SB,
Generateelementname (Key), (list<map<string, object>>) value);
Sb.append ("</");
Sb.append (key);
Sb.append (">");
} else if (value instanceof Map) {
Sb.append ("<");
Sb.append (key);
Sb.append (">");
Parsemaptoxml (SB, (map<string, object>) value);
Sb.append ("</");
Sb.append (key);
Sb.append (">");
} else {
Sb.append ("<");
Sb.append (key);
Sb.append (">");
Sb.append (value);
Sb.append ("</");
Sb.append (key);
Sb.append (">");
}
}
}
/**
* Convert the list object to XML because the elements in the list do not have a name.
* By default, the name of the list is replaced by the _list> section with _info>, as the name of the element in the list;
* Do not consider the list element is also the list of cases.
*/
private static void Parselisttoxml (StringBuffer sb, String ElementName,
list<map<string, object>> param) {
if (param = = NULL | | param.isempty ()) {
Return
}
For (map<string, object> Element:param) {
Sb.append ("<");
Sb.append (elementname);
Sb.append (">");
Parsemaptoxml (sb, Element);
Sb.append ("</");
Sb.append (elementname);
Sb.append (">");
}
}
Replace the name of the list with the _list part of _info as the name of the element in the list
private static string Generateelementname (String listname) {
if (ListName = = NULL | |!listname.contains ("_list")) {
return listname + "_info";
}
Return listname.substring (0, Listname.lastindexof ("_list")) + "_info";
}
XML format:
The data is in an XML-formatted file with a character encoding of GBK.
<?xml version= "1.0" encoding= "GBK"?>
<packet type= "REQUEST" version= "1.0" >
<HEAD>
<USER></USER>
<PASSWORD></PASSWORD>
<REQUEST_TYPE></REQUEST_TYPE>
</HEAD>
<BODY>
<MANUAL_UNDERWRITING>
<INSURE_SEQUENCE_NO></INSURE_SEQUENCE_NO>
<IF_CANCEL_INSURE></IF_CANCEL_INSURE>
<UNDERWRITING_INFO_LIST>
<UNDERWRITING_INFO>
<UNDERWRITING_PHOTO></UNDERWRITING_PHOTO>
</UNDERWRITING_INFO>
<UNDERWRITING_INFO>
<UNDERWRITING_PHOTO></UNDERWRITING_PHOTO>
</UNDERWRITING_INFO>
</UNDERWRITING_INFO_LIST>
</MANUAL_UNDERWRITING>
</BODY>
</PACKET>
Map format:
/**
* Scenario One: Converting a file to a string base64 requires a separate pass in the filename, the path
* file, filename, file path can be placed under the same map
* @throws documentexception
* @throws saxexception
* @throws IOException
* @throws parserconfigurationexception
* @throws jdomexception
*/
public void Test () throws Documentexception, Saxexception, IOException, Parserconfigurationexception, jdomexception{
System.out.println ("====testcheck Artificial underwriting Test start====");
File Underwritingphoto1=new file ("D:/1.png");
File Underwritingphoto2=new file ("D:/2.png");
String base64file1=filestreamutil.filetobase64 (UNDERWRITINGPHOTO1);
String base64file2=filestreamutil.filetobase64 (UnderwritingPhoto2);
Map<string,object> underwritinginfo1=new hashmap<string, object> ();
Map<string,object> underwritinginfo2=new hashmap<string, object> ();
List<map<string,object>> underwritinginfolist=new arraylist<map<string,object>> ();
Map<string,object> manualunderwriting=new hashmap<string, object> ();
Map<string,object> body=new hashmap<string, object> ();
Underwritinginfo1.put ("Underwriting_photo", base64file1);//File stream
Underwritinginfo2.put ("Underwriting_photo", base64file2);
Underwritinginfolist.add (UNDERWRITINGINFO1);
Underwritinginfolist.add (UnderwritingInfo2);
Manualunderwriting.put ("Underwriting_info_list", underwritinginfolist);
Manualunderwriting.put ("If_cancel_insure", "N");//whether to cancel the insurance
Manualunderwriting.put ("Insure_sequence_no", "001");//Insured serial number
Body.put ("manual_underwriting", manualunderwriting);
String xmlstr= Ciitcxmlutil.maptoxml ("1.0", "GBK", "REQUEST", "N01", body);
Map<string,string> map=new hashmap<string, string> ();
Map.put ("Xmlstr", xmlstr);
System.out.println ("====testcheck Artificial underwriting Test xmlstr====" +xmlstr);
Platecheckaction.peoplecheckinfbase64 (map);
Httpproxy.send ("Http://localhost:8080/egis-scms-insu/scms/insu/peopleCheckInfBase64.do", map, Map.class);
System.out.println ("====testcheck Artificial underwriting Test end====");
}
/**
* Scenario Two: Save File directly in map<string,file>. File names, file paths, files are passed together
* This is not a workable approach.
* Object cannot be converted to file if the file is passed into map<string,object> and then taken out.
* will prompt string cannot be converted to f *
* If,map<string,file> then means that the MAP can only save files and cannot be stored in other non-file type data
* If the file can be transmitted separately, but since we want to send a map must be to put more values in the map, otherwise it will not be used to map a file directly passed
* But if you want to deposit some additional data, you need to define map<string,object> *
* Do multiple maps have to be in a list,list<map<string,object>> or define list<map<string,file>>?
* If defined as List<map<string,object>>, List.add (Filemap), time will be error. Because the list contains a map of object, and if the map is stored in file, it will compile the error
* If it's defined as list<map<string,file>>, it's more unlikely to happen.
*
* If map is defined as map<string,object> map ==map.put ("file", file);//file file=new file ("d://input.jpg"); If you secretly deposit a file in , which is also stored as a string file path. Instead of files of type file.
* Because there is no way to receive the file type when the value is taken. When it is received, it can only be a string or object type. Cannot be a Flie type
* If an object type interface is used. Strong to file type, will prompt an error: Java.lang cannot be converted to file *
* I did an experiment: if you transfer a map directly, a file in the map can be strongly converted to the file type.
* If the map is converted to XML, then the XML will be converted to map in the past. This time All of the objects in the map are treated as String types
* Although the Xlmtomap method returns a map<string,object> such a turn, the String cannot be converted to file when strongly converted to file *
* Scenario Three: Reason same as scenario two, cannot be strongly converted to bype[] because after XML to map, all values in map are treated as string types and cannot be strongly converted to byte[]
*
* Summary: Currently can only take the program one incoming BASE64 file content
* Scenario two, scheme three incoming file,byte[] all because of the XML and map conversion process, map value value cannot be cast to the original file and byte[] Type
*
* @throws documentexception
* @throws saxexception
* @throws IOException
* @throws parserconfigurationexception
* @throws jdomexception
*/
public void Test2 () throws Documentexception, Saxexception, IOException, Parserconfigurationexception, jdomexception{
System.out.println ("====test2 Artificial underwriting Test start====");
File File1=new file ("D:/1.png");
File File2=new file ("D:/2.png");
Map<string,object> underwritinginfo1=new hashmap<string, object> ();
Map<string,object> underwritinginfo2=new hashmap<string, object> ();
List<map<string,object>> underwritinginfolist=new arraylist<map<string,object>> ();
Map<string,object> manualunderwriting=new hashmap<string, object> ();
Map<string,object> body=new hashmap<string, object> ();
Underwritinginfo1.put ("Underwriting_photo", file1);//File stream
Underwritinginfo2.put ("Underwriting_photo", file2);
Underwritinginfolist.add (UNDERWRITINGINFO1);
Underwritinginfolist.add (UnderwritingInfo2);
For (map<string, object> file:underwritinginfolist) {
Object o=file.get ("Underwriting_photo");
File f= (file) o;
System.out.println (F.getabsolutepath ());
// }
Manualunderwriting.put ("Underwriting_info_list", underwritinginfolist);
Manualunderwriting.put ("If_cancel_insure", "N");//whether to cancel the insurance
Manualunderwriting.put ("Insure_sequence_no", "001");//Insured serial number
Body.put ("manual_underwriting", manualunderwriting);
String xmlstr= Ciitcxmlutil.maptoxml ("1.0", "GBK", "REQUEST", "N01", body);
Map<string,string> map=new hashmap<string, string> ();
Map.put ("Xmlstr", xmlstr);
System.out.println ("====testcheck Artificial underwriting Test xmlstr====" +xmlstr);
Platecheckaction.peoplecheckinffiles (map);
Httpproxy.send ("Http://localhost:8080/egis-scms-insu/scms/insu/peopleCheckInfTytes.do", map, Map.class);
System.out.println ("====test2 Artificial underwriting Test end====");
}
XML and Map transfer