从 Java 中的文档生成器列表中删除重复的 XML 元素



这是我正在尝试做的事情:

  • 加载 XML 文件,然后扫描 XML 文件以将所有元素放在组合框中,以便用户可以选择一个元素。
我已经做到了这一点,因为

XML 文件本身的正文中有几个具有相同标签的元素,因此,该元素在下拉列表中出现不止一次,在我的 for 循环中有没有办法比较已经存在的内容并删除重复项?

这是我拥有的整个方法

     public static void readXML(String filePath) {
        try {
            //Gets selected XML file
            File XmlFile = new File(filePath);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(XmlFile);
            //Searches all text
            doc.getDocumentElement().normalize();
            //Make a non-editable combo box
            JComboBox<String> comboBox = new JComboBox<String>();
            comboBox.setEditable(false);
            //Get all the XML elements from the file
            NodeList list = doc.getElementsByTagName("*");
            //TODO:
            //Make sure all XML elements only appear once in the list
            //Populate combobox with all elements from input file
            for (int i = 0; i < list.getLength(); i++) {
                Element element = (Element)list.item(i);                
                String item = element.getNodeName().toString();
                //Add comparison here??
                comboBox.addItem(item);
            }
            //Add Combo box and refresh the frame window so that it appears
            buttonPanel.add(comboBox);
            frame.revalidate();
            //Add action listener show which XML element has been selected
            comboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    //Get the source of the component, which is the combo box
                    JComboBox<?> comboBox = (JComboBox<?>) event.getSource();
                    //Print the selected item
                    String selected = comboBox.getSelectedItem().toString();
                    log.append("The selected XML element is: " + selected + newline);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

编辑:

我面临的第二个问题是确保所有元素都按字母顺序排列。我通过执行以下操作解决了这个问题:


// Make a sublist so that the elements can be sorted
List<String> subList = allValues.subList(0, allValues.size()); Collections.sort(subList);


// Add the items from the subList to the comboBox
for (int j = 0; j < subList.size(); j++) {
String listItem = subList.get(j).toString();
comboBox.addItem(listItem); }

声明一个 ArrayList,

如果您使用的是 Java 7,

        ArrayList<String> allValues = new ArrayList<>();

或者如果您使用的是早期版本的 Java,

        ArrayList<String> allValues = new ArrayList<String>();

在你的for循环内,

           for (int i = 0; i < list.getLength(); i++) {
                Element element = (Element)list.item(i);                
                String item = element.getNodeName().toString();
                if (!allValues.contains(item)){
                     comboBox.addItem(item);
                     allValues.add(item);
                }
            }

相关内容

  • 没有找到相关文章