Results 1 to 3 of 3
  1. #1
    swetareddy is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Exclamation XML Parsing : use Recursive method.

    Please do help me friends ..Struggling a lot!!!

    I have an XML File :

    Java Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <workspaces>
      
         <workspace>
           <id>3</id>
           <name>Personal</name>
         </workspace>
         <workspace>
           <id>4</id>
    
           <name>Test1</name>
           <workspaces>
             <workspace>
                <id>5</id>
                <name>Child of Test1</name>
              </workspace>
           </workspaces>
    
         </workspace>
         <workspace>
           <id>6</id>
           <name>Test2</name>
           <workspaces>
             <workspace>
                <id>7</id>
    
                <name>Child of Test 2</name>
              </workspace>
           </workspaces>
         </workspace>
    </workspaces>
    The code I have written in such a way that it displays on a frame .

    But , I have aproblem : I have to write the code in a recursive manner becoz there may be sum situations in the project in future where i cannot guess the depth of the xml file.
    For now i get all the nodes in an order but actually 5 is the child of 4 . and even 7 is the child of 6.

    The code is as follows :

    Java Code:
    package sage;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.w3c.dom.CharacterData;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import java.io.*;
    import org.xml.sax.*;
    import javax.xml.parsers.*;
    import classes.sage.*;
    import javax.swing.tree.TreeNode;
    
    public class NextPage
    {
        JFrame frame;
        JPanel pamel;
        JButton okButton;
        JButton cancelButton;
        JScrollPane leftPane;
        JTextArea bottomArea;
        JTextArea leftArea;
        JTextArea topArea;
        MyTreeModel treeModel;
        MyTree treePane;
        IconNode treeNode;
    
        public NextPage()
        {
         frame = new JFrame("Upload Dialog Box");
         okButton = new JButton("OK");
         cancelButton = new JButton("Cancel");
         bottomArea = new JTextArea(10,20);
         bottomArea.setEditable(false);
         topArea = new JTextArea(5, 40);
         topArea.setEditable(false);
         bottomArea = new JTextArea();
         leftArea = new JTextArea();
         leftPane = new JScrollPane(leftArea);
    
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = null;
         try {
              db = dbf.newDocumentBuilder();
         }
         catch (Exception e)
         {
         }
         Document doc = null;
         try {
               doc = db.parse("http://dakota-swetha.cwru.edu/remote/workspaces/9HTTjw9qSVGiG2aGsCLC/");
            }
            catch (Exception e)
            {
            }
            doc.getDocumentElement().normalize();
    
            treeNode = new IconNode(doc.getDocumentElement().getNodeName(), "root");
            NodeList nodeLst = doc.getElementsByTagName("workspace");
            for (int s = 0; s < nodeLst.getLength(); s++)
            {
                Node fstNode = nodeLst.item(s); //Get workspace           
                //Get ID element
                Element E = (Element)fstNode;
                NodeList fstNmElmntLst = E.getElementsByTagName("id");
                Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                NodeList fstNm = fstNmElmnt.getChildNodes();
                IconNode tn2 = new IconNode(((Node) fstNm.item(0)).getNodeValue(), "child");
                treeNode.add(tn2);
    
                //Get Name element
                E = (Element)fstNode;
                fstNmElmntLst = E.getElementsByTagName("name");
                fstNmElmnt = (Element) fstNmElmntLst.item(0);
                fstNm = fstNmElmnt.getChildNodes();
                IconNode tn3 = new IconNode(((Node) fstNm.item(0)).getNodeValue(), "child");            
                tn2.add(tn3);
                treeNode.add(tn2);
            }
            treeModel = new MyTreeModel(treeNode);
            treePane = new MyTree(treeModel);
            frame.setLayout(null); // sets absolute layout
            //frame.setPreferredSize(new Dimension();
            frame.setPreferredSize(new Dimension(320, 500));
            frame.setResizable(false);
            okButton.setBounds(50, 400, 80, 30);
            cancelButton.setBounds(180, 400, 80, 30);
            treePane.setBounds(40, 80, 240, 260);
    
            frame.add(okButton);
            frame.add(cancelButton);
            frame.add(treePane);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            try {
                jbInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        public static void main(String[] argv)
        {
         NextPage frame = new NextPage();
         return;
        }
    
        private void jbInit() throws Exception {
        }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-26-2011 at 08:03 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by swetareddy View Post
    The code I have written in such a way that it displays on a frame .

    But , I have aproblem : I have to write the code in a recursive manner becoz there may be sum situations in the project in future where i cannot guess the depth of the xml file.
    For now i get all the nodes in an order but actually 5 is the child of 4 . and even 7 is the child of 6.

    Hello and welcome to the forum.

    What is your exact question? Have you tried creating your recursive method yet, and if so, can we see it? If not, I recommend you try it first and then show us what you have. Also, you seem to be combining XML parsing with a Swing class, and you really don't want to do that, but rather you want to do these very separate tasks in separate classes.

    Finally, I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  3. #3
    swetareddy is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    My exact problem is I need to get the info of that xml file as a tree structure on a fram.

    I am not know how to implement that recursively.
    I am trying to look down to the examples on the web but I am not able to get to it.
    Can you please help me with this.


    Thankyou

Similar Threads

  1. Recursive Descent Parsing in Java
    By hypnoticohm in forum New To Java
    Replies: 3
    Last Post: 10-06-2010, 04:14 PM
  2. recursive method
    By michail in forum New To Java
    Replies: 0
    Last Post: 01-31-2010, 01:50 PM
  3. help with BNF Grammar program using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 10-21-2009, 08:00 PM
  4. BNF grammar using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 02:40 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •