之前解析xml用的都是dom4j来解析,最近学习了使用xpath来解析xml,下面附上代码:
public String parseXPath(String xml,Map<String,String> namespaceMap,String resExpression ) throws Exception{
final Map<String,String> namespaces = namespaceMap;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document restDom = db.parse(is);
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
@SuppressWarnings("rawtypes")
public Iterator getPrefixes(String namespaceURI) {
// TODO Auto-generated method stub
return null;
}
public String getPrefix(String namespaceURI) {
// TODO Auto-generated method stub
return null;
}
public String getNamespaceURI(String prefix) {
String namespace = namespaces.get(prefix);
if(prefix == null || prefix.length() == 0){
return XMLConstants.DEFAULT_NS_PREFIX;
}else{
return namespace;
}
}
});
XPathExpression compile = xPath.compile(resExpression);
return compile.evaluate(restDom);
}
此处已将xml转为java字符串
String xml =
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:m56=\"http://10.29.26.34:80/PSIGW/PeopleSoftServiceListeningConnector/M562995.V1\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<m56:Create__CompIntfc__WK_CI_USERPORFILE>" +
"<m56:OPRID>aaa@wku.edu.cn</m56:OPRID><!--登陆账号 Optional:--> " +
"<m56:OPRCLASS>HCPPALL</m56:OPRCLASS><!-- 必填,固定值 Optional:-->" +
"<m56:ROWSECCLASS>HCDPALL</m56:ROWSECCLASS><!-- 必填,固定值 Optional:--> " +
"<m56:OPERPSWD>1234</m56:OPERPSWD><!-- 密码 Optional:--> " +
"<m56:SYMBOLICID>SYSADM1</m56:SYMBOLICID><!-- 必填,固定值 Optional:--> " +
"<m56:LANGUAGE_CD>ENG</m56:LANGUAGE_CD><!-- 语言 Optional:--> " +
"<m56:CURRENCY_CD/> <!--Optional:--> " +
"<m56:ACCTLOCK>0</m56:ACCTLOCK><!-- PS中的是否锁定账户,不是必填 Optional:--> " +
"<m56:PRCSPRFLCLS>HCSPPRFL</m56:PRCSPRFLCLS><!-- 必填,固定值 Optional:--> " +
"<m56:DEFAULTNAVHP>HCSPNAVHP</m56:DEFAULTNAVHP><!-- 必填,固定值 Optional:--> " +
"<m56:EXPENT>0</m56:EXPENT><!-- PS中账号是否过期,默认值是0,不是必填 Optional:--> " +
"<m56:USERIDALIAS/> <!--Optional:--> " +
"<m56:PTALLOWSWITCHUSER/> <!--Optional:--> " +
"<m56:OPERPSWDCONF>1234</m56:OPERPSWDCONF><!-- 密码确认 Optional:--> " +
"<m56:ROLEUSER_REASSIGN/> <!--Optional:--> " +
"<m56:ROLEUSER_ASSIGN_SW/> <!--Optional:--> " +
"<m56:NO_SYMBID_WARN/> <!--Optional:--> " +
"<m56:PSWDEXPIRED/> <!--Optional:--> " +
"<m56:MPDEFAULMP/> <!--Optional:--> " +
"<m56:SERVERNAME/> <!--Optional:--> " +
"<m56:WORKLIST_USER_SW/> <!--Optional:--> " +
"<m56:EMAIL_USER_SW/> <!--Optional:--> " +
"<m56:ROLEUSER_ALT/> <!--Optional:--> " +
"<m56:ROLEUSER_SUPR/> <!--Optional:--> " +
"<m56:EFFDT_FROM/> <!--Optional:--> " +
"<m56:EFFDT_TO/> <!--Zero or more repetitions:-->" +
"<m56:PSOPRALIAS><m56:OPRALIASTYPE>EMP</m56:OPRALIASTYPE><!-- 添加的人员类型Zero or more repetitions:-->" +
"<m56:PSOPRALIASFIELD><!--Optional:-->" +
"<m56:ATTRVALUE>0870913</m56:ATTRVALUE><!-- 添加的人员的编码 -->" +
"</m56:PSOPRALIASFIELD>" +
"</m56:PSOPRALIAS> <!--Zero or more repetitions:--> " +
"<m56:PSOPTIONS><!--Optional:-->" +
"<m56:FROM_QUERY_RULE/><!--Optional:--><m56:FROM_DIR_RULE/>" +
"</m56:PSOPTIONS> <!--Zero or more repetitions:--> " +
"<m56:PSROLEUSER_VW>" +
"<m56:ROLENAME_1>PeopleSoft User</m56:ROLENAME_1><!-- 角色 -->" +
"</m56:PSROLEUSER_VW> <!--Zero or more repetitions:--> " +
"<m56:PSUSEROTHER_VW/>" +
"</m56:Create__CompIntfc__WK_CI_USERPORFILE>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
String resExpression = "/soapenv:Envelope/soapenv:Body/m56:Create__CompIntfc__WK_CI_USERPORFILE/m56:PSROLEUSER_VW/m56:ROLENAME_1";
//String resExpression = "//m12:OPRID/text()";
XPathUtil httpU = new XPathUtil();
Map<String,String> namespaces = new HashMap<String,String>();
namespaces.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.put("m56", "http://10.29.26.34:80/PSIGW/PeopleSoftServiceListeningConnector/M562995.V1");
System.out.println("Result is "+ httpU.parseXPath(xml,namespaces, resExpression));
但有的时候因为xml文本的不规范,会导致xpath按路径取不到所需要的值,这里我用了通配符来解决这个问题:
XML:
String xml =
"<OutputParameters xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://xmlns.oracle.com/apps/ap/rest/MdmDataSynch/synch_company/\">"+
"<X_ORIG_SYSTEM_TASK_ID>41a2a3bb-bdf0-45a1-93f9-e2dc39915c6b</X_ORIG_SYSTEM_TASK_ID>"+
"<X_EBS_API_TASK_ID>4b77bcf0-d843-50b7-e054-0010e0765cec</X_EBS_API_TASK_ID>"+
"<X_RETURN_STATUS>S</X_RETURN_STATUS>"+
"<X_ERROR_MSG/>"+
"</OutputParameters>";
JAVA代码:
String resExpression = "/*[name()='OutputParameters']/*[name()='X_RETURN_STATUS']/text()";
System.out.println(resExpression);
XPathUtil httpU = new XPathUtil();
Map<String,String> namespaces = new HashMap<String,String>();
System.out.println("Result11 is "+ httpU.parseXPath(xml,namespaces, resExpression));