org.dom4j.InvalidXPathException: Invalid XPath expression:

org.dom4j.InvalidXPathException: Invalid XPath expression: '//staff_eval_stat_on_user'. Caused by:

org.dom4j.invalidxpathexception: invalid xpath expression: '//staff_eval_stat_on_user'. caused by: org/jaxen/dom4j/dom4jxpath

把dom4j所需的 jaxen-1.1-beta-6.jar

jaxme-api-0.3.jar

jsr173_1.0_api.jar

pull-parser-2.1.10.jar

relaxngDatatype-20030807.jar

xpp3-1.1.3.3.jar

xsdlib-20030807.jar 导入即可

最近两天在看dom4j,jdom,dom,sax。后面对比了一下发现还是dom4j比较方便。

在写淘宝的api 接口返回的结果有一些问题,就基于dom写了一个demo.如下;

	
xml结构如下:


    cntaobao高梵旗舰店:李玟
    
    2
    
        2013-03-27 09:02:38
        
        0
        
        2013-03-27 18:18:26
        
        1
        
    cntaobao高梵旗舰店:张学友
    
    0
    

dom解析如下
		String payload = com.taobao.api.internal.util.StringUtils.stripNonValidXMLCharacters(body);
				Document doc = Util.string2Doc(payload);
				NodeList onUserNodeList=doc.getElementsByTagName("staff_eval_stat_on_user");
				if(onUserNodeList!=null && onUserNodeList.getLength()>0){
					for (int i = 0; i < onUserNodeList.getLength(); i++) {
						StaffEvalStatOnUser  onUser=new StaffEvalStatOnUser();
						Node countNode=doc.getElementsByTagName("count").item(i);
						Long count=null;
						if (countNode!=null) {
							count=CommonUtil.getStrToLong(StringUtils.trim(countNode.getTextContent()));
						}
						Node userIdNode=doc.getElementsByTagName("user_id").item(i);
						String userId=null;
						if(userIdNode!=null){
							userId=StringUtils.trim(userIdNode.getTextContent());
						}
						Node  parentNote=doc.getElementsByTagName("loginlogs").item(i);
						if (parentNote!=null) {
							NodeList parentNoteList=parentNote.getChildNodes();
							if (parentNoteList!=null && parentNoteList.getLength()>0) {
								List loginLogList=new ArrayList();
								for (int j = 0; j < parentNoteList.getLength(); j++) {
									Node node=parentNoteList.item(j);
									String nodeName=node.getNodeName();
									if (TbConstants.NODE_NAME_LOGINLOG.equals(nodeName)) {
										LoginLog loginLog=new LoginLog();
										NodeList childNodeList=node.getChildNodes();
										for (int k = 0; k < childNodeList.getLength(); k++) {
											Node childNode=childNodeList.item(k);
											String childNodeName=childNode.getNodeName();
											if (TbConstants.NODE_NAME_TIME.equals(childNodeName)) {
												loginLog.setTime(StringUtils.trim(childNode.getTextContent()));
											}else if (TbConstants.NODE_NAME_TYPE.equals(childNodeName)) {
												loginLog.setType(StringUtils.trim(childNode.getTextContent()));
											}
										}
										loginLogList.add(loginLog);
									}
								}
								onUser.setLoginlogs(loginLogList);
							}
						}
						onUser.setCount(count);
						onUser.setUserId(userId);
						onUserList.add(onUser);
					}
				}
			
dom4j解析如下:
public static List modifyDocument(String body){
		List onUserList=null;
		if (StringUtils.isNotEmpty(body)) {
			onUserList=new  ArrayList();
			try {
				String payload = com.taobao.api.internal.util.StringUtils.stripNonValidXMLCharacters(body);
				Document document = DocumentHelper.parseText(payload);
				List list=document.selectNodes("//staff_eval_stat_on_user" );
				 if (CollectionUtils.isNotEmpty(list)) {
					for (Element element : list) {
						StaffEvalStatOnUser onUser=new StaffEvalStatOnUser();
						Node userIdChildNode =element.selectSingleNode("user_id");
						if (userIdChildNode!=null) {
							onUser.setUserId(StringUtils.trim(userIdChildNode.getText()));
						}
						Node  countChildNode=element.selectSingleNode("count");
						if (countChildNode!=null) {
							onUser.setCount(CommonUtil.getStrToLong(StringUtils.trim(countChildNode.getText())));
						}
						List loginLogElementList=element.selectNodes("//loginlog");
						if (CollectionUtils.isNotEmpty(loginLogElementList)) {
							List loginLogList=new ArrayList();
							for (Element loginLogElement : loginLogElementList) {
								LoginLog loginLog=new LoginLog();
								Node timeNode=loginLogElement.selectSingleNode("time");
								if (timeNode!=null) {
									loginLog.setTime(StringUtils.trim(timeNode.getText()));
								}
								Node typeNode=loginLogElement.selectSingleNode("type");
								if (typeNode!=null) {
									loginLog.setType(StringUtils.trim(typeNode.getText()));
								}
								loginLogList.add(loginLog);
							}
							onUser.setLoginlogs(loginLogList);
						}
						onUserList.add(onUser);
					}
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onUserList;
	}

你可能感兴趣的:(xml/dom4j/dom)