TreeItem cannot be cast to java.lang.String

I am using the following code to write some data to XML file in JAVA.

But I am getting "TreeItem cannot be cast to java.lang.String" error:
Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.widgets.TreeItem cannot be cast to java.lang.String

Open in new window

This is the code that I have:

    public void write2XML(Map<Integer, TreeItem> checkListMap, Map<Integer, String> checkNameMap, String file) {
        Iterator<Entry<Integer, TreeItem>> it_checkListMap = checkListMap.entrySet().iterator();
        Iterator<Entry<Integer, String>> it_checkNameMap = checkNameMap.entrySet().iterator();
        try {
          DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
          // root elements
          Document doc = docBuilder.newDocument();
          Element rootElement = doc.createElement("Company");
          while (it_checkListMap.hasNext()){
              Map.Entry pairs_checkListMap = (Map.Entry);
              // staff elements
              Element checkList = doc.createElement("Checks");
                  // firstname elements
                  Element CheckName = doc.createElement("Name");
                  CheckName.appendChild(doc.createTextNode((String) pairs_checkListMap.getValue()));

          // write the content into xml file
          TransformerFactory transformerFactory = TransformerFactory.newInstance();
          Transformer transformer = transformerFactory.newTransformer();
          DOMSource source = new DOMSource(doc);
          StreamResult result = new StreamResult(file);
          transformer.transform(source, result);
          System.out.println("File saved!");
        } catch (ParserConfigurationException pce) {
        } catch (TransformerException tfe) {
    public void getCheckedStandards(String file){
        TreeItem[] items = tree.getSelection();
        final Map<Integer, TreeItem> checkListMap;
        checkListMap = new HashMap<Integer, TreeItem>();
        final Map<Integer, String> checkNameMap;
        checkNameMap = new HashMap<Integer, String>();
        for (int i = 0; i < items.length; i++) {
            checkListMap.put(i, items[i]);
            //System.out.println("itemS: " + items[0]); 
            TreeItem[] child = items[i].getItems();
            for (int index=0; index < child.length; index++) { 
                if (child[index].getChecked()) {
                    System.out.println("item: " + index + ": " + child[index].getText()); 
                    checkNameMap.put(index, child[index].getText());
        write2XML(checkListMap, checkNameMap, file);

Open in new window

And this is the XML I am trying to create:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TolgarAuthor Commented:
Can you please help me to resolve this issue?

And also, can please check if my code will create the XML the way I showed in the question.

The error message seems pretty clear.  You can't cast a TreeItem object to String.  You can see that those aren't the same thing, so of course it won't work.

In terms of running your code to see what it will do -- you really need to do that yourself.  Have you tried it?  Does it produce the output you want?
TolgarAuthor Commented:
Yes, the error message is clear but is there a way to fix this error?

Since, I cannot run the code due to this error, I don't know if it produces the expected XML. But that's fine for now. I am only interested in fixing this issue.

This question is related with this open questions:

I am waiting for your reply.

Don't cast TreeItem to a String.

I'm not sure what else to tell you.  If you know that a node in the xml is a TreeIttem, you can try casting it -- but that certainly won't work.

You need to get all the pieces of data from the xml and assign them to fields in the TreeItem structure.

When you do that, you need to be sure that you're getting a TreeItem.  That usually means checking the xml tag name.

From a logic perspective, usually you read each line in the xml file, determine if the tagname is one you have code for, then execute the code for that tag.  For TreeItem, you would write code to read the xml elements and assign them appropriately into the new TreeItem you've instantiated.

Since you're reading the entire xml file into a document, you could also , in a loop, go to the tag which matches a specific tag name, and process each one.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.