高分求助---XML无限级扩展的支持?

damiyi 2003-11-04 10:46:48
我的想法是,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);
}
}
}

...全文
23 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
etre 2003-11-06
  • 打赏
  • 举报
回复
XML本身就是具有无限扩展的能力,就象树一样,关键看你是如何定义的,只有你能定义出来,就可以解析出来。
递归可以吧
damiyi 2003-11-04
  • 打赏
  • 举报
回复
就没人会吗?还以为到CSDN能得到帮助呢!!
真的很着急呀!

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧