<?XML version= "1.0" encoding= "UTF-8"?><messagelist> <Itemtype= "1"> <template_idvalue= "P2itjpj0tattp4qrxp-z51nyud3adnhgvlouswgy4p0"/> <Topcolorvalue= "#173177"/> < Firstvalue= "Hello, your credit card received 1000 yuan remittance." "Color= "#173177"/> <ProductTypevalue= "Account type: credit card \ Tail Number: 1758 4545"Color= "#173177"/> < Timevalue= "September 30, 2013 17:58"Color= "#173177"/> <typevalue= "Collection"Color= "#173177"/> < Numbervalue= "1000 Yuan"Color= "#173177"/> <Remarkvalue= "Remark: If you have any questions, please call our hotline 123323. "Color= "#173177"/> </Item> <Itemtype= "2"> <template_idvalue= "P2itjpj0tattp4qrxp-z51nyud3adnhgvlouswgy4p0"/> <Topcolorvalue= "#173177"/> < Firstvalue= "Hello, test type=2 push." "Color= "#173177"/> <ProductTypevalue= "Account type: credit card \ Tail Number: 1758 4545"Color= "#173177"/> < Timevalue= "September 30, 2013 17:58"Color= "#173177"/> <typevalue= "Collection"Color= "#173177"/> < Numbervalue= "1000 Yuan"Color= "#173177"/> <Remarkvalue= "Remark: If you have any questions, please call our hotline 123323. "Color= "#173177"/> </Item></messagelist>
/** * @throws documentexception * @Title: Getbuysuccesstemplate * @Date: 2015-3-23 * @Autor: Gavin * @Description: To Do (push-to-end purchase success information) * @param @param template_id settings file * @return void return type * @throws * * * private String gettemplatejson (int ty PE) {try {stringbuffer Templatejson = new StringBuffer (); String path = This.getclass (). GetResource ("/"). GetPath (); String Xmlpath = path + file.separator + "config" + file.separator+ "Spring" + file.separator + "Send-message.xml"; Logutils.writelog (Xmlpath); File XMLFile = new file (Xmlpath); Saxreader reader = new Saxreader (); Document doc = Reader.read (xmlfile); Element childs = (Element) Doc.selectsinglenode ("//messagelist//item[@type = '" + Type + "']"); Element Template_idele = (Element) Doc.selectsinglenode ("//messagelist//item[@type = '" + type+ "']//template_id"); Element Topcolorele = (Element) Doc.selectsinglenode ("//messagelist//item[@type = '" + type+ "']//topcolor"); list<element> childlist = childs.elements (); TemplatEjson.append (Gettemplatecommon (Template_idele.attributevalue ("value"), Topcolorele.attributevalue ("value")); for (Element child:childlist) {if (!child.getname (). Equals ("template_id") &&!child.getname () equals ("Topcol or ")) {templatejson.append (" \ "+ child.getname () +" \ ": {\" value\ ": \" "+ Child.attributevalue (" value ") +" \ ", \" Color\ ": \ "" + Child.attributevalue ("color") + "\"}, "); }} templatejson.append ("}}"); Return templatejson.tostring (). Replace (",}}", "}}"); } catch (Documentexception e) {//TODO auto-generated catch block E.printstacktrace (); return null; } }
XPath operation XML in Java, very convenient