Java reads XML with dom4j

Source: Internet
Author: User

Dom4j is a very useful class library for reading XML. In particular, it can select conditions to select a node, which is very useful.

1. Generate the document file.

/*** @ Param xmlfilepath: XML file path * @ return Document Object */public static document getdocument (string xmlfilepath) {saxreader reader = new saxreader (); document = NULL; try {inputstream in = analyticalxml. class. getresourceasstream (xmlfilepath); document = reader. read (in);} catch (incluentexception e) {system. out. println ("an exception occurred when reading the xmlfilename file under classpath. Check whether the classpath and file name exist! "); E. printstacktrace ();} return document ;}

2,

/***** Low performance * Creation Time: 2012-3-26 * @ author WQL * @ Param xmlfilename * @ Param conditions/data/equip [@ itemid1 = 41] [@ itemid2 = 51] Two conditions */public static void xml_armor_equip_config (string xmlfilename, string conditions) {// todo: Document document = analyticalxml. getdocument (xmlfilename); List list = document. selectnodes (conditions); stringbuffer sb = new stringbuffer (0); iterator iter = List. iterator (); While (ITER. hasnext () {element Ep = (element) ITER. next (); For (iterator it = EP. attributeiterator (); it. hasnext ();) {attribute = (attribute) it. next (); string name = attribute. getname (); string value = attribute. getvalue (); sb. append ("" + name + "=" + value);} sb. append ("\ r"); iterator iterss = EP. elementiterator ("material"); While (iterss. hasnext () {element Kp = (element) iterss. next (); sb. append ("<material"); string itemtype = KP. attributevalue ("itemtype"); sb. append ("itemtype:" + itemtype); string Itemid = KP. attributevalue ("Itemid"); sb. append ("Itemid:" + Itemid); string itemcount = KP. attributevalue ("itemcount"); sb. append ("itemcount:" + itemcount); sb. append ("\ r");} iterator rivet = EP. elementiterator ("rivet"); While (rivet. hasnext () {sb. append ("<rivet"); element Np = (element) rivet. next (); string x = NP. attributevalue ("X"); sb. append ("X:" + x); string y = NP. attributevalue ("Y"); sb. append ("Y:" + Y);} system. out. println (sb. tostring (); sb. append ("\ r \ n ");}}
 
3. Call
  
xml_Armor_equip_config("/XML/armor_equip_config.xml","/data/equip[@itemid1=41][@itemid2=51]"); 

Dom4j is a very useful class library for reading XML. In particular, it can select conditions to select a node, which is very useful.

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.