免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2531 | 回复: 1
打印 上一主题 下一主题

XML程序出问题了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-20 11:00 |只看该作者 |倒序浏览
程序代码如下,想用System.out.println()输出,运行结果没有任何输出,这是为什么呢?

package test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;



/**
*
* This class implements the HandlerBase helper class, which means that it
* defines all the "callback" methods that the SAX parser will invoke to notify
* the application. In this example we override the methods that we require.
*
* This example uses full package names in places to help keep the JAXP and SAX
* APIs distinct.
*/
public class SAXDemo extends org.xml.sax.helpers.DefaultHandler {
  /** The main method sets things up for parsing */
  public static void main(String[] args) throws IOException, SAXException,
      ParserConfigurationException {
    // Create a JAXP "parser factory" for creating SAX parsers
    javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();

    // Configure the parser factory for the type of parsers we require
    spf.setValidating(false); // No validation required

    // Now use the parser factory to create a SAXParser object
    // Note that SAXParser is a JAXP class, not a SAX class
    javax.xml.parsers.SAXParser sp = spf.newSAXParser();

    // Create a SAX input source for the file argument
    org.xml.sax.InputSource input = new InputSource(new FileReader(args[0]));

    // Give the InputSource an absolute URL for the file, so that
    // it can resolve relative URLs in a <!DOCTYPE> declaration, e.g.
    input.setSystemId("file://" + new File(args[0]).getAbsolutePath());

    // Create an instance of this class; it defines all the handler methods
    SAXDemo handler = new SAXDemo();

    // Finally, tell the parser to parse the input and notify the handler
    sp.parse(input, handler);

    // Instead of using the SAXParser.parse() method, which is part of the
    // JAXP API, we could also use the SAX1 API directly. Note the
    // difference between the JAXP class javax.xml.parsers.SAXParser and
    // the SAX1 class org.xml.sax.Parser
    //
    // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser
    // parser.setDocumentHandler(handler); // Set main handler
    // parser.setErrorHandler(handler); // Set error handler
    // parser.parse(input); // Parse!
  }

  StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text

  String servletName; // The name of the servlet

  String servletClass; // The class name of the servlet

  String servletId; // Value of id attribute of <servlet> tag

  // When the parser encounters plain text (not XML elements), it calls
  // this method, which accumulates them in a string buffer
  public void characters(char[] buffer, int start, int length) {
    accumulator.append(buffer, start, length);
  }

  // Every time the parser encounters the beginning of a new element, it
  // calls this method, which resets the string buffer
  public void startElement(String name, Attributes attributes) {
    accumulator.setLength(0); // Ready to accumulate new text
    // If its a servlet tag, look for id attribute
    if (name.equals("servlet"))
      servletId = attributes.getValue("id");
  }

  // When the parser encounters the end of an element, it calls this method
  public void endElement(String name) {
    if (name.equals("servlet-name")) {
      // After </servlet-name>, we know the servlet name saved up
      servletName = accumulator.toString().trim();
    } else if (name.equals("servlet-class")) {
      // After </servlet-class>, we've got the class name accumulated
      servletClass = accumulator.toString().trim();
    } else if (name.equals("servlet")) {
      // Assuming the document is valid, then when we parse </servlet>,
      // we know we've got a servlet name and class name to print out
      System.out.println("Servlet " + servletName
          + ((servletId != null) ? " (id=" + servletId + ")" : "")
          + ": " + servletClass);
    }
  }

  /** This method is called when warnings occur */
  public void warning(SAXParseException exception) {
    System.err.println("WARNING: line " + exception.getLineNumber() + ": "
        + exception.getMessage());
  }

  /** This method is called when errors occur */
  public void error(SAXParseException exception) {
    System.err.println("ERROR: line " + exception.getLineNumber() + ": "
        + exception.getMessage());
  }

  /** This method is called when non-recoverable errors occur. */
  public void fatalError(SAXParseException exception) throws SAXException {
    System.err.println("FATAL: line " + exception.getLineNumber() + ": "
        + exception.getMessage());
    throw (exception);
  }
}


XML内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web>
        <servlet id="hello_servlet_id">
                <servlet-name>hello </servlet-name>
                <servlet-class>Hello </servlet-class>
        </servlet>
</web>

论坛徽章:
0
2 [报告]
发表于 2007-04-20 14:30 |只看该作者

问题的解决方法

将org.xml.sax.helpers.DefaultHandler 改为org.xml.sax.HandlerBase ,运行出结果了;
但又出问题是: HandlerBase 类已经过期了,以后还是要改为其他的支持类。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP