- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个扩展 SAX DefaultHandler 的类。为了获取标签之间的值,我这样做:
private static class MyHandler extends DefaultHandler {
private String str;
@Override
public void characters(char ch[], int start, int length) throws
SAXException {
String current = new String(ch, start, length);
str+=current;
}
}
如果结束标记位于同一行,但如果 xml 如下所示:
<string name="sentence">The fox runs\nover the hill into the pasture
</string>
它呈现为:
"The fox runs\nover the hill into the pasture
"
而不是
"the fox runs
over the hill into the pasture"
我不能使用trim(),因为字符串可能看起来像“The Fox run\nover the hill into the Pasture\n”
最佳答案
这里的关键是标签内的空白是否重要。如果它不重要,您可以删除多余的空格。这可以通过一个简单的正则表达式来完成。
str += current.replaceAll("\\s{2,}$", "")
如果行尾至少包含两个空格(无论是空格+换行符还是任何其他组合),此正则表达式将修剪该行。如果该行全是空格,它将被完全删除。
但是它将保持不变
The fox runs\nover the hill into the pasture\n
如果您想处理用户意外键入 \n\n
而不仅仅是 \n
的情况,您可以稍作更改
str += current.replaceAll("\\s{2,}$", "\n")
关于java - 如何使 SAX DefaultHandler 忽略隐式换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37200494/
这个问题在这里已经有了答案: Specs: What's the purpose of the blank identifier in variable assignment? [duplicate
我正在玩 Android 和 RSS,在 XML 解析器部分,我们有 DefaultHandler 及其方法: public void startElement(String uri, String
我刚刚读到characters(char[] ch, int start, int length)可以针对一个内部 XML 数据多次调用(例如 John Smith )。 在我的项目中,我必须解析大型
有没有办法在 SAXParser 或 XMLReader 上调用 quit() 函数? XML 文档大约每天更新一次,我只希望在元素发生变化时读取完整文档。 在我的默认处理程序中: @Override
我创建了一个扩展 SAX DefaultHandler 的类。为了获取标签之间的值,我这样做: private static class MyHandler extends DefaultHandle
我正在尝试解析和检测标签中 CDATA 的开始,例如: 我有一个扩展默认处理程序的类 class MyXmlDoc extends DefaultHandler{ 使用 startElement()
我正在开发一个 android 应用程序,我将在其中大量使用 SAX。我想知道将处理程序的“数据”或结果发送回我的 Activity 的最佳方式是什么? IMO 在我的处理程序中调用 intents
我遇到的问题是我想使用 SAX defaultHandler 类的扩展来解析 1 个 XML 文档。当只使用一个处理程序时,我可以解析出 XML 并将不同的标签分配给对象的属性(请参阅域和区域)。一旦
我正在开发一个 RSS 阅读器。运行此代码时,localName 和 uri 为空。我正在解析 RSS 提要。我正在运行以下代码。相同的代码在另一个 android 项目中运行良好。 @Overrid
我制作了一个 sax 解析器,其主类具有 main 方法,并使用 XMLReader.setContentHandler(myCustomParserClass) 设置解析器,这是一个扩展 Defau
我正在使用DefaultHandler 解析基于XML 的InputStream。它工作正常,但有一个异常(exception)。所有字符都转换为小写。例如,如果我的 xml 有一个像 VALUE 这
我有以下适用于我的主要 Activity 的代码,但我有一个扩展的 DefaultHandler 类并希望能够访问一些资源。 如何让以下内容起作用? Drawable newMarker = this
在消息处理程序中调用 DefaultHandler(Message) 和 inherited 之间到底有什么区别。例如: TScrollBox = class(TScrollingWinControl
我正在扩展 DefaultHandler 来解析 xml。我正在解析这个页面: http://maps.googleapis.com/maps/api/directions/xml?origin=st
我在尝试从基本 JAX-RS 设置中获取 JSON 输出时遇到一个停止显示的错误。我正在尝试生成此默认“服务”代码的 Netbeans 的 Web 服务向导: package service; imp
我正在使用带有 jpgraph 的数据库处理带有满意度表单的图表,我遇到了一些与处理程序错误相关的问题。 这是我的程序: getMessage()); } //var $req
我是一名优秀的程序员,十分优秀!