如何从 XMLReader 获取属性
我有一些 HTML,我正在使用 Html.fromHtml(...)
将其转换为 Spanned
,并且我有一个正在其中使用的自定义标记:
<customtag id="1234">
因此,我实现了一个 TagHandler
来处理这个自定义标签,如下所示:
public void handleTag( boolean opening, String tag, Editable output, XMLReader xmlReader ) {
if ( tag.equalsIgnoreCase( "customtag" ) ) {
String id = xmlReader.getProperty( "id" ).toString();
}
}
在这种情况下,我收到一个 SAX 异常,因为我相信“id”字段实际上是一个属性,而不是一个属性。但是,XMLReader
没有 getAttribute()
方法。所以我的问题是,如何使用此 XMLReader
获取“id”字段的值?谢谢。
I have some HTML that I'm converting to a Spanned
using Html.fromHtml(...)
, and I have a custom tag that I'm using in it:
<customtag id="1234">
So I've implemented a TagHandler
to handle this custom tag, like so:
public void handleTag( boolean opening, String tag, Editable output, XMLReader xmlReader ) {
if ( tag.equalsIgnoreCase( "customtag" ) ) {
String id = xmlReader.getProperty( "id" ).toString();
}
}
In this case I get a SAX exception, as I believe the "id" field is actually an attribute, not a property. However, there isn't a getAttribute()
method for XMLReader
. So my question is, how do I get the value of the "id" field using this XMLReader
? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以使用
TagHandler
提供的XmlReader
无需反射即可访问标签属性值,但该方法比反射更不简单。诀窍是将XmlReader
使用的ContentHandler
替换为自定义对象。替换ContentHandler
只能在调用handleTag()
中完成。这会带来获取第一个标签的属性值的问题,可以通过在 html 开头添加自定义标签来解决这个问题。使用此类读取属性很容易:
这种方法的优点是它允许禁用某些标记的处理,同时对其他标记使用默认处理,例如,您可以确保不创建
ImageSpan
对象:It is possible to use
XmlReader
provided byTagHandler
and get access to tag attribute values without reflection, but that method is even less straightforward than reflection. The trick is to replaceContentHandler
used byXmlReader
with custom object. ReplacingContentHandler
can only be done in the call tohandleTag()
. That presents a problem getting attribute values for the first tag, which can be solved by adding a custom tag at the start of html.With this class reading attributes is easy:
This approach has the advantage that it allows to disable processing of some tags while using default processing for others, e.g. you can make sure that
ImageSpan
objects are not created:下面是我通过反射获取
xmlReader
私有属性的代码:请注意,您可以将值放入映射中,但对于我的使用来说,开销太大了。
Here is my code to get the private attributes of the
xmlReader
by reflection:Note you could put the values into a map but for my usage that's too much overhead.
根据 rekire 的回答,我制作了这个稍微更强大的解决方案,可以处理任何标签。
在handleTag内部执行以下操作:
然后属性将可以这样访问:
attributes.get(“my attribute name”);
Based on the answer by rekire I made this slightly more robust solution that will handle any tag.
And inside handleTag do:
And then the attributes will be accessible as so:
attributes.get("my attribute name");
其他解决方案还有一种替代方案,它不允许您使用自定义标签,但具有相同的效果:
然后按如下方式阅读:
根据您想要使用自定义标签执行的操作,上述内容可能会对您有所帮助。如果您只是想阅读它们,则不需要
SpannableStringBuilder
,只需将getText
转换为Spanned
接口即可进行调查。请注意,代表
...
的Annotation
是自 API 级别 1 以来的 Android 内置功能!它又是那些隐藏的瑰宝之一。 It 的每个
标签只能有一个属性的限制,但是没有什么可以阻止您嵌套多个注释来实现多个属性:如果您使用使用
Editable
界面而不是Spannable
,您还可以修改每个注释周围的内容。例如,更改上面的代码:将导致就好像您在 XML 中具有此内容一样:
需要注意的一个警告是,当您进行影响文本长度的修改时,跨度会四处移动。确保在正确的时间读取跨度开始/结束索引,最好将它们内联到方法调用中。
Editable
还允许您进行简单的搜索和替换:There's an alternative to the other solutions, that doesn't allow you to use custom tags, but has the same effect:
Then read it like this:
Depending on what you wanted to do with your custom tags, the above may help you. If you just want to read them, you don't need a
SpannableStringBuilder
, just castgetText
toSpanned
interface to investigate.Note that
Annotation
representing<annotation foo="bar">...</annotation>
is an Android built-in since API level 1! It's one of those hidden gems again. The It has the limitation of one attribute per<annotation>
tag, but nothing prevents you from nesting multiple annotations to achieve multiple attributes:If you use the
Editable
interface instead ofSpannable
you can also modify the content around each annotation. For example changing the above code:will result as if you had this in the XML:
One caveat to look out for is when you make modifications that affect the length of the text, the spans move around. Make sure you read the span start/end indices at the correct times, best if you inline them to the method calls.
Editable
also allows you to do simple search and replace substitution:如果您只需要一个属性,那么 vorrtex 的建议实际上非常可靠。为了给你一个例子来说明处理是多么简单,请看这里:
在你的自定义 TagHandler 中,你不使用 equals 而是使用 indexOf
然后你可以将 onClick 视图参数中的后缀值作为标记传递以保留它很通用。
If all you need is just one attribute the suggestion by vorrtex is actually pretty solid. To give you an example of just how simple it would be to handle have a look here:
And in your custom TagHandler you don't use equals but indexOf
And you can then pass the postfix value in your onClick view parameter as a tag to keep it generic.