Engineero
Engineero

Reputation: 12908

Java XML: parsing nested XML file with identical tags

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!

Upvotes: 3

Views: 4761

Answers (1)

M A
M A

Reputation: 72844

The Document instance already represents the tree structure of the XML in memory. You'll have to navigate your way properly through this structure. If you want the top-level Cluster elements, you can get the child nodes of the root of the XML and loop over them:

List<Node> topLevelClusterElements = new ArrayList<Node>();

NodeList childNodes = doc.getDocumentElement().getChildNodes();
for(int i = 0; i < 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);
    }
}

Upvotes: 5

Related Questions