📄 domtreetest.java
字号:
/**
@version 1.0 2001-09-01
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.lang.*;
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.show();
}
}
/**
This frame contains a tree that displays the contents of
an XML document.
*/
class DOMTreeFrame extends JFrame
{
public DOMTreeFrame()
{
setTitle("DOMTreeTest");
setSize(WIDTH, HEIGHT);
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
openFile();
}
catch(TransformerException e)
{
}
catch(IOException r)
{
}
}
});
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()throws TransformerException,IOException
{
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();
saveDocument(f);
}
private void saveDocument(File f)throws TransformerException,IOException
{
try
{
if (builder == null)
{
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
}
doc = builder.parse(f);
Element root = doc.getDocumentElement();
/*String back ="www";
Element nodeback = doc.createElement(back);
nodeback.setAttribute("hight","36");
Text text = doc.createTextNode(back);
nodeback.appendChild(text);
root.appendChild(nodeback);
*/
NodeList children = root.getChildNodes();
rootFirstTraverse(root);
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(doc),new StreamResult(new FileOutputStream(f)));
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (ParserConfigurationException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
catch (SAXException exception)
{
JOptionPane.showMessageDialog(this, exception);
}
}
private void rootFirstTraverse(Node node)
{
while(node!=null)
{
if(node.getFirstChild()==null)// && node instanceof Node
{
try
{
Text textNode =(Text)node;
String text = textNode.getData().trim();
//move out the blank characters
if(!text.equals(""))
{
//replace here with the function what you used to encription the text
System.out.println(text);
System.out.println(proc(text));
textNode.setNodeValue(proc(text));
}
}
catch(ClassCastException e)
{
//process the exception throwed when the node do not have text value
//System.out.println("此处为空!");
}
}//if
rootFirstTraverse(node.getFirstChild());
node=node.getNextSibling();
}//while
}
private String proc(String from)
{
String result;
result = from.substring(0,1);
return result;
}
private Document doc;
private Document newdoc;
private String name;
private int size;
private DocumentBuilder builder;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -