- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用多线程来停止运行 SAX 解析器的应用程序时遇到问题。
我有一个类从 XML 文件逐行读取,我希望它能够在按下按钮时停止:
private String filename;
private boolean paused = false;
public xmlReader(String filename) {
this.filename = filename;
}
public void run() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
// when an open row is encountered in the posts.xml file
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (!paused) {
System.out.println("Start Element :"
+ attributes.getValue("name"));
}
}
}
saxParser.parse (filename, handler);
这是读取 xml 文件的代码,但是由于我希望能够暂停和恢复 run 方法,所以我从线程调用它:
public class myThread implements Runnable {
private String file;
private TextArea Suggestions;
private volatile boolean running = true;
private volatile boolean paused = false;
databaseReader db = new databaseReader(this.file);
public void setSuggestions(TextArea Suggestions) {
this.Suggestions = Suggestions;
}
public void setFile(String dbFile) {
this.file = dbFile;
}
public void stop() {
running = false;
// you might also want to do this:
}
public void pause() {
// you may want to throw an IllegalStateException if !running
paused = true;
}
public void resume() {
synchronized (db) {
paused = false;
db.notifyAll(); // Unblocks thread
}
}
@Override
public void run() {
while (running) {
synchronized (db) {
db.run();
if (!running) {
break;
}
if (paused) {
try {
System.out.println("paused");
db.wait();
} catch (InterruptedException ex) {
break;
}
if (!running) {
break;
}
}
}
}
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
同样,我有一个按钮,按下时可以更改线程上 isRunning 的值:
if (!isRunning) {
myThread search = new myThread();
search.start();
}
但是,这些似乎都没有停止线程,以至于即使在关闭应用程序后,线程仍然继续运行。
我尝试过的其他事情- 交换实现 Runnable 而不是扩展线程类并调用中断、wait()
最佳答案
SAXParser
是一个基于事件的解析器 - 最古老且最不方便的 XML 解析器类型。正如您自己经历的那样,问题在于它完全控制了解析,而您只能使用react;你不能再行动了。
您有两个选择:
创建一个可停止的InputStream
,以便您可以通过停止其输入来停止解析器。它可能仍然有一些缓冲数据,但很快就会停止。
改用流式解析器或 pull-parser 解析器(请参阅 example )。它不控制解析。相反,您驱动解析并请求下一个 XML 元素。当您掌控一切时,停止它是理所当然的。
选项 1 或多或少是这样工作的:
public class StoppedException extends RuntimeException {
}
public class StoppableInputStream extends InputStream {
private boolean stopped;
private InputStream wrappedStream;
public StoppableInputStream(InputStream wrapped) {
wrappedStream = wrapped;
}
public boolean isStopped() {
return stopped;
}
public void setStopped(boolean stopped) {
return this.stopped = stopped;
}
public int read() {
if (stopped)
throw new StoppedException();
return wrappedStream.read();
}
public int read(byte[] b) {
if (stopped)
throw new StoppedException();
return wrappedStream.read(b);
}
public int read(byte[] b, int off, int len) {
if (stopped)
throw new StoppedException();
return wrappedStream.read(b, off, len);
}
// do the same for all other methods of InputStream
}
然后像这样初始化解析器:
FileInputStream fis = new FileInputStream("yourfilename.xml");
StoppableInputStream sis = new StoppableInputStream(fis);
saxParser.parse (filename, handler);
最后,您可以像这样停止解析器:
sis.setStopped(true);
我还没有测试过代码。它甚至可能无法编译。
我建议您选择选项 2。
关于java - 阻塞线程运行 SAX 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41729454/
我一直在尝试为 SAX 解析器设置 UI 线程和处理程序。这是我没有实现 UI 线程和处理程序的解析器: public class AndroidXMLReader extends ListActiv
我正在尝试读取一个大型 XML 文档,并且我想以 block 的形式读取它,而不是 XmlDocument 将整个文件读入内存的方式。我知道我可以使用 XmlTextReader 来做到这一点,但我想
我正在尝试将 11384 个 XML 文件解析到一个 SQLite 数据库中。其中之一: ]> 1 2 我正在使用 SAX 解析器: pub
我需要用 Perl 解析 XML 文件。文件的一部分单独存储,并作为系统实体插入。不过,这个问题很常见。但是我无法获得任何信息来解决它。 ]> &externalContent; 当使
我正在尝试从 xsd 解析 HL7 消息定义。我将模式定义分成两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义。 我正在尝试调整示例代码以从此处解析 XML https://gis
我正在使用 SAX 解析 MathML 表达式(尽管它是 MathML 的事实可能并不完全相关)。输入字符串示例为 λ 为了让 SAX 解
我正在尝试使用 Java 和 SAX 为 Android 设备解析 XML 文件。我从互联网上获取,在解析它时,我得到一个 ExpatException :字符“é”的格式不正确(无效标记)。有没有办
我正在尝试使用java中的sax读取xml文件。我只获得 endElement 的输出,但无法找出 startElement 出了什么问题。 这是我的处理程序: public class XMLHan
我正在使用 SAX 解析器来解析具有父级及其子级标记的 XML,如下所示:
我正在尝试从 RSS 提要中提取数据。 RSS 链接 - http://www.thehindu.com/sport/?service=rss ? 这是我的默认处理程序的字符方法。 public vo
我尝试执行下面的代码,但我的 SAX 解析器没有调用 startElement 方法。 下面是我的代码: package getTableStructure; import java.util.Lis
我正在满足一项要求,即我需要拆分大型 XML 并进一步处理。 这是 XML 示例,它可以变成单行。 yongjin 这是我的代码: import java.util.Arrays; import ja
我正在尝试将标签的内容放入我的 java Sax 解析器中的变量中。但是,Characters 方法仅返回 Char 数组。有没有办法将 Char 数组转换为 Int??? public void c
我有下面的代码.. System.setProperty("http.proxyHost","176.6.129.25") ; System.setProp
如何使用 SAX 显示树中最大深度的节点名称。该算法很适合我理解这个概念.. 例如,我应该如何使用 startelement、endelement、startdocument、enddocument
有没有可行的方法使用默认的处理程序类来查找对应的XML标签?例如... 1 1 我想使用 startElement() 和 endElement() 方
我想使用 SAX 解析器从 xml 文件中解析一些数据。我的xml如下: Pies & past Fruits 为了解析这些数据,我扩展了 DefaultHandler。 解析后的输出
我正在构建一个创建 XML 的流程(从各种来源并出于我事先不知道的各种目的),并且我希望将生成的 XML 直接注入(inject)到标准 XML 处理中,例如 SAX、StAX和 DOM。我已经完成了
我有一个由 MS Excel 创建的 XML 文件,其中包含如下元素: 22. Department"GS "NAES "ABCDEF"
我在使用 java sax 解析器打开 stackoverflow 帖子 XML 转储时遇到问题。它识别每个元素的结尾,但似乎跳过了 startElement 方法。我使用示例代码: try {
我是一名优秀的程序员,十分优秀!