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.