Java servlet XML parsing

Source: Internet
Author: User

The XML file format is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<operationXml>
<trans>
<versionId>8a8a93b05ad07ded015aea9ac3480416</versionId>
<transType>ADD_USER</transType>
<transXml>
<id>8a8a93b05ad07ded015aea9ac3390413</id>
<userId>syn_michelle_xi_91990415</userId>
<appId>4028fb83507dd41701507dd614ec0003</appId>
<display>0</display>
<username>syn_michelle_xi_91990415</username>
<pwd>8a8a93b05ad07ded015aea9ac3390414</pwd>
<organId>O0000000000000025622</organId>
<organCode>8a8a93b05ad07ded015aea62f80b03c9</organCode>
<struId>S0000000000000037508</struId>
<struPath>rootId#1#S0000000000000000424#S0000000000000037508</struPath>
<parentOrganId>O0000000000000000436</parentOrganId><
Parentorgancode>0000000000110100000000466000000000002</parentorgancode>
<name> finalising </name>
<organ_mobile>18600092536</organ_mobile>
<organ_email>[email protected]</organ_email>
<zjlx>0</zjlx><zjhm>360102198209190025</zjhm>
<rid></rid>
<unitname> China Civil Aviation Science and Technology Research Institute </unitname>
<unitbsdtid>D77B02867A594208AABE1C41E38B8A47</unitbsdtid>
<zzjgdm>400007439</zzjgdm>

<surveyleveltype>0</surveyleveltype>
<tyshxydm>121000004000074395</tyshxydm>
<prefecture>100000</prefecture>
<sfdlxxqy></sfdlxxqy>
</transXml>
</trans></operationXml>

Java method:

@Override
protected void doget (HttpServletRequest request, httpservletresponse response) throws IOException {

Map<string,string> Map = new hashmap<string,string> ();
Jsonobject json = NULL;
list<user> saveuserlist = new arraylist<user> ();
//string versionId = Request.getparameter ("VersionId");
//string action = request.getparameter ("action");
String versionId = "8a8a93b05ad07ded015aea9ac3480416";
String action = "Get_syn";

//initialization, which is used for database operations
Postgisdao Pdao = new Postgisdao ();
Pdao.initpostdb ();

Synchronizeclient sc = new synchronizeclient (urlstring);
System.out.println ("Action:" + action);//This is the action
System.out.println ("versionId:" + versionId) obtained in the notification; This is the versionid that is obtained in the notification

String DataXML = Sc.gettransxmlbyversionid (action, versionId);

if (! Utilmethod.isempty (DataXML)) {
try {
Document document = Documenthelper.parsetext (DataXML);
Element Rootele = Document.getrootelement ();
Element trans = rootele.element ("trans");
Get identity parameters
String Backversionid = Trans.elementtext ("VersionId");
String Transtype = Trans.elementtext ("Transtype");

Get user Information
@SuppressWarnings ("Unchecked")

There may be multiple transxml tags here, so use elements

list<element> transxmllist = trans.elements ("Transxml");

for (Element transxml:transxmllist) {

String id = transxml.elementtext ("id");
String userId = Transxml.elementtext ("UserId");
String appId = Transxml.elementtext ("AppId");
String display = Transxml.elementtext ("display");
String username = transxml.elementtext ("username");
String pwd = Transxml.elementtext ("pwd");
String Organid = Transxml.elementtext ("Organid");
String Organcode = Transxml.elementtext ("Organcode");
String Struid = Transxml.elementtext ("Struid");
String Strupath = Transxml.elementtext ("Strupath");
String Parentorganid = Transxml.elementtext ("Parentorganid");
String Parentorgancode = Transxml.elementtext ("Parentorgancode");
String name = Transxml.elementtext ("name");
String organ_mobile = Transxml.elementtext ("Organ_mobile");
String Organ_email = Transxml.elementtext ("Organ_email");
String ZJLX = Transxml.elementtext ("ZJLX");
String ZJHM = Transxml.elementtext ("Zjhm");
String rid = transxml.elementtext ("rid");
String unitname = Transxml.elementtext ("Unitname");
String Unitbsdtid = Transxml.elementtext ("Unitbsdtid");
String ZZJGDM = Transxml.elementtext ("ZZJGDM");
String Surveyleveltype = Transxml.elementtext ("Surveyleveltype");
String TYSHXYDM = Transxml.elementtext ("TYSHXYDM");
String prefecture = transxml.elementtext ("prefecture");
String sfdlxxqy = Transxml.elementtext ("sfdlxxqy");

list<user> List_user = Pdao.selectuserbyid (userId);
if (list_user.size () = = 0) {
User user = new user ();
User.setusername (USERID);
User.setuserpassword (PWD);
User.setuserphone (Long.parselong (organ_mobile));
User.setusermail (Organ_email);
Saveuserlist.add (user);
}

}
If the user already exists in the system, it will not be added to the saveuserlist (that is, unchanged in this system)
Boolean result = Pdao.saveuser (saveuserlist);
if (result) {
Map.put ("Message", "Synchronization succeeded");
Map.put ("type", "1");
}else{
Map.put ("Message", "Synchronization failed");
Map.put ("type", "0");
}
JSON = Jsonobject.frommap (map);

} catch (Documentexception | SQLException e) {
E.printstacktrace ();
Log.error ("Failed to synchronize data, new local user failed", e);

}
}
Setting the response type
Response.setcontenttype ("Application/json;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println (Json.tostring ());

}

Java servlet XML parsing

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.