// Swing-related variables and methods, including // setting up a JTree and basic content pane public void buildTree(DefaultTreeModel treeMode1, DefaultMutableTreeNode base, String xmlURI) throws IOException, SAXException { //creat instances for parser XMLReader reader = XMLReaderFactory.createXMLReader( ); ContentHandler jTreeHandler = new jTreeHandler(treeMode1, base); //register contentHandler reader.setContentHandler(jTreeHandler); //register ErrorHandler
//parse InputSource inputSource = new InputSource( new FileInputStream(new File("xml.xml"))); inputSource.setSystemId("xml.xml"); reader.parse(inputSource); }//end of buildTree
viewer.init(args[0]); viewer.setVisible(true); } catch(Exception e) { e.printStackTrace(); } }//end of main class jTreeHandler implements ContentHandler { /** tree mode1 to add tree nodes to **/ private DefaultTreeModel treeMode1;
/**current node to add sub-node to **/ private DefaultMutableTreeNode current;
/**Locator**/ private Locator locator;
/**HashMap**/ private Map namespaceMappings;
public jTreeHandler(DefaultTreeModel treeMode1, DefaultMutableTreeNode base) { this.treeMode1 = treeMode1; this.current = base; this.namespaceMappings = new HashMap(); }
//contentHandler callback implementations
//constructor public void setDocumentLocator(Locator locator) { //save this for later use this.locator = locator; }
public void startDocument()throws SAXException { //no visual event occur here }
public void endDocument() throws SAXException { //No visual event occur here }
public void processingInstruction(String target, String data) throws SAXException { DefaultMutableTreeNode pi = new DefaultMutableTreeNode("PI(target = '" + target +"', data = '" + data +"')"); current.add(pi); }
public void startPrefixMapping(String prefix, String uri) { //no visual event occur here namespaceMappings.put(uri, prefix); }//end of startPrefixMapping
public void endPrefixMapping(String prefix) { for(Iterator i = namespaceMappings.keySet().iterator(); i.hasNext();) { String uri = (String)i.next( );
public void endElement(String namespaceURI, String localName,String qName)throws SAXException { // Walk back up the tree current = (DefaultMutableTreeNode)current.getParent( ); }
public void characters(char[] ch, int start, int length) throws SAXException { String s = new String(ch, start, length); DefaultMutableTreeNode data = new DefaultMutableTreeNode("Character Data: '" + s + "'"); current.add(data); } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
// This is ignorable, so don't display it } public void skippedEntity(String name) throws SAXException { DefaultMutableTreeNode skipped = new DefaultMutableTreeNode("Skipped Entity: '" + name + "'"); current.add(skipped); }
}//end of JTreeHandler}//end of TestSAX这是全部的代码 谢谢
TestSAX viewer = new TestSAX(); viewer.init(args[0]); 你用viewer来调用init()放法,那init()这个方法体在哪?
给TestSAX 加一个方法 public void init(String args){ ... }
我今天刚找到他的 init的源码 我贴在这 需要的下载 谢谢楼上的朋友帮忙 public void init(String xmlURI) throws IOException, SAXException { DefaultMutableTreeNode base = new DefaultMutableTreeNode( "XML Document: " + xmlURI); // Build the tree model defaultTreeModel = new DefaultTreeModel(base); jTree = new JTree(defaultTreeModel); // Construct the tree hierarchy buildTree(defaultTreeModel, base, xmlURI); // Display the results getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER); }
import java.util.Stack;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.IOException;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.awt.MenuContainer;
import java.awt.GraphicsConfiguration;
import java.awt.Frame;
public class TestSAX extends JFrame {
// Swing-related variables and methods, including
// setting up a JTree and basic content pane public void buildTree(DefaultTreeModel treeMode1,
DefaultMutableTreeNode base, String xmlURI)
throws IOException, SAXException
{
//creat instances for parser
XMLReader reader = XMLReaderFactory.createXMLReader( );
ContentHandler jTreeHandler = new jTreeHandler(treeMode1, base);
//register contentHandler
reader.setContentHandler(jTreeHandler); //register ErrorHandler
//parse
InputSource inputSource = new InputSource(
new FileInputStream(new File("xml.xml")));
inputSource.setSystemId("xml.xml");
reader.parse(inputSource);
}//end of buildTree
public static void main(String[] args)
{
try
{
if(args.length==1)
{
System.out.println("Usage : java javaXml12 SAXTreeView" + "[XMLDcument]");
return ;
}
TestSAX viewer = new TestSAX();
viewer.init(args[0]);
viewer.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}//end of main
class jTreeHandler implements ContentHandler
{
/** tree mode1 to add tree nodes to **/
private DefaultTreeModel treeMode1;
/**current node to add sub-node to **/
private DefaultMutableTreeNode current;
/**Locator**/
private Locator locator;
/**HashMap**/
private Map namespaceMappings;
public jTreeHandler(DefaultTreeModel treeMode1, DefaultMutableTreeNode base)
{
this.treeMode1 = treeMode1;
this.current = base;
this.namespaceMappings = new HashMap();
}
//contentHandler callback implementations
//constructor
public void setDocumentLocator(Locator locator)
{
//save this for later use
this.locator = locator;
}
public void startDocument()throws SAXException
{
//no visual event occur here
}
public void endDocument() throws SAXException
{
//No visual event occur here
}
public void processingInstruction(String target, String data) throws SAXException
{
DefaultMutableTreeNode pi =
new DefaultMutableTreeNode("PI(target = '" + target +"', data = '" + data +"')");
current.add(pi);
}
public void startPrefixMapping(String prefix, String uri)
{
//no visual event occur here
namespaceMappings.put(uri, prefix);
}//end of startPrefixMapping
public void endPrefixMapping(String prefix)
{
for(Iterator i = namespaceMappings.keySet().iterator();
i.hasNext();)
{
String uri = (String)i.next( );
String thisPrefix = (String) namespaceMappings.get(uri);
if(prefix.equals(thisPrefix))
{
namespaceMappings.remove(uri);
break;
}
}
}//end of endPrefixMapping
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)throws SAXException
{
DefaultMutableTreeNode element = new DefaultMutableTreeNode("Element :" + localName);
current.add(element);
current = element;
//determine namespace
if(namespaceURI.length() > 0 )
{
String prefix =(String)namespaceMappings.get(namespaceURI);
if(prefix.equals(""))
prefix ="[None]"; DefaultMutableTreeNode namespace =
new DefaultMutableTreeNode("Namespace: prefix = '" +
prefix + "', URI = '" + namespaceURI + "'");
current.add(namespace);
}
//process attributes
for(int i= 0; i < atts.getLength(); i++)
{
DefaultMutableTreeNode attribute =
new DefaultMutableTreeNode(("Attribute (name = '" +
atts.getLocalName(i) + "', value = '" + atts.getValue(i) + "')"));
String attURI = atts.getURI(i);
if (attURI.length( ) > 0)
{
String attPrefix = (String)namespaceMappings.get(attURI);
if (attPrefix.equals(""))
{
attPrefix = "[None]";
}
DefaultMutableTreeNode attNamespace =
new DefaultMutableTreeNode("Namespace: prefix = '" +
attPrefix + "', URI = '" + attURI + "'");
attribute.add(attNamespace);
}
current.add(attribute);
}
}
public void endElement(String namespaceURI,
String localName,String qName)throws SAXException
{
// Walk back up the tree
current = (DefaultMutableTreeNode)current.getParent( );
}
public void characters(char[] ch, int start, int length)
throws SAXException { String s = new String(ch, start, length);
DefaultMutableTreeNode data =
new DefaultMutableTreeNode("Character Data: '" + s + "'");
current.add(data);
} public void ignorableWhitespace(char[] ch,
int start, int length) throws SAXException
{
// This is ignorable, so don't display it
} public void skippedEntity(String name) throws SAXException
{
DefaultMutableTreeNode skipped =
new DefaultMutableTreeNode("Skipped Entity: '" + name + "'");
current.add(skipped);
}
}//end of JTreeHandler}//end of TestSAX这是全部的代码 谢谢
viewer.init(args[0]); 你用viewer来调用init()放法,那init()这个方法体在哪?
谢谢你了 我再试试
public void init(String args){
...
}
public void init(String xmlURI) throws IOException, SAXException {
DefaultMutableTreeNode base = new DefaultMutableTreeNode(
"XML Document: " + xmlURI); // Build the tree model
defaultTreeModel = new DefaultTreeModel(base);
jTree = new JTree(defaultTreeModel); // Construct the tree hierarchy
buildTree(defaultTreeModel, base, xmlURI); // Display the results
getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
}