当前位置: 代码迷 >> J2SE >> 联系XML和JAVA需要什么机子装写什么?该怎么解决
  详细解决方案

联系XML和JAVA需要什么机子装写什么?该怎么解决

热度:120   发布时间:2016-04-24 17:52:00.0
联系XML和JAVA需要什么机子装写什么?
联系XML和JAVA需要什么机子装写什么?
JAVA的开发工具我用JCREATOR,XML习惯用什么工具?

------解决方案--------------------
不需要装社么~~联系XML需要XML解析器(就是JAR包),现在解吸器有DOM JDOM DOM4J SAX
随便贴个例子你看看
/**
@version 1.10 2004-09-04
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
This program displays an XML document as a tree.
*/
public class DOMTreeTest
{
public static void main(String[] args)
{
JFrame frame = new DOMTreeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
This frame contains a tree that displays the contents of
an XML document.
*/
class DOMTreeFrame extends JFrame
{
public DOMTreeFrame()
{
setTitle( "DOMTreeTest ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

JMenu fileMenu = new JMenu( "File ");
JMenuItem openItem = new JMenuItem( "Open ");
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event) { openFile(); }
});
fileMenu.add(openItem);

JMenuItem exitItem = new JMenuItem( "Exit ");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event) { System.exit(0); }
});
fileMenu.add(exitItem);

JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}

/**
Open a file and load the document.
*/
public void openFile()
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File( ". "));

chooser.setFileFilter(new
javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.isDirectory() || f.getName().toLowerCase().endsWith( ".xml ");
}
public String getDescription() { return "XML files "; }
});
int r = chooser.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
try
{
if (builder == null)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
}

Document doc = builder.parse(f);
JTree tree = new JTree(new DOMTreeModel(doc));
tree.setCellRenderer(new DOMTreeCellRenderer());

setContentPane(new JScrollPane(tree));
validate();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e);
}
catch (ParserConfigurationException e)
{
JOptionPane.showMessageDialog(this, e);
}
catch (SAXException e)
{
JOptionPane.showMessageDialog(this, e);
}
}

private DocumentBuilder builder;
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400;
  相关解决方案