<a href="http://www.javayou.com?cat_id=1">Java自由人</a> |
1. test.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="Java自由人登录">
<p>
用户名:<input type="text" name="username" size="15"/>
密码:<input type="text" name="password" size="15"/>
<br/>
<anchor>现在登录
<go href="/wap/user.do" method="get">
<postfield name="name" value="$(username)"/>
<postfield name="password" value="$(password)"/>
<postfield name="eventSubmit_Login" value="WML"/>
</go>
</anchor><br/>
<a href="/wap/index.vm">返回首页</a>
</p>
</card>
</wml>
2. HyperLinkTrace.java
package demo.htmlparser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.PrototypicalNodeFactory;
import org.htmlparser.tags.CompositeTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
/**
* 用来遍历WML文档中的所有超链接
* @author Winter Lau
*/
public class HyperLinkTrace {
public static void main(String[] args) throws Exception {
//初始化HTMLParser
Parser parser = new Parser();
parser.setEncoding("8859_1");
parser.setInputHTML(getWmlContent());
//注册新的结点解析器
PrototypicalNodeFactory factory = new PrototypicalNodeFactory ();
factory.registerTag(new WmlGoTag ());
parser.setNodeFactory(factory);
//遍历符合条件的所有节点
NodeList nlist = parser.extractAllNodesThatMatch(lnkFilter);
for(int i=0;i<nlist.size();i++){
CompositeTag node = (CompositeTag)nlist.elementAt(i);
if(node instanceof LinkTag){
LinkTag link = (LinkTag)node;
System.out.println("LINK: \t" + link.getLink());
}
else if(node instanceof WmlGoTag){
WmlGoTag go = (WmlGoTag)node;
System.out.println("GO: \t" + go.getLink());
}
}
}
/**
* 获取测试的WML脚本内容
* @return
* @throws Exception
*/
static String getWmlContent() throws Exception{
URL url = ParserTester.class.getResource("/demo/htmlparser/test.wml");
File f = new File(url.toURI());
BufferedReader in = new BufferedReader(new FileReader(f));
StringBuffer wml = new StringBuffer();
do{
String line = in.readLine();
if(line==null)
break;
if(wml.length()>0)
wml.append("\r\n");
wml.append(line);
}while(true);
return wml.toString();
}
/**
* 解析出所有的链接,包括行为<a>与<go>
*/
static NodeFilter lnkFilter = new NodeFilter() {
public boolean accept(Node node) {
if(node instanceof WmlGoTag)
return true;
if(node instanceof LinkTag)
return true;
return false;
}
};
/**
* WML文档的GO标签解析器
* @author Winter Lau
*/
static class WmlGoTag extends CompositeTag {
private static final String[] mIds = new String[] {"GO"};
private static final String[] mEndTagEnders = new String[] {"ANCHOR"};
public String[] getIds (){
return (mIds);
}
public String[] getEnders (){
return (mIds);
}
public String[] getEndTagEnders (){
return (mEndTagEnders);
}
public String getLink(){
return super.getAttribute("href");
}
public String getMethod(){
return super.getAttribute("method");
}
}
}
GO: /wap/user.do |