读取html带自定义标签问题,TagHandler

昵__称 2014-08-01 08:16:29
我接受一个html"<g color='blue'>自定义</g>"
用Html.fromHtml()显示。
可是读取不到属性
public class MainActivity extends Activity {
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
String s = "<g color='blue'>自定义</g>";
tv1.setText(Html.fromHtml(s, null, new GameTagHandler()));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
}

public class GameTagHandler implements TagHandler {
private int startIndex = 0;
private int stopIndex = 0;

@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (tag.toLowerCase().equals("g")) {
if (opening) {
startGame(tag, output, xmlReader);
} else {
endGame(tag, output, xmlReader);
}
}
}

public void startGame(String tag, Editable output, XMLReader xmlReader) {
String cc;
try {
cc = (String) xmlReader.getProperty("color");
} catch (SAXNotRecognizedException e) {
e.printStackTrace();
} catch (SAXNotSupportedException e) {
e.printStackTrace();
}
startIndex = output.length();
}

public void endGame(String tag, Editable output, XMLReader xmlReader) {
stopIndex = output.length();
output.setSpan(new GameSpan(), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String cc;
try {
cc = (String) xmlReader.getProperty("color");
} catch (SAXNotRecognizedException e) {
e.printStackTrace();
} catch (SAXNotSupportedException e) {
e.printStackTrace();
}
}

private class GameSpan extends ClickableSpan implements OnClickListener {

@Override
public void onClick(View v) {
Log.e("asdf", "sdsdfs");
}
}
}
}

求解
...全文
422 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵__称 2014-08-02
  • 打赏
  • 举报
回复
引用 3 楼 sagittarius1988 的回复:
是要这种效果?
看起来是这个效果,我想拿到color里面的值,这个得不到。
sagittarius1988 2014-08-02
  • 打赏
  • 举报
回复


是要这种效果?
卖水果的net 2014-08-02
  • 打赏
  • 举报
回复
不懂帮顶~~~~~~
昵__称 2014-08-02
  • 打赏
  • 举报
回复
引用 7 楼 sagittarius1988 的回复:
String s = "<g color='blue'>自定义</g>";
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
	        SAXParser parser = factory.newSAXParser();
	        XMLReader reader = parser.getXMLReader();
	        MyContentHandler sss = new MyContentHandler();
	        reader.setContentHandler(sss);
	        reader.parse(new InputSource(new StringReader(s)));
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class MyContentHandler implements ContentHandler {
    private StringBuffer buf;
    public void setDocumentLocator( Locator locator ) {
    }
    public void startDocument() throws SAXException {
        buf=new StringBuffer();
        System.out.println("*******开始解析文档*******");
    }
    public void endDocument() throws SAXException {
        System.out.println("*******解析文档结束*******");
    }
    public void processingInstruction( String target, String instruction )
        throws SAXException {
    }
    public void startPrefixMapping( String prefix, String uri ) {
          System.out.println("\n前缀映射: " + prefix +" 开始!"+ "  它的URI是:" + uri);
    }
    public void endPrefixMapping( String prefix ) {
          System.out.println("\n前缀映射: "+prefix+" 结束!");
    }
    public void startElement( String namespaceURI, String localName,
                                  String fullName, Attributes attributes )
                          throws SAXException {
        System.out.println("\n 元素: " + "["+fullName+"]" +" 开始解析!");
        // 打印出属性信息
        for ( int i = 0; i < attributes.getLength(); i++ ) {
            System.out.println("\t属性名称:" + attributes.getLocalName(i)
                + " 属性值:" + attributes.getValue(i));
        }
    }
    public void endElement( String namespaceURI, String localName,
                                                      String fullName )
                          throws SAXException {
        //打印出非空的元素内容并将StringBuffer清空                  
      String nullStr="";
        if (!buf.toString().trim().equals(nullStr)){
           System.out.println("\t内容是: " + buf.toString().trim());
        }
        buf.setLength(0);
        //打印元素解析结束信息
        System.out.println("元素: "+"["+fullName+"]"+" 解析结束!");              
    }
    public void characters( char[] chars, int start, int length )
                                throws SAXException {
          //将元素内容累加到StringBuffer中                
          buf.append(chars,start,length);
    }
    public void ignorableWhitespace( char[] chars, int start, int length )
                                  throws SAXException {
    }
    public void skippedEntity( String name ) throws SAXException {
    }
}
这样是可以解析出来的,color的属性放在Attributes中了,但是我看源码里,如果是自定义的tag,解析时却不用这个Attributes,不知为何
没测试你这个又没用但是我找到的其他的方法http://blog.csdn.net/q445697127/article/details/38348031 其实xmlReader里面有属性,用反射得到那些值
昵__称 2014-08-02
  • 打赏
  • 举报
回复
引用 5 楼 u014497038 的回复:
感觉应该是一个很简单的一个东西,楼主一定是一个鑫鑫鑫鑫新新新手
那大神帮我这个鑫鑫鑫鑫新新新手解决下咯
sagittarius1988 2014-08-02
  • 打赏
  • 举报
回复
String s = "<g color='blue'>自定义</g>";
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
	        SAXParser parser = factory.newSAXParser();
	        XMLReader reader = parser.getXMLReader();
	        MyContentHandler sss = new MyContentHandler();
	        reader.setContentHandler(sss);
	        reader.parse(new InputSource(new StringReader(s)));
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class MyContentHandler implements ContentHandler {
    private StringBuffer buf;
    public void setDocumentLocator( Locator locator ) {
    }
    public void startDocument() throws SAXException {
        buf=new StringBuffer();
        System.out.println("*******开始解析文档*******");
    }
    public void endDocument() throws SAXException {
        System.out.println("*******解析文档结束*******");
    }
    public void processingInstruction( String target, String instruction )
        throws SAXException {
    }
    public void startPrefixMapping( String prefix, String uri ) {
          System.out.println("\n前缀映射: " + prefix +" 开始!"+ "  它的URI是:" + uri);
    }
    public void endPrefixMapping( String prefix ) {
          System.out.println("\n前缀映射: "+prefix+" 结束!");
    }
    public void startElement( String namespaceURI, String localName,
                                  String fullName, Attributes attributes )
                          throws SAXException {
        System.out.println("\n 元素: " + "["+fullName+"]" +" 开始解析!");
        // 打印出属性信息
        for ( int i = 0; i < attributes.getLength(); i++ ) {
            System.out.println("\t属性名称:" + attributes.getLocalName(i)
                + " 属性值:" + attributes.getValue(i));
        }
    }
    public void endElement( String namespaceURI, String localName,
                                                      String fullName )
                          throws SAXException {
        //打印出非空的元素内容并将StringBuffer清空                  
      String nullStr="";
        if (!buf.toString().trim().equals(nullStr)){
           System.out.println("\t内容是: " + buf.toString().trim());
        }
        buf.setLength(0);
        //打印元素解析结束信息
        System.out.println("元素: "+"["+fullName+"]"+" 解析结束!");              
    }
    public void characters( char[] chars, int start, int length )
                                throws SAXException {
          //将元素内容累加到StringBuffer中                
          buf.append(chars,start,length);
    }
    public void ignorableWhitespace( char[] chars, int start, int length )
                                  throws SAXException {
    }
    public void skippedEntity( String name ) throws SAXException {
    }
}
这样是可以解析出来的,color的属性放在Attributes中了,但是我看源码里,如果是自定义的tag,解析时却不用这个Attributes,不知为何
sagittarius1988 2014-08-02
  • 打赏
  • 举报
回复
引用 4 楼 u014387289 的回复:
[quote=引用 3 楼 sagittarius1988 的回复:] 是要这种效果?
看起来是这个效果,我想拿到color里面的值,这个得不到。[/quote] 研究了会,感觉有点奇怪啊。看看源码先
多功能一体机 2014-08-02
  • 打赏
  • 举报
回复
感觉应该是一个很简单的一个东西,楼主一定是一个鑫鑫鑫鑫新新新手
猴头 2014-08-01
  • 打赏
  • 举报
回复
不懂帮顶~~~~~~
虽然只是小版本的升级,但对于希望在JSF应用中使用HTML5技术的开发人员而言,JSF 2.2来的更新很重要,尤其是pass through能力,它允许在JSF组件不知情的情况下传递HTML属性。 HTML5中增加了很多新特性,其中有些是在已有的元素上增加了对新属性的支持。例如,input元素的type属性支持text、search、email、url、tel、range、number和date等属性值。另外,它还有一系列的自定义数据属性,用来在HTML元素上关联少量数据。这些数据不会显示,但可以用JavaScript读取。 对于像JSF这样基于组件的库,上述情况来一个问题:为了识别新属性,所有已有的组件必须更新。对于需要显式支持这些属性的组件而言,的确如此。但在很多情况下,组件只需要在最终生成的标记中包含这些属性即可。JSF 2.2的pass-through属性就是这样实现的。 在Facelet页中,pass-through属性可以通过以下三种方法设置: 1. 通过组件标签的命名空间属性 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://java.sun.com/jsf/passthrough"> html> 2. 通过TagHandler f:passThroughAttribute设置单个属性 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> html> 3. 通过TagHandler f:passThroughAttributes设置多个属性 #{bean.multipleAttributes}代表一个Map对象。它的值可以是常量,也可以是表达式。 通过使用Express Language 3(Java EE 7的一部分),多个属性也可以通过EL表达式直接定义。 上述改变结果是,开发人员现在可以使用纯HTML来编写JSF视图,这是Wicket等竞争对手框架经常被提及的优势。 在服务器端,可以使用类UIComponent的新方法getPassThroughAttributes() 和getPassThroughAttributes(boolean create) 来设置属性。 UIComponent component = new SomeComponent(); Map passThrough = component.getPassThroughAttributes(); passThrough.put("placeholder", "Enter text"); 与pass through能力一起,JSF 2.2还进行了两项意义重大的增强:Faces Flow和“无状态视图(Stateless Views)”。 Faces Flow Faces Flow的灵感来自ADF “任务流(Task Flows)”和Spring“页面流(Web Flow)”。它提供了对流的直接支持。流被定义为节点间的流转,可以引导用户浏览一组页面和一些相关用例。节点可以是: “一个视图(A View)”:应用程序中的任何JSF页面 “一次方法调用(A Method Call)”:通过EL表达式从流图调用应用逻辑 “一个开关(A Switch)”:在流图中基于布尔型EL表达式进行导航决策 “一次流调用(A Flow Call)”:参数调用另一个流并接收返回值 “一次流返回(A Flow Return)”:返回至调用流 节点定义了流的入口和出口。 JSF 2.2引入了如下两个新注释: @FlowScoped是一个CDI作用域,定义了bean在特定流中的作用范围。Bean在进入该作用域时自动激活,而退出时自动失效。 @FlowDefinition是一个类级别的注释,它允许通过FlowBuilder API定义流。 最后,引入了新的EL对象#{flowScope},用于流的本地存储。该对象对应 facesContext.getApplication().getFlowHandler().getCurrentFlowScope()。 “无状态视图(Stateless Views)” 许多框架是有状态的,尤其是组件框架。但是如果状态信息不需要维护会有若干优点,最重要的是可以避免集群中节点间的状态复制,或者避免使用粘性会话保证请求返回至发起节点。与上述优点相比,提升框架性能和降低内存消耗等经常被提及的优点就不是那么确定无疑了。虽然状态可能导致Web框架的性能以及内存消耗问题,但在企业应用中这种影响实际上是微不足道的。JSF 2.x的“部分状态保存(Partial State Saving)”仅更新有变化的状态,使得应用和保存状态的过程相当高效,对性能的影响较小。同样地,状态信息占用的内存也非常小。 JSF的无状态实现方式很直观,处理TagHandler将其布尔值属性transient 传递给UIViewRoot#setTransient即可。如果页面设置为临时的,JSF StateManager就不存储它的任何数据,页面还原时,它也会被创建为无状态的。 JSF 2.2还有许多其它小的变化。Arjan Tijms在J-Development上的文章进行了更为详尽的说明。

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧