• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to add XML Data into Jtext area

 
Santosh Maskar
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have two application one is reading Xml file another is to display the Data on Swing componant using JLable , but i want to show this data using JText Area, but this function have the String Parameter and my XML Program is returning the SAXParser , so how can i will sow the data which is returning from XML parser into the Swings JTextArea.


here is sample code for reading the SAML file
import java.io.IOException;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class BasicSAX extends DefaultHandler
{

public BasicSAX(String xmlFile)
{
SAXParser parser = new SAXParser();

parser.setContentHandler(this);

try
{
parser.parse(xmlFile);
}
catch (SAXException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}

/*public void startElement(String uri, String local, String qName, Attributes atts)
{
System.out.println(local);
}
*/
public void characters(char[ ] ch, int start, int length)
{
System.out.println(new String(ch, start, length));
}

public String XmlData (String mstrFileName)
{
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(this);
try
{
saxParser.parse(mstrFileName);
}catch(SAXException saxEx)
{
saxEx.printStackTrace();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}

return "aaa";
}
}

Here is anotehr example where i need to show the XML output iin JTextArea

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import com.pspl.xml.BasicSAX;


public class XMLFile extends JFrame
{

public XMLFile()
{
super("File Chooser Test Frame");
setSize(500, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);

Container c = getContentPane();
c.setLayout(new FlowLayout());

JButton openButton = new JButton("Open File ");
final JTextArea txtarea = new JTextArea(20,30);
txtarea.setAutoscrolls(true);
txtarea.setEditable(false);


final JLabel statusbar = new JLabel("");

// Create a file chooser that opens up as an Open dialog
openButton.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
int option = chooser.showOpenDialog(XMLFile.this);
String abc = "abc";
if (option == JFileChooser.APPROVE_OPTION)
{
File sf = chooser.getSelectedFile();
File f= null;
String filelist = "nothing";
try
{
filelist = sf.getCanonicalPath();
}
catch(Exception Ex)
{
Ex.printStackTrace();
}

BasicSAX basicSAX = new BasicSAX(filelist);

txtarea.append(abc);
}
else
{
statusbar.setText("You canceled.");
}
}
});

c.add(openButton);
c.add(statusbar);
c.add(txtarea);
}

public static void main(String args[])
{
XMLFile sfc = new XMLFile();
sfc.setVisible(true);
}
}

Regards
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML is just a text file. To display it in a text area you just need to read it in using a reader something like...

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!