[英]Java XML: parsing nested XML file with identical tags
I have an XML document that represents a data structure in LabVIEW (an array of clusters of clusters) that stores simulation parameters. 我有一个XML文档,它代表LabVIEW中的数据结构(一组簇集群),用于存储模拟参数。 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.
我通过将我的数据结构从LabVIEW保存为XML来生成文档,我需要保留其通用格式,以便LabVIEW可以在以后读取它。 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. 每个父
Cluster
将具有完全相同的子元素(组件1参数,组件2参数等),只有它们的Value
字段(未显示)将是不同的。 Each parent Cluster
will also have a unique name. 每个父
Cluster
也将具有唯一的名称。 I cannot change the tags used to specify the parent/child clusters because then LabVIEW will not read the file. 我无法更改用于指定父/子群集的标记,因为LabVIEW不会读取该文件。
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). 我正在开发一个Java应用程序,允许用户编辑存储在文档中的参数数据,而不会破坏其格式(这样LabVIEW仍然可以读取它)。 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. 我希望用户能够通过其“
Name
字段选择其中一个父集群,然后使用存储在其中的数据填充表单,以便可以编辑此数据。 My problem is that using the DocumentBuilder
and Document
classes, I cannot seem to split out only the parent Cluster
nodes. 我的问题是使用
DocumentBuilder
和Document
类,我似乎无法拆分父Cluster
节点。
Working from the answer to parsing XML with NodeList and DocumentBuilder : 从使用NodeList和DocumentBuilder解析XML的答案开始:
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...
}
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. 我想我正在寻找一种方法来将我的XML文件表示为一个维护树结构的对象,然后生成一个只包含顶级
Cluster
元素的列表,然后每个元素都可以钻取以获取/设置它们的子Cluster
元素及其属性。
Thanks! 谢谢!
The Document
instance already represents the tree structure of the XML in memory. Document
实例已经表示内存中XML的树结构。 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: 如果需要顶级
Cluster
元素,可以获取XML根节点的子节点并循环遍历它们:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.