关于类中两个 SAXParser 的问题
我有两个 sax 解析器,如下:
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
SAXParser parser1 = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("G:\\Documents\\posts.xml"), this);
parser1.parse(new File("G:\\Documents\\comments.xml"), this);
} catch (Exception ex) {
ex.printStackTrace();
}
问题是,当调用处理程序 startElement 时,如何检查它是来自 parser1 还是 parser2?我假设使用了 uri 和 localName,但是如何使用呢?
I have two sax parser as follows:
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
SAXParser parser1 = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new File("G:\\Documents\\posts.xml"), this);
parser1.parse(new File("G:\\Documents\\comments.xml"), this);
} catch (Exception ex) {
ex.printStackTrace();
}
the question is that when the handler startElement is called, how do I check whether this is from parser1 or parser2? I am assuming that uri and localName is used, but how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用两个处理程序。作为一个简单的示例(您必须填写详细信息):
看起来没有办法找到生成事件的解析器。
I'd suggest using two handlers. As a trivial example (you'll have to fill in the details):
It doesn't look like there's a way to find the parser that generated the event.
如果要区分基于同一类的两个内容处理程序,则应构造内容处理程序的两个实例,并传递构造函数参数来区分这两个实例(或 setter 方法)。将该信息保存在内容处理程序中作为字段,并引用它们来区分。
If you want to differentiate two content handlers based on the same class, you should construct two instances of the content handler, and pass a constructor parameter to differentiate the two, (or setter method). Save that information in the content handler as fields, and refer to them to differentiate.