Help needed to save Jtree to xml

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 24 December 2012 - 09:55 AM

Quote

Well the xmltree is my tree from an other class were the tree is made so when

so this is the one you should encode
When you'll read it back pass it to your JTree as you do now
Was This Post Helpful? 0
  • +
  • -

#17 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 - 03:06 PM

I wil post some of my code tomorow were i made the jtree
thanks for helping
Was This Post Helpful? 0
  • +
  • -

#18 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 25 December 2012 - 02:28 PM

so this is the code for the tree



public class Treeviewer extends DefaultHandler
{
        private JTree xmlJTree;
	private DefaultTreeModel treeModel;
	int lineCounter;
	DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Viewer");
	 private WriteXmlfile write = new WriteXmlfile();
	JTextField txtFile = null;

        
        public void MakeTheTree(JTree tree)
        {
            treeModel = new DefaultTreeModel(base);
            xmlJTree = new JTree(treeModel);
        }
        
        //need to look at this for the save
        public void TreeToXml(File file)
        throws TransformerConfigurationException, TransformerException 
        {
        Document document = new Document() 
        {
        };
           
            treeModel = (DefaultTreeModel) getXmlJTree().getModel();
           
            
             write.WriteToXml(file, document);
            
            
            
        }  
        
        
	
	public void startElement(String uri, String localName, String tagName, Attributes attr) throws SAXException 
        {

		DefaultMutableTreeNode current = new DefaultMutableTreeNode(tagName);

		base.add(current);
		base = current;

		for (int i = 0; i < attr.getLength(); i++) 
                {
			DefaultMutableTreeNode currentAtt = new DefaultMutableTreeNode(attr.getLocalName(i) + " = "
					+ attr.getValue(i));
			base.add(currentAtt);
		}
	}

	public void skippedEntity(String name) throws SAXException
        {
		System.out.println("Skipped Entity: '" + name + "'");
	}

	
	public void startDocument() throws SAXException 
        {
		super.startDocument();
		 base = new DefaultMutableTreeNode("XML Viewer");
		((DefaultTreeModel) getXmlJTree().getModel()).setRoot(base);
	}

	public void characters(char[] ch, int start, int length) throws SAXException
        {

		String s = new String(ch, start, length).trim();
		if (!s.equals("")) 
                {
			DefaultMutableTreeNode current = new DefaultMutableTreeNode("beschrijving : " + s);
			base.add(current);

		}
	}

	public void endElement(String namespaceURI, String localName, String qName) throws SAXException 
        {

		base = (DefaultMutableTreeNode) base.getParent();
	}

	
	public void endDocument() throws SAXException 
        {
		// Refresh JTree
		((DefaultTreeModel) getXmlJTree().getModel()).reload();
		expandAll(getXmlJTree());
	}

	public void expandAll(JTree tree)
        {
		int row = 0;
		while (row < tree.getRowCount()) 
                {
			tree.expandRow(row);
			row++;
		}
	}

	public void xmlSetUp(File xmlFile) 
        {
		try {
			SAXParserFactory fact = SAXParserFactory.newInstance();
			SAXParser parser = fact.newSAXParser();
			parser.parse(xmlFile, this);

		} catch (Exception e) {

		}
        }

    /**
     * @return the xmlJTree
     */
    public JTree getXmlJTree() {
        return xmlJTree;
    }

    /**
     * @param xmlJTree the xmlJTree to set
     */
    public void setXmlJTree(JTree xmlJTree) {
        this.xmlJTree = xmlJTree;
    }

  
}



this is how i make the tree so in this class i want to make a save to xml using the
TreeToXml method
Was This Post Helpful? 0
  • +
  • -

#19 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 25 December 2012 - 09:48 PM

Last time I'll try to help you... the previous answers are quite clear or you missed the whole concept
It is not the JTree or its model that you should try to encode but the instance of the class that is the source of the JTree

You want to save on file the data from where the JTree is built NOT THE JTREE ITSELF what is the problem of understanding that simple difference
Was This Post Helpful? 0
  • +
  • -

#20 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 26 December 2012 - 02:53 AM

owk i get it to read it back but it still shows the swing objects how can i eliminate that?
thats the thing i dond get.
Was This Post Helpful? 0
  • +
  • -

#21 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 26 December 2012 - 01:30 PM

You encoded a JTree
A JTree is a fancy GUI component (using Swing) that display the data contained it it's model.

It is that data that you have to encode not the JTree or its model.
Was This Post Helpful? 0
  • +
  • -

#22 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 26 December 2012 - 02:40 PM

ic so its in my treeview class were i need to get the data to encode then if i get this right
Was This Post Helpful? 0
  • +
  • -

#23 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help needed to save Jtree to xml

Posted 26 December 2012 - 07:51 PM

after 25 replies where I tried to transmit the message you finally seem to understand... OUF !!!
Was This Post Helpful? 0
  • +
  • -

#24 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 26 December 2012 - 09:23 PM

Yeah sorry bout that but
i'm working on 3 diffrend projects atm and
i think i need more sleep...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2