Hey Friends,
I have an issue in developing a applet whose purpose is to read xml file and show it in to applet.
For that purpose i was trying to write a program which contains an Text box and a XML read function which read xml file and then set content to Text box.
What i had done is:
I had created a jar file consisting of the following files
jar cfv MyAppletJar.jar DemoTest.class Company.xml
1) Applet class
import javax.swing.*; import java.io.File; import java.awt.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DemoTest extends JApplet{ JTextField tf; public void init(){ try { tf = new JTextField(20); add(tf); System.out.println(DemoTest.createTestX("Company.xml")); } catch (Exception e) { System.err.println("createGUI didn't complete successfully"+e); } } public static String createTestX(String filePath) { try { File file = new File(filePath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element " + doc.getDocumentElement().getNodeName()); NodeList nodeLst = doc.getElementsByTagName("employee"); System.out.println("Information of all employees"); return doc.getDocumentElement().getNodeName(); } catch (Exception e) { System.out.println("File Reading error"); e.printStackTrace(); } return null; } }
2) Xml file
<?xml version="1.0"?> <company> <employee> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee> <firstname>Paul</firstname> <lastname>Enderson</lastname> </employee> <employee> <firstname>George</firstname> <lastname>Bush</lastname> </employee> </company>
after this i had created an html page embed with applet tag to embed applet.
<HTML> <Head> <Title>Test XML </Title> <Body> <APPLET CODE="DemoTest.class" width="100" height="100" archive="MyAppletJar.jar"> </APPLET> </Body> </HTML>
But its throwing Java.security.accesscontrolException
I can understand the problem in reading file from client system , but the file is available within the same jar file in which applet class is available .
Now i m not getting what to do
Please help me out.