remove duplicate xml elements from doc builder list in java

Dan Pickard

Here's what I'm trying to do:

  • load in an XML file, then scan the XML file to put all the elements in a combobox so that a user can select one.

I've got this working up to a point in the fact that there are several elements with the same tags within the body of the XML file itself, and thus, the element occurs more than once in the drop down list, is there a way in my for loop to compare what's already there and remove duplicates?

Here's the entire method I've got

     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();
        }
    }


EDIT:

A second problem I faced was to make sure that all the elements were in alphabetical order. I fixed this issue by doing the following:

// 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);
}

Sufiyan Ghori

declare an ArrayList,

If you are using Java 7,

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

or if you are using earlier version of Java,

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

inside your for loop,

           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);
                }
            }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove contiguous duplicate elements from an array list in Java (with panache)

From Dev

Remove case sensitive duplicate elements from list in java

From Dev

Remove contiguous duplicate elements from an array list in Java (with panache)

From Dev

Python remove duplicate elements from xml tree

From Dev

Python remove duplicate elements from xml tree

From Dev

remove duplicate elements from list in python

From Dev

Java duplicate elements in a list

From Dev

remove elements from XML file in java

From Dev

Remove consecutive elements from string list JAVA

From Dev

Java 8 remove duplicate strings irrespective of case from a list

From Dev

Remove Duplicate Captures from List

From Dev

Remove duplicate sublists from a list

From Dev

Create a list of duplicate elements from an existing list

From Dev

Cannot remove duplicate elements from arraylist of hashmap

From Dev

Remove duplicate elements from HTML string

From Dev

remove duplicate elements from two numpy arrays

From Dev

To remove duplicate elements from an array in Perl

From Dev

Java 8 - Remove repeated sequence of elements from a List

From Dev

How to remove elements from a list?

From Dev

Remove elements from a list C

From Dev

Remove duplicates from list elements

From Dev

Remove older elements from a list

From Dev

Compare and remove elements from list

From Dev

Remove duplicate subcategory block items from xml

From Dev

How to remove adjacent duplicate elements in a list using list comprehensions?

From Dev

Remove duplicate from a list of type class

From Dev

Remove duplicate common output from two list

From Java

Remove duplicate items from KeyValuePair List by Value

From Dev

Remove duplicate and small vectors from list

Related Related

  1. 1

    Remove contiguous duplicate elements from an array list in Java (with panache)

  2. 2

    Remove case sensitive duplicate elements from list in java

  3. 3

    Remove contiguous duplicate elements from an array list in Java (with panache)

  4. 4

    Python remove duplicate elements from xml tree

  5. 5

    Python remove duplicate elements from xml tree

  6. 6

    remove duplicate elements from list in python

  7. 7

    Java duplicate elements in a list

  8. 8

    remove elements from XML file in java

  9. 9

    Remove consecutive elements from string list JAVA

  10. 10

    Java 8 remove duplicate strings irrespective of case from a list

  11. 11

    Remove Duplicate Captures from List

  12. 12

    Remove duplicate sublists from a list

  13. 13

    Create a list of duplicate elements from an existing list

  14. 14

    Cannot remove duplicate elements from arraylist of hashmap

  15. 15

    Remove duplicate elements from HTML string

  16. 16

    remove duplicate elements from two numpy arrays

  17. 17

    To remove duplicate elements from an array in Perl

  18. 18

    Java 8 - Remove repeated sequence of elements from a List

  19. 19

    How to remove elements from a list?

  20. 20

    Remove elements from a list C

  21. 21

    Remove duplicates from list elements

  22. 22

    Remove older elements from a list

  23. 23

    Compare and remove elements from list

  24. 24

    Remove duplicate subcategory block items from xml

  25. 25

    How to remove adjacent duplicate elements in a list using list comprehensions?

  26. 26

    Remove duplicate from a list of type class

  27. 27

    Remove duplicate common output from two list

  28. 28

    Remove duplicate items from KeyValuePair List by Value

  29. 29

    Remove duplicate and small vectors from list

HotTag

Archive