Skip to content

Commit

Permalink
修复index计算不正确的bug,整体版本更为0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
haomiaowang committed Jun 2, 2014
1 parent 6d564c5 commit 9396ad3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
18 changes: 16 additions & 2 deletions src/main/java/cn/wanghaomiao/example/XpathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

Expand All @@ -34,9 +36,21 @@ public static void main(String[] args) throws IOException, NoSuchFunctionExcepti
String x11 = "//div[@id='post_list']/div[self::div/div/div/span[@class='article_view']/a/num()>1000]/div/h3/allText()";
String x12 = "//div[@id='post_list']/div[2]/div/p/preceding-sibling::h3/allText()";
String x13 = "//div[@id='post_list']/div[2]/div/p/preceding-sibling::h3/allText()|//div[@id='post_list']/div[1]/div/h3/allText()";
Document doc = Jsoup.connect("http://www.cnblogs.com/").get();
String x14 = "//html/body/div/div[4]/div[6]/div[19]/div[2]/h3/a";
String x15 = "//html[1]/body[1]/div[3]/div[1]/div[1]/div[1]/ul[1]/li/div[2]/h2/a";
String x16 = "//html/body/div[3]/div/div/div/ul/li/div[2]/h2/a";
// Document doc = Jsoup.connect("http://www.cnblogs.com/").get();
// BufferedReader br = new BufferedReader(new FileReader("D:\\LAB\\tt.html"));
// StringBuilder content = new StringBuilder();
// String line = br.readLine();
// content.append(line);
// while (line!=null){
// line = br.readLine();
// content.append(line);
// }
Document doc = Jsoup.connect("http://book.douban.com/search/java").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0").get();
JXDocument jxDocument = new JXDocument(doc);
List<Object> rs = jxDocument.sel(x13);
List<Object> rs = jxDocument.sel(x16);
for (Object o:rs){
if (o instanceof Element){
int index = ((Element) o).siblingIndex();
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/cn/wanghaomiao/xpath/core/Functions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.wanghaomiao.xpath.core;

import cn.wanghaomiao.xpath.util.CommonUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

Expand Down Expand Up @@ -132,35 +133,29 @@ public String allText(Element e){
}

/**
* 判断一个元素是不是最后一个
* 判断一个元素是不是最后一个同名同胞中的
* @param e
* @return
*/
public boolean last(Element e){
return e.equals(e.lastElementSibling());
return CommonUtil.getElIndexInSameTags(e)==CommonUtil.sameTagElNums(e);
}
/**
* 判断一个元素是不是第一个
* 判断一个元素是不是同名同胞中的第一个
* @param e
* @return
*/
public boolean first(Element e){
return e.equals(e.firstElementSibling());
return CommonUtil.getElIndexInSameTags(e)==1;
}

/**
* 返回一个元素在兄弟节点中的位置
* 返回一个元素在同名兄弟节点中的位置
* @param e
* @return
*/
public int position(Element e){
int index = 1;
Element curEl = e.firstElementSibling();
while (!e.equals(curEl)){
curEl = curEl.nextElementSibling();
index+=1;
}
return index;
return CommonUtil.getElIndexInSameTags(e);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/cn/wanghaomiao/xpath/core/XpathEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.model.Node;
import cn.wanghaomiao.xpath.model.Predicate;
import cn.wanghaomiao.xpath.util.CommonUtil;
import cn.wanghaomiao.xpath.util.ScopeEm;
import cn.wanghaomiao.xpath.util.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
Expand Down Expand Up @@ -200,7 +200,7 @@ public Element filter(Element e,Node node) throws NoSuchFunctionException, NoSuc
*/
public Elements getAxisScopeEls(String axis,Element e) throws NoSuchAxisException {
try {
String functionName = StrUtil.getJMethodNameFromStr(axis);
String functionName = CommonUtil.getJMethodNameFromStr(axis);
Method axisSelector = AxisSelector.class.getMethod(functionName, Element.class);
return (Elements) axisSelector.invoke(SingletonProducer.getInstance().getAxisSelector(),e);
}catch (NoSuchMethodException e1) {
Expand Down Expand Up @@ -247,13 +247,10 @@ public Object callFilterFunc(String funcname,Element el) throws NoSuchFunctionEx
}

public int getElIndex(Element e){
int index = 1;
Element curEl = e.firstElementSibling();
while (!e.equals(curEl)){
curEl = curEl.nextElementSibling();
index+=1;
if (e!=null){
return CommonUtil.getElIndexInSameTags(e);
}
return index;
return 1;
}

}
53 changes: 53 additions & 0 deletions src/main/java/cn/wanghaomiao/xpath/util/CommonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.wanghaomiao.xpath.util;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
* @author: 汪浩淼 [ et.tw@163.com ]
* Date: 14-3-15 下午10:31
*/
public class CommonUtil {
public static String getJMethodNameFromStr(String str){
if (str.contains("-")){
String[] pies = str.split("-");
StringBuilder sb = new StringBuilder(pies[0]);
for (int i=1;i<pies.length;i++){
sb.append(pies[i].substring(0,1).toUpperCase()).append(pies[i].substring(1));
}
return sb.toString();
}
return str;
}

/**
* 获取同名元素在同胞中的index
* @param e
* @return
*/
public static int getElIndexInSameTags(Element e){
Elements chs = e.parent().children();
int index = 1;
for(int i=0;i<chs.size();i++){
Element cur = chs.get(i);
if (e.tagName().equals(cur.tagName())){
if (e.equals(cur)){
break;
}else {
index+=1;
}
}
}
return index;
}

/**
* 获取同胞中同名元素的数量
* @param e
* @return
*/
public static int sameTagElNums(Element e){
Elements els = e.parent().getElementsByTag(e.tagName());
return els.size();
}
}
19 changes: 0 additions & 19 deletions src/main/java/cn/wanghaomiao/xpath/util/StrUtil.java

This file was deleted.

0 comments on commit 9396ad3

Please sign in to comment.