以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  Java&XML解析画表问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=19149)


--  作者:rocker
--  发布时间:6/2/2005 1:49:00 PM

--  Java&XML解析画表问题
1.XMLPainter.java文件
import java.awt.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import org.w3c.dom.*;

public class XMLPainter extends JTextArea
{
 String parsedFile; 
 int WIDTH=80,HEIGTH=60;
 int r,c;
 static Document parsedDocument;    
 public void paintComponent(Graphics g)
 {  
  super.paintComponent(g);
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  try
  {
   /*解析器属性设置*/
   
   dbf.setValidating(true);//解析器解析XML文件时对其进行验证

   dbf.setIgnoringComments(true);//忽略注释
   
   dbf.setIgnoringElementContentWhitespace(true);//忽略空白
      
   dbf.setNamespaceAware(true);//使解析器理解命名空间
   
   dbf.setExpandEntityReferences(true);//展开实体引用节点
   
   /*解析器属性设置*/
  
   DocumentBuilder db=dbf.newDocumentBuilder();   
   
   /*解析文件*/
   //System.out.println("XMLParser"); 
   parsedDocument = db.parse(new File("table.xml"));
   //System.out.println("XMLParserlast");  
  }
  catch(SAXException se)
  {
   Exception e = se;
   if(se.getException()!=null)
   {
    e = se.getException();
    e.printStackTrace();    
   }
  }
  catch(ParserConfigurationException pce)
  {
   pce.printStackTrace();
  }
  
  catch(IOException ie)
  {
   ie.printStackTrace();   
  } 
  catch(Exception e)
  {
   System.err.println(e.toString());
  }  
  try
  {    
   //System.out.println("paintComponentlast");
   Node root = parsedDocument.getDocumentElement();
   
   NodeList nodes = root.getChildNodes();   
   for(r=1;r<=nodes.getLength();r++)
   {    
    if(nodes.item(r-1).hasChildNodes())
    {
     NodeList childs = nodes.item(r-1).getChildNodes();
     for(c=1;c<=childs.getLength();c++)
     {
      int type = childs.item(c-1).getNodeType();
      if(type == Node.ELEMENT_NODE)
      {
       g.drawLine((c-1)*WIDTH,(r-1)*HEIGTH,(c-1)*WIDTH,r*HEIGTH);
       g.drawLine((c-1)*WIDTH,(r-1)*HEIGTH,c*WIDTH,(r-1)*HEIGTH);
       g.drawString(childs.item(c-1).getFirstChild().getNodeValue(),(c-1)*WIDTH,(r-1)*HEIGTH);
      }
     }
    }
   }
   g.drawLine(0,r*HEIGTH,c*WIDTH,r*HEIGTH);
   g.drawLine(c*WIDTH,0,c*WIDTH,r*HEIGTH);
     
  }
     catch(Exception e)
     {
      //System.err.println(e.toString());
     }

 }
 public void draw(String tableToDraw)
 {  
  parsedFile = tableToDraw;
  System.out.println("draw");
  //repaint();  
  
  
 }
}

2.UserGUI.java文件
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class UserGUI extends JApplet implements ActionListener
{  
 JButton printButton,previewButton,pagesetButton;
 JPanel buttonPanel;
 XMLPainter customPanel;
 //ParsePainter testPanel; 
 Container container = getContentPane();
 JScrollPane scroll;
    
 public void init()
 {//

   
  printButton = new JButton("打印");
  printButton.addActionListener(this);  
  
  previewButton = new JButton("打印预览");
  previewButton.addActionListener(this);     
  
  pagesetButton = new JButton("页面设置");
  pagesetButton.addActionListener(this);  
  
  buttonPanel = new JPanel();
  buttonPanel.setBackground(Color.white);
  
  customPanel = new XMLPainter();
  customPanel.setBackground(Color.white);  
  customPanel.draw("table.xml");
  
  scroll = new JScrollPane(customPanel); 
  
  
  buttonPanel.add(printButton);
  buttonPanel.add(previewButton);
  buttonPanel.add(pagesetButton); 
  
  container.add(buttonPanel,BorderLayout.SOUTH);
  container.add(scroll,BorderLayout.CENTER);
   
  //setSize(800,600);
  
  
    
  
  
 } 
 
 public void actionPerformed(ActionEvent evt)
    {//
        Object src = evt.getSource();
        if (src == printButton)
            printAction();
        else if (src == previewButton)
            previewAction();    
        else if (src == pagesetButton)
            pagesetAction();         
    }
 
 private void printAction()
 {//  
  JOptionPane.showConfirmDialog(null, "对不起!! 打印内容不能为空,打印已取消", "内容不能为空",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
 } 
 
 private void previewAction()
 {
  JOptionPane.showMessageDialog(null,"完善中","完善中",JOptionPane.CLOSED_OPTION);

  //printButton.setLabel(pagesetButton.getLabel());
 }
 private void pagesetAction()
 {
  //JOptionPane.showMessageDialog(null,"完善中","完善中",JOptionPane.CLOSED_OPTION);
  //pagesetButton.setLabel(previewButton.getLabel());
 } 
 
}


程序一到解析就不能运行了 捕捉异常抛出个accesscontrolexception异常 请高手指点


[此贴子已经被作者于2005-6-4 14:17:26编辑过]

--  作者:rocker
--  发布时间:6/4/2005 2:11:00 PM

--  
都两天了 怎么没一个人吭声啊
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms