Java Dialog box to allow use to pick file
This is the code I have at present:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.io.File;
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 XMLReader extends JFrame
{
JFileChooser _fileChooser = new JFileChooser();
JPanel content = new JPanel();
//... Create menu elements (menubar, menu, menu item)
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
int retval = _fileChooser.showOpenDialog(XMLReader.this);
//... The user selected a file, get it, use it.
public static void main(String argv[])
{
ArrayList timeStamp = new ArrayList();
ArrayList Y = new ArrayList();
File file = XMLReader.this;
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("reading");
System.out.println("Share Data");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
.
.
.
}
I am trying to get a d开发者_运维技巧ialog box to allow the user to pick the XML they want parsed. I know the parsing works as I had hard coded in the file before.
I would also like to return the ArrayLists so that I can use them as the inputs to another class is this possible (at the moment I am only printing them to screen)?
System.out.println(timeStamp);
System.out.println(Y);
Can I use a return statement and if so how to I set up the class that I want to use them in?
The dialog box serve to retrive the path
I see that You are using the awt and dom so:
public Document loadXmlFile(Frame frame, DocumentBuilder docBuilder, String startPath) {
FileDialog fd = new FileDialog(frame, "Loadxml-title", FileDialog.LOAD);
//Add type filter
fd.setDirectory(startPath));
fd.show();
String file = fd.getFile();
if(file == null) {
return null;
}
return docBuilder.parse(file);
}
Q2: "Error message when compiling, can't have DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); with throw or catch exception."
This error is raised bu compiler because You don't have the block try & catch in method main.
The definition of DocumentBuilderFactory.newInstace(); says that this method throw the FactoryConfigurationError. In Java when method throw some error and it is not a RuntimeException you need to throw it in method definition or add the try catch block to handle it.
DocumentBuilderFactory dbf = null;
try {
dbf = DocumentBuilderFactory.newInstance();
} Exception(FactoryConfigurationError e){
e.printStackTrace();
}
精彩评论