Help needed to save Jtree to xml

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 6310 Views - Last Post: 26 December 2012 - 09:23 PM Rate Topic: -----

#1 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Help needed to save Jtree to xml

Posted 20 December 2012 - 02:36 PM

hi guys i have a programme that can read out the xml file into a jtree en display it on the screen.
now i can make changes to the tree (edit it) but the problem is i have no clue how to save the edited tree again wen i press the save button please help.
Is This A Good Question/Topic? 0
  • +

Replies To: Help needed to save Jtree to xml

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Help needed to save Jtree to xml

Posted 20 December 2012 - 03:03 PM

You could try using an XMLEncoder to write the TreeModel to a File.
Was This Post Helpful? 0
  • +
  • -

#3 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 20 December 2012 - 03:14 PM

i try that like this
  XMLEncoder encoder = new XMLEncoder(
                          new BufferedOutputStream(
                              new FileOutputStream(file)));
                         encoder.writeObject(xmlJTree);
                          encoder.close();


but it dond work :(
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Help needed to save Jtree to xml

Posted 20 December 2012 - 03:15 PM

It doesn't work how? Please be specific.
Was This Post Helpful? 0
  • +
  • -

#5 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 20 December 2012 - 03:20 PM

wel i use this
  if (e.getSource() == btnSave)
            {
                       JFileChooser fc = new JFileChooser();
                       int option = fc.showSaveDialog(ShowTree.this);

                          if (option == JFileChooser.APPROVE_OPTION) 
                         {
                          try 
                          {
                            String filename = fc.getSelectedFile().getName();
                            String path = fc.getSelectedFile().getParentFile()
                                        .getPath();

                        int len = filename.length();
                        String ext = "";
                        File file = null;

                        if (len > 4) 
                        {
                            ext = filename.substring(len - 4, len);
                        }

                        if (ext.equals(".xml")) 
                        {
                            file = new File(path, filename);
                        } else {
                            file = new File(path, filename + ".xml");
                        }
                       XMLEncoder encoder = new XMLEncoder(
                          new BufferedOutputStream(
                              new FileOutputStream(file)));
                         encoder.writeObject(xmlJTree);
                          encoder.close();


to open a save location but wen i enter a path to save to i saves the file but its empty :s
Was This Post Helpful? 0
  • +
  • -

#6 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 21 December 2012 - 03:41 PM

owk i got this to work but my xml looks so strange like this
<?xml version="1.0" encoding="UTF-8"?>
-<java class="java.beans.XMLDecoder" version="1.7.0_10">-<object class="javax.swing.tree.DefaultTreeModel">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>XML Viewer</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>formulieren</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>form</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>taal = frans</string></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label1</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : bedrag</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label2</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : procent</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label3</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : jaren</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label4</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : Bereken</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label5</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : resultaat</string></void></object></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>form</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>taal = E</string></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label1</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : amount</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label2</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : procent</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label3</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : Years</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label4</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : Calculate</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label5</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : result</string></void></object></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>form</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>taal = F</string></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label1</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : montant</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label2</string></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>beschrijving : procent</string></void></object></void></object></void>-<void method="add">-<object class="javax.swing.tree.DefaultMutableTreeNode">-<void property="userObject"><string>label3</string></void></object></void></object></void></object></void></object></object></java>



using the xmlencoder
 try (XMLEncoder encoder = new XMLEncoder(
                                         new BufferedOutputStream(
                                             new FileOutputStream(file)))) {
                                     encoder.writeObject(xmlJTree.getModel());
                                     encoder.close();


can i fix the void things and add so it will only return the tree?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 23 December 2012 - 06:55 PM

View Postalriske, on 21 December 2012 - 06:41 PM, said:

owk i got this to work but my xml looks so strange like this

Can you define what you qualify as "so strange" ?
Was This Post Helpful? 0
  • +
  • -

#8 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 23 December 2012 - 09:28 PM

Wel i just whant it to write the tree nodes away
parrent and child nothing els
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 23 December 2012 - 09:38 PM

encoder.writeObject(xmlJTree); 


but what defines an xmlJTree ? That is what is saved. You will to narrow down the object you want to save
Was This Post Helpful? 0
  • +
  • -

#10 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 02:14 AM

Wel the xmltree is my tree from an other class were the tree is made so wen
you read out a file it fils the three
Was This Post Helpful? 0
  • +
  • -

#11 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 07:49 AM

in my prev programme i had to use a jTable to write things away so i made a class like this

  public void WriteToXml(File file,Document document)
    {
         try 
        {
            FileWriter writer = new FileWriter(file);
            XMLOutputter outputter = new XMLOutputter();

            //
            // Set the XLMOutputter to pretty formatter. This formatter
            // use the TextMode.TRIM, which mean it will remove the
            // trailing white-spaces of both side (left and right)
            //
            outputter.setFormat(Format.getPrettyFormat());

            //
            // Write the document to a file and also display it on the
            // screen through System.out.
            //
            outputter.output(document, writer);
            outputter.output(document, System.out);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }



and this to write it

  public void toXml(File file)
        throws TransformerConfigurationException, TransformerException 
     {
        Document document = new Document() 
        {
        };

        Element root = new Element("Printers");

        model = (DefaultTableModel) getTable().getModel();
        //DEBUG
        System.out.printf("row count = %d, col count = %d\n",
            model.getRowCount(), model.getColumnCount());

        for (int row = 0; row < model.getRowCount(); row++)
        {
            //
            // Creating a child for the root element. Here we can see how to
            // set the text of an xml element.
            //
            Element child = new Element("Printer");

            for (int col = 0; col < model.getColumnCount(); col++)
            {
                child.addContent(new Element(ELEMENT_NAMES[col]).setText(
                        model.getValueAt(row, col).toString()));
            } // end col iteration
              //
              // Add the child to the root element and add the root element as
              // the document content.
              //

            root.addContent(child);
        } // end row iteration

        document.setContent(root);
        write.WriteToXml(file, document);
     }



i whant to do the same thing for the jtree but i dond know how

This post has been edited by alriske: 24 December 2012 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 08:45 AM

View Postalriske, on 24 December 2012 - 05:14 AM, said:

Wel the xmltree is my tree from an other class were the tree is made so wen
you read out a file it fils the three

So it is that class that you want to save to disk, not the JTree with all its Swing components
Was This Post Helpful? 0
  • +
  • -

#13 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 09:11 AM

yup just the tree and the info of the tree not the swing :)
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 09:30 AM

Is this a question ? Because if it is the answer is in my previous post.
Was This Post Helpful? 0
  • +
  • -

#15 alriske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 15-December 12

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 09:35 AM

this you mean encoder.writeObject(xmlJTree); ?
problem is that this saves the swing things to
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2