CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web Services / XML

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

楼主damiyi()2003-11-04 10:46:48 在 Java / Web Services / 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);  
                  }  
          }  
  }  
   
  问题点数:100、回复次数:2Top

1 楼damiyi()回复于 2003-11-04 16:03:11 得分 0

就没人会吗?还以为到CSDN能得到帮助呢!!  
  真的很着急呀!Top

2 楼etre(林荃)回复于 2003-11-06 11:03:35 得分 100

XML本身就是具有无限扩展的能力,就象树一样,关键看你是如何定义的,只有你能定义出来,就可以解析出来。  
  递归可以吧Top

相关问题

  • 基于xml实现无限级菜单
  • ■■■■写一个新闻发布系统,如何做到,可以无限扩展新闻类别级数??
  • 求asp+xml+xsl生成的无限级分类开合菜单
  • 关于无限级菜单
  • 关于无限级动态XML目录树,MS的deeptree.htc的一个问题。请教
  • 无限级分类的删除
  • 无限级目录树问题
  • 求一个无限级栏目程序
  • 无限级分类检索的问题
  • 哪有支持无限级的树型菜单源码?

关键词

  • 节点
  • 属性
  • istitle
  • hashtable
  • ename
  • saxparseexception
  • tempkey
  • 复杂
  • textbuffer
  • saxexception

得分解答快速导航

  • 帖主:damiyi
  • etre

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo