设计:2个jcomboboxes,第2个框的列表取决于从第1个框的选择,来自XML的数据 [英] Design: 2 jcomboboxes, box 2 list depends on selection from box 1, data from XML

查看:52
本文介绍了设计:2个jcomboboxes,第2个框的列表取决于从第1个框的选择,来自XML的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决此问题的方法令我不知所措-这是经验不足的副产品.

I'm overwhelmed with attempting to design a solution for this problem - it's a by-product of inexperience.

我的目标是读取XML输入文件,存储来自XML的信息,并使用来自XML的数据填充两个组合框.第二个组合框的内容将根据第一个组合框的选择而更改.

My goal is to read an XML input file, store the information from the XML and populate two combo boxes with data from the XML. The content of the second combo box will change based on the selection in the first.

给出以下XML结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Node>
    <ID>Unique string</ID>
    <Name>Unique string</Name>
    <Code>Generic string<Code>
    <Kind>Generic string</Kind>
    <Frame>Generic string</Frame>
            ...
</Node>
    ...
</Category>

第一个组合框: 必须仅包含在种类"部分中找到的唯一值.

First combo box: Must contain only the unique values found in the the Kind section.

第二个组合框: 包含每个节点的种类等于在第一个组合框中选择的种类的所有节点的所有名称条目.

Second combo box: Contains ALL of the Name entries from every Node whose Kind equals the Kind selected in the first combo box.

关于XML源: 它是在外部维护和生成的. ID部分中的值将始终是唯一的. 名称"部分中的值将始终是唯一的. 该模式将(据说)永远不会改变. 将来,新的唯一值可能会出现在种类"部分.

Regarding the XML source: It is externally maintained and generated. The values in the ID section are always going to be unique. The values in the Name section are always going to be unique. The schema will (supposedly) never change. New unique values may appear in the Kind section in the future.

我建议的解决方案: 创建一个类XMLNode来表示来自XML源的Node. XMLNode类的成员对应于每个Node中的标签. 遍历所有节点,并为每个节点创建一个XMLNode. 在遍历节点时: 在哈希图中添加XMLNode对象,其键为= XMLNode.ID,而vals = XMLNode. 创建一组唯一的种类.

My proposed solution: Create a class XMLNode to represent a Node from the XML source. Members of class XMLNode correspond to the tags in each Node. Loop through all the Nodes and create an XMLNode for each one. While looping through nodes: Add XMLNode objects in a hash map, with Keys = XMLNode.ID and vals = the XMLNode. Create an array of unique Kinds.

从种类"条目数组中填充组合框一. 从名称数据中每个填充两个组合框.

Populate combo box one from the array of Kind entries. Populate combo box two from the Name data for each .

这是适当的方法,还是我忽略了更好/更容易/更优雅的解决方案? 如果我的工作方向正确,那么我提出的解决方案是否存在明显的缺陷?

Is this an appropriate approach, or have I overlooked a better/easier/more elegant solution? If I'm on the right track, are there any obvious flaws to my proposed solution?

推荐答案

我最终得到了NodeImporter类和Node类. Node代表一个节点,NodeImporter解析XML.在NodeImporter中,XML源被解析并表示为HashMaps的HashMap.外部HashMap(Key,Value)是(Kind,(HashMap(Key,Value)).内部HashMap(Key,Value)是(UinqueID,Node),以获得(Kind,(UniqueID,Node))的最终结果.我将最终结果称为"filteredMap",NodeImporter的所有字段和方法都是私有的,除了构造函数和filteredMap的getter之外. 需要数据来构建组合框的类从NodeImporter的实例获取filteredMap.然后,它可以获取外部HashMap的键以构建第一个ComboBoxModel.可以轻松地将内部HashMap用作第二个组合框的HashMap.

I ended up with a class NodeImporter and a class Node. Node represents a node, NodeImporter parses the XML. In the NodeImporter, the XML source is parsed and represented as a HashMap of HashMaps. The outer HashMap (Key,Value) is (Kind,(HashMap(Key,Value)). The inner HashMap (Key,Value) is (UinqueID,Node) for a final result of (Kind,(UniqueID,Node)). I call the final result "filteredMap". All of NodeImporter's fields and methods are private, except for the constructor, and a getter for filteredMap. The class that needs the data to build the combo boxes gets filteredMap from an instance of NodeImporter. It can then get the keys of the outer HashMap to build the first ComboBoxModel. It can just as easily get the inner HashMap to be used as the HashMap for the second combo box.

用于设置的伪代码:

使用类成员构建的类NodeImporter:

Class NodeImporter built with class members:

arrayList(String) uniqueKinds = null  
arrayList(Node) allNodes  = null  
HashMap(String, HashMap(String,Node))) filteredNodes = null  

NodeImporter类的构造函数:

Class NodeImporter constructor:

open XML  
while xml source has next  
  {  
  add next node to allNodes, key = node.uniqueId, Val = node  
  if next node.kind not in uniqueKinds, add node.kind to uniqueKinds  
  }



ClassNodeImporter method makeFilteredeMap:  

    private boolean makeFilteredeMap() {
        if (uniqueKinds.isEmpty()) {
            return false;
        } else {
            for (String k : uniqueKinds) {
                HashMap<String, Node> aMap = new HashMap<String, Node>();
                for (Node n : allNodes) {
                    if (n.getKind().equals(k)) {
                        aMap.put(n.getCode(), n);
                    }
                }
                filteredNodes.put(k, aMap);
            }
            return true;
        }
    }

这篇关于设计:2个jcomboboxes,第2个框的列表取决于从第1个框的选择,来自XML的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆