有人可以帮助我使用这个 JAVA SAXParser 吗? [英] Can someone help me with this JAVA SAXParser?

查看:28
本文介绍了有人可以帮助我使用这个 JAVA SAXParser 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经折腾了 3 个小时,但我无法让这个 F***** 解析器工作.抱歉骂人了.我不明白为什么我找不到一个像样的教程,它完全符合我的要求.

I've been fiddling for 3 hours and I can't get this F***** parser to work. Sorry for cursing. I don't understand why I can't find one decent tutorial that does exactly what I want.

我只想向函数发送一个字符串/XML.然后,解析它.这并不难.在python中,我可以闭着眼睛做.很棒的,令人毛骨悚然的文档就在这里:http://www.crummy.com/software/BeautifulSoup/documentation.html

I just want to send the function a String/XML. Then, parse it. it's not that hard. In python, I can do it with my eyes closed. Awesome, freaking documentation right here: http://www.crummy.com/software/BeautifulSoup/documentation.html

import BeautifulSoup
soup = BeautifulSoup(the_xml)
persons_name = soup.findAll('first_name')[0].string

为什么我找不到教我如何解析 XML 的好的、简单的文档????这是我当前的 JAVA SAX 代码,它不起作用,我什至不知道为什么.

Why can't I find a good, simple, documentation that teaches me how to parse XML????? This is my current code for JAVA SAX, and its not working, and I don't even know why.

  public static void parseit(String thexml)
    {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      try {   
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse( thexml , new DefaultHandler() );
      } catch (Throwable err) {
            err.printStackTrace ();
      }
    }

有人可以写给我使用 SAX 解析器解析 XML 的代码吗...拜托...就像 5 行代码.

Can someone just write me the code to parse the XML using SAX parser...please...It's just like 5 lines of code.

推荐答案

好的,那么您需要做的是实现您自己的处理程序(而不是使用默认处理程序).所以替换

Ok, so what you need to do is to implement your own handler (instead of using default one). So replace

saxParser.parse( thexml , new DefaultHandler() );

 saxParser.parse( thexml , new MyFreakingHandler() );

其中 MyFreakingHandler 实现接口 HandlerBase 或它可以扩展 DefaultHandler 类.然后简单地为诸如

where MyFreakingHandler implements interface HandlerBase or it can extend DefaultHandler class. Then simply provide implementation for such methods like

public void startDocument () throws SAXException
public void endElement (String name) throws SAXException

我不知道为什么你在网上找不到任何教程.我已经至少 3 年没有使用 SAXParser,为了回答您的帖子,我只是简单地向 Google 寻求帮助.

I don't know however why you could not find any tutorial on the web. I haven't been using SAXParser for at least 3 years now and in order to answer your post I just simply asked Google for help.

好的,所以把事情弄清楚.曾经有一个关于 SAX 的官方 Java 教程,不知何故我现在在网上找不到,但是仍然有一些不错的非官方教程可以非常有帮助.试试这个,例如:http://www.java-samples.com/showtutorial.php?tutorialid=152

Ok, so to clear things out. There used to be an official Java tutorial for SAX, that somehow I cannot find on the web now, however there are still number of decent non-official tutorials that can be quite helpful. Try with this on for instance: http://www.java-samples.com/showtutorial.php?tutorialid=152

这篇关于有人可以帮助我使用这个 JAVA SAXParser 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆