高分求助---XML无限级扩展的支持?
我的想法是,XML文件包含2种类型的节点:简单节点和复杂节点
简单节点只有1个属性,作为Hashtable的Key,值作为Hashtable的value
复杂节点可以包含简单节点,复杂节点下面又可以包含复杂节点(这里是不是还需要1个属性标识是否包含复杂节点呢?---这个,,,还没有想好)
复杂节点的一个属性仍作为Hashtable的Key,这个Hashtable的Value是简单节点的集合
已经实现了复杂节点包含简单节点,(程序如下),但是如何扩展能够支持复杂节点继续包含复杂节点(递归)?
请大虾们不吝赐教!感激不尽!!!
slideSample01.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE system SYSTEM "slideshow.dtd">
<system>
<item key="A">a</item>
<title key="G">
<item key="E">e</item>
<item key="F">f</item>
</title>
<title key="D">
<item key="B">b</item>
<item key="C">c</item>
</title>
</system>
========================================================================
import java.io.*;
import java.util.Hashtable;
import java.util.Properties;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class Xml extends DefaultHandler
{
StringBuffer textBuffer;
static Hashtable hashtable = new Hashtable();
static Hashtable tempTable = new Hashtable();
boolean isTitle = false;
String key = new String();
String value = new String();
String tempKey = new String();
public static void main(String argv[])
{
DefaultHandler handler = new Xml();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
try {
// Set up output stream
out = new OutputStreamWriter(System.out, "GB2312");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File("slideSample01.xml"), handler);
System.out.println(hashtable.toString());
System.out.println((hashtable.get("D")).toString());
} catch (Throwable t)
{
t.printStackTrace();
}
//System.exit(0);
}
static private Writer out;
//===========================================================
// SAX DocumentHandler methods
//===========================================================
public void startDocument()
throws SAXException
{
emit("<?xml version='1.0' encoding='GB2312'?>");
nl();
}
public void endDocument()
throws SAXException
{
try {
nl();
out.flush();
System.out.println(hashtable.containsKey("A"));
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void startElement(String namespaceURI,
String sName, // simple name
String qName, // qualified name
Attributes attrs)
throws SAXException
{
String aName = new String();
echoText();
String eName = sName; // element name
if ("".equals(eName)) eName = qName; // not namespaceAware
emit("<"+eName);
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName)) aName = attrs.getQName(i);
emit(" ");
emit(aName+"=\""+attrs.getValue(i)+"\"");
}
}
emit(">");
if(eName.equals("title")) {
isTitle = true;
key = attrs.getValue(0);
return;
}
if(isTitle) tempKey = attrs.getValue(0);
else if(eName.equals("item")) key = attrs.getValue(0);
}
public void endElement(String namespaceURI,
String sName, // simple name
String qName // qualified name
)
throws SAXException
{
echoText();
String eName = sName; // element name
if ("".equals(eName)) eName = qName; // not namespaceAware
emit("</"+eName+">");
if(eName.equals("title")) {
isTitle = false;
hashtable.put(key,tempTable.clone());
tempTable.clear() ;
return;
}
if(isTitle){
tempTable.put(tempKey,value);
}
if(eName.equals("item"))
hashtable.put(key,value);
}
public void characters(char buf[], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
} else {
textBuffer.append(s);
}
value = s;
}
//===========================================================
// SAX ErrorHandler methods
//===========================================================
// treat validation errors as fatal
public void error(SAXParseException e)
throws SAXParseException
{
throw e;
}
// dump warnings too
public void warning(SAXParseException err)
throws SAXParseException
{
System.out.println("** Warning"
+ ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
//===========================================================
// Utility Methods ...
//===========================================================
// Display text accumulated in the character buffer
private void echoText()
throws SAXException
{
if (textBuffer == null) return;
String s = ""+textBuffer;
emit(s);
textBuffer = null;
}
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void emit(String s)
throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
// Start a new line
private void nl()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
}