Parsing an XML file with 4 level XML file with Java -
i trying parse file , different levels data totally owned me. please help. can parse 1 level file 1 killing me. when thought got messed further. need loop through usermodel records. programming language need parse java. below provide 2 samples. xml file , code writing. don't think drilling down element want loop through. in sample can 1 instance of records , loop stops. wrong.
<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <getusersresponse xmlns="http://tempuri.org/"> <getusersresult xmlns:a="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <a:usermodel> <a:_x003c_canmanageusers_x003e_k__backingfield>1</a:_x003c_canmanageusers_x003e_k__backingfield> <a:_x003c_configurationprofilename_x003e_k__backingfield>default</a:_x003c_configurationprofilename_x003e_k__backingfield> <a:_x003c_defaultlandingpage_x003e_k__backingfield>1</a:_x003c_defaultlandingpage_x003e_k__backingfield> <a:_x003c_email_x003e_k__backingfield>admin@admin.com</a:_x003c_email_x003e_k__backingfield> <a:_x003c_fax_x003e_k__backingfield>555-555-5555</a:_x003c_fax_x003e_k__backingfield> <a:_x003c_firstname_x003e_k__backingfield>onbehalf</a:_x003c_firstname_x003e_k__backingfield> <a:_x003c_lastname_x003e_k__backingfield>admin</a:_x003c_lastname_x003e_k__backingfield> <a:_x003c_localeid_x003e_k__backingfield>1033</a:_x003c_localeid_x003e_k__backingfield> <a:_x003c_mobileconfigurationprofile_x003e_k__backingfield>default</a:_x003c_mobileconfigurationprofile_x003e_k__backingfield> <a:_x003c_mobile_x003e_k__backingfield/> <a:_x003c_orderprivilege_x003e_k__backingfield>0</a:_x003c_orderprivilege_x003e_k__backingfield> <a:_x003c_phone_x003e_k__backingfield/> <a:_x003c_pricingdisplay_x003e_k__backingfield/> <a:_x003c_userid_x003e_k__backingfield>admin</a:_x003c_userid_x003e_k__backingfield> </a:usermodel> <a:usermodel> <a:_x003c_canmanageusers_x003e_k__backingfield>1</a:_x003c_canmanageusers_x003e_k__backingfield> <a:_x003c_configurationprofilename_x003e_k__backingfield>default</a:_x003c_configurationprofilename_x003e_k__backingfield> <a:_x003c_defaultlandingpage_x003e_k__backingfield>1</a:_x003c_defaultlandingpage_x003e_k__backingfield> <a:_x003c_email_x003e_k__backingfield/> <a:_x003c_fax_x003e_k__backingfield/> <a:_x003c_firstname_x003e_k__backingfield>direct</a:_x003c_firstname_x003e_k__backingfield> <a:_x003c_lastname_x003e_k__backingfield>sales</a:_x003c_lastname_x003e_k__backingfield> <a:_x003c_localeid_x003e_k__backingfield>1033</a:_x003c_localeid_x003e_k__backingfield> <a:_x003c_mobileconfigurationprofile_x003e_k__backingfield>default</a:_x003c_mobileconfigurationprofile_x003e_k__backingfield> <a:_x003c_mobile_x003e_k__backingfield/> <a:_x003c_orderprivilege_x003e_k__backingfield>2</a:_x003c_orderprivilege_x003e_k__backingfield> <a:_x003c_phone_x003e_k__backingfield/> <a:_x003c_pricingdisplay_x003e_k__backingfield/> <a:_x003c_userid_x003e_k__backingfield>direct</a:_x003c_userid_x003e_k__backingfield> </a:usermodel> </getusersresult> </getusersresponse> </s:body> </s:envelope>
now here java code on how started this:
file fxmlfile = new file("c:\\users\\user\\documents\\testusers.xml"); document dom = (document) db.parse(fxmlfile); element docele = dom.getdocumentelement(); nodelist nl = docele.getchildnodes(); if (nl != null && nl.getlength() > 0) { (int = 0; < nl.getlength(); i++) { // system.out.println("node type is: "+nl.item(i).getnodetype()); // system.out.println("element node is: "+node.element_node); if (nl.item(i).getnodetype() == node.element_node) { element el = (element) nl.item(i); // system.out.println("node name: "+el.getnodename()); //system.out.println(el.getelementsbytagname("a:usermodel").item(0).gettextcontent()); if (el.getnodename().contains("a:usermodel")) { //system.out.println("the loop cycle is: "+i); string canmanageusers = el.getelementsbytagname("a:_x003c_canmanageusers_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("can manage users", canmanageusers); //system.out.println(canmanageusers); string confprofilename = el.getelementsbytagname("a:_x003c_configurationprofilename_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("configuration profile name", confprofilename); // system.out.println(confprofilename); string defaultlandingpage = el.getelementsbytagname("a:_x003c_defaultlandingpage_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("default landing page", defaultlandingpage); //system.out.println(defaultlandingpage); string email = el.getelementsbytagname("a:_x003c_email_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("e-mail", email); //system.out.println(email); string fax = el.getelementsbytagname("a:_x003c_fax_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("fax", fax); //system.out.println(fax); string fname = el.getelementsbytagname("a:_x003c_firstname_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("first name", fname); //system.out.println(fname); string lname = el.getelementsbytagname("a:_x003c_lastname_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("last name", lname); //system.out.println(lname); string localeid = el.getelementsbytagname("a:_x003c_localeid_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("locale id", localeid); //system.out.println(localeid); string mobileconfprofile = el.getelementsbytagname("a:_x003c_mobileconfigurationprofile_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("mobile configuration profile", mobileconfprofile); //system.out.println(mobileconfprofile); string mobile = el.getelementsbytagname("a:_x003c_mobile_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("mobile", mobile); //system.out.println(mobile); string orderprivilege = el.getelementsbytagname("a:_x003c_orderprivilege_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("can submit orders", orderprivilege); //system.out.println(orderprivilege); string phone = el.getelementsbytagname("a:_x003c_phone_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("phone", phone); //system.out.println(phone); string pricingdisplay = el.getelementsbytagname("a:_x003c_pricingdisplay_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("pricing display mode", pricingdisplay); //system.out.println(pricingdisplay); string userid = el.getelementsbytagname("a:_x003c_userid_x003e_k__backingfield").item(0).gettextcontent(); datamap.put("user id", userid); //system.out.println(userid);
foreword:
i posted before update regarding java
. i'll leave xpath
part of solution should transferable java
based solution.
here's way of doing in python using lxml , xpath:
from lxml import etree doc = etree.xml("""<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:body> <getusersresponse xmlns="http://tempuri.org/"> <getusersresult xmlns:a="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <a:usermodel> <a:_x003c_canmanageusers_x003e_k__backingfield>1</a:_x003c_canmanageusers_x003e_k__backingfield> <a:_x003c_configurationprofilename_x003e_k__backingfield>default</a:_x003c_configurationprofilename_x003e_k__backingfield> <a:_x003c_defaultlandingpage_x003e_k__backingfield>1</a:_x003c_defaultlandingpage_x003e_k__backingfield> <a:_x003c_email_x003e_k__backingfield>admin@admin.com</a:_x003c_email_x003e_k__backingfield> <a:_x003c_fax_x003e_k__backingfield>555-555-5555</a:_x003c_fax_x003e_k__backingfield> <a:_x003c_firstname_x003e_k__backingfield>onbehalf</a:_x003c_firstname_x003e_k__backingfield> <a:_x003c_lastname_x003e_k__backingfield>admin</a:_x003c_lastname_x003e_k__backingfield> <a:_x003c_localeid_x003e_k__backingfield>1033</a:_x003c_localeid_x003e_k__backingfield> <a:_x003c_mobileconfigurationprofile_x003e_k__backingfield>default</a:_x003c_mobileconfigurationprofile_x003e_k__backingfield> <a:_x003c_mobile_x003e_k__backingfield/> <a:_x003c_orderprivilege_x003e_k__backingfield>0</a:_x003c_orderprivilege_x003e_k__backingfield> <a:_x003c_phone_x003e_k__backingfield/> <a:_x003c_pricingdisplay_x003e_k__backingfield/> <a:_x003c_userid_x003e_k__backingfield>admin</a:_x003c_userid_x003e_k__backingfield> </a:usermodel> <a:usermodel> <a:_x003c_canmanageusers_x003e_k__backingfield>1</a:_x003c_canmanageusers_x003e_k__backingfield> <a:_x003c_configurationprofilename_x003e_k__backingfield>default</a:_x003c_configurationprofilename_x003e_k__backingfield> <a:_x003c_defaultlandingpage_x003e_k__backingfield>1</a:_x003c_defaultlandingpage_x003e_k__backingfield> <a:_x003c_email_x003e_k__backingfield/> <a:_x003c_fax_x003e_k__backingfield/> <a:_x003c_firstname_x003e_k__backingfield>direct</a:_x003c_firstname_x003e_k__backingfield> <a:_x003c_lastname_x003e_k__backingfield>sales</a:_x003c_lastname_x003e_k__backingfield> <a:_x003c_localeid_x003e_k__backingfield>1033</a:_x003c_localeid_x003e_k__backingfield> <a:_x003c_mobileconfigurationprofile_x003e_k__backingfield>default</a:_x003c_mobileconfigurationprofile_x003e_k__backingfield> <a:_x003c_mobile_x003e_k__backingfield/> <a:_x003c_orderprivilege_x003e_k__backingfield>2</a:_x003c_orderprivilege_x003e_k__backingfield> <a:_x003c_phone_x003e_k__backingfield/> <a:_x003c_pricingdisplay_x003e_k__backingfield/> <a:_x003c_userid_x003e_k__backingfield>direct</a:_x003c_userid_x003e_k__backingfield> </a:usermodel> </getusersresult> </getusersresponse> </s:body> </s:envelope>""") nsmap = dict( s = "http://schemas.xmlsoap.org/soap/envelope/", t = "http://tempuri.org/", = "http://schemas.datacontract.org/2004/07/", = "http://www.w3.org/2001/xmlschema-instance", ) def name_from_stupid_tag(tag): return tag.rpartition('_x003c_')[2].partition('_x003e_')[0] user_models = [] user_model in doc.xpath('s:body/t:getusersresponse/t:getusersresult/a:usermodel',namespaces=nsmap): data = dict((name_from_stupid_tag(e.tag),unicode(e.text) if e.text not none else e.text) e in user_model) user_models.append(data)
yielding:
[{'canmanageusers': u'1', 'configurationprofilename': u'default', 'defaultlandingpage': u'1', 'email': u'admin@admin.com', 'fax': u'555-555-5555', 'firstname': u'onbehalf', 'lastname': u'admin', 'localeid': u'1033', 'mobile': none, 'mobileconfigurationprofile': u'default', 'orderprivilege': u'0', 'phone': none, 'pricingdisplay': none, 'userid': u'admin'}, {'canmanageusers': u'1', 'configurationprofilename': u'default', 'defaultlandingpage': u'1', 'email': none, 'fax': none, 'firstname': u'direct', 'lastname': u'sales', 'localeid': u'1033', 'mobile': none, 'mobileconfigurationprofile': u'default', 'orderprivilege': u'2', 'phone': none, 'pricingdisplay': none, 'userid': u'direct'}]
Comments
Post a Comment