java - 从 java 中的文档生成器列表中删除重复的 xml 元素

标签 java xml compare

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

  • 加载 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);
                }
            }

关于java - 从 java 中的文档生成器列表中删除重复的 xml 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783572/

相关文章:

android - 无法从警报对话框中的 EditText View 获取数据

xml - XML 包含多个命名空间 - 但它只有一个

java - 给定这段代码我将如何排序

java - 如何编译 jrxml 以获取 jasper?

java - Cucumber:场景中的使用文件

java - 如何使用 JAXB 将缺失的元素解码为空对象

java - 在for循环中匹配两个char类型数组,for循环无法正确执行

Python比较元组

Java 对象差异 : How to apply diffs to an object

java - 为什么 JTable 不显示用户输入?