Background
I have an XML document that represents a data structure in LabVIEW (an array of clusters of clusters) that stores simulation parameters. I generated the document by saving my data structure as XML from LabVIEW, and I need to keep its general format so that LabVIEW can read it back at a later time. The document is structured as 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 will have exactly the same child elements (Component 1 params, Component 2 params, etc.), only their Value fields (not shown) will be different. Each parent Cluster will also have a unique name. I cannot change the tags used to specify the parent/child clusters because then LabVIEW will not read the file.
Work so far
I am working on a Java app to allow users to edit the parameter data stored in the document without breaking its format (so that LabVIEW can still read it). I want the user to be able to select one of the parent clusters by its Name field, and then populate a form with the data stored within so that this data can be edited. My problem is that using the DocumentBuilder and Document classes, I cannot seem to split out only the parent Cluster nodes.
Working from the answer to parsing XML with NodeList and DocumentBuilder:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("param_file.xml");
NodeList nodes = doc.getElementsByTagName("Cluster"); // every Cluster is in this list, but I only want to iterate over the top-level clusters.
for (int i = 0; i < nodes.getLength(); ++i)
{
Element node = (Element) nodes.item(i);
// Display the cluster names for the user to select one...
}
Question
I guess I am looking for a way to represent my XML file as an object that maintains the tree structure and then generate a list of only the top-level Cluster elements, which can then each be drilled into to get/set their child Cluster elements and the attributes thereof.
Thanks!