Java XML: parsing nested XML file with identical tags -


background

i have xml document represents data structure in labview (an array of clusters of clusters) stores simulation parameters. generated document saving data structure xml labview, , need keep general format labview can read @ later time. document structured follows:

<array>     <cluster>         <name>meaningful name 1</name>         <cluster>  <!-- note clusters within clusters -->             <name>component 1 params</name>             <!-- parameter values here -->         </cluster>         <cluster>             <name>component 2 params</name>             <!-- parameter values here -->         </cluster>     </cluster>     <!-- more clusters of clusters --> </array> 

each parent cluster have same child elements (component 1 params, component 2 params, etc.), value fields (not shown) different. each parent cluster have unique name. cannot change tags used specify parent/child clusters because labview not read file.

work far

i working on java app allow users edit parameter data stored in document without breaking format (so labview can still read it). want user able select 1 of parent clusters name field, , populate form data stored within data can edited. problem using documentbuilder , document classes, cannot seem split out parent cluster nodes.

working answer parsing xml nodelist , documentbuilder:

documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder();  document doc = builder.parse("param_file.xml");  nodelist nodes = doc.getelementsbytagname("cluster");  // every cluster in list, want iterate on top-level clusters. (int = 0; < nodes.getlength(); ++i) {     element node = (element) nodes.item(i);     // display cluster names user select one... } 

question

i guess looking way represent xml file object maintains tree structure , generate list of top-level cluster elements, can each drilled get/set child cluster elements , attributes thereof.

thanks!

the document instance represents tree structure of xml in memory. you'll have navigate way through structure. if want top-level cluster elements, can child nodes of root of xml , loop on them:

list<node> toplevelclusterelements = new arraylist<node>();  nodelist childnodes = doc.getdocumentelement().getchildnodes(); for(int = 0; < childnodes.getlength(); i++) {     node childnode = childnodes.item(i);     if(childnode.getnodetype() == node.element_node && childnode.getnodename().equals("cluster")) {         element clusterelement = (element) childnode;         toplevelclusterelements.add(clusterelement);     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -