XPath operation XML in Java, very convenient

Source: Internet
Author: User
Tags xpath

<?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

Related Article

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.