[英]Get specified parameter from encoded url paramters String with java
Note that what I want is not get specified parameter in a sevlet, but to get the parameter from a String
like that: 请注意,我想要的不是在sevlet中获取指定的参数,而是从类似的
String
获取参数:
res_data=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf8%22%3F%3E%3Cdirect_trade_create_res%3E%3Crequest_token%3E201502051324ee4d4baf14d30e3510808c08ee1d%3C%2Frequest_token%3E%3C%2Fdirect_trade_create_res%3E&service=alipay.wap.trade.create.direct&sec_id=MD5&partner=2088611853232587&req_id=20121212344553&v=2.0
It's a url encoded utf-8 string, when decode this by python I can get the real data it represents: 这是一个url编码的utf-8字符串,当用python解码时,我可以得到它代表的真实数据:
res_data=<?xml version="1.0" encoding="utf-8"?><direct_trade_create_res><request_token>201502051324ee4d4baf14d30e3510808c08ee1d</request_token></direct_trade_create_res>&service=alipay.wap.trade.create.direct&sec_id=MD5&partner=2088611853232587&req_id=20121212344553&v=2.0
I want to get the parameter res_data
that I care about, more specifically, I just want the request_token
in the xml
of res_data
我想要得到的参数
res_data
我在乎,更具体,我只是想request_token
在xml
的res_data
I know I can use regex to get this work, but is there a more suitable way to use some lib like apache url lib or something else that I can get the res_data
parameter more elegantly? 我知道我可以使用regex来完成这项工作,但是有没有更合适的方法来使用apache url lib之类的lib或其他可以更优雅地获取
res_data
参数的东西? May be stealing some components from servlet mechanism? 可能会从servlet机制中窃取某些组件吗?
You can use java.net.URLDecoder
. 您可以使用
java.net.URLDecoder
。 Assuming the parameter is in a string called param
(and you have already split it away from the other parameters that were connected to it by &
): 假设参数在名为
param
的字符串中(并且您已经将其与通过&
与其连接的其他参数分开了):
String[] splitString = param.split("=");
String realData = null;
try {
String realData = java.net.URLDecoder.decode( splitString[1], "UTF-8" );
} catch ( UnsupportedEncodingException e ) {
// Nothing to do, it should not happen as you supplied a standard one
}
Once you do that, you can parse it with the XML parser of your choice and extract whatever you want. 完成此操作后,您可以使用所选的XML解析器对其进行解析,然后提取所需的任何内容。 Don't try to parse XML with a regex, though.
但是,请勿尝试使用正则表达式解析XML。
Since you say you don't want to hack it with a regex you might use a proper XML parser, although for such a small example it is probably overkill. 既然您说您不想使用正则表达式来破解它,那么您可能会使用适当的XML解析器,尽管对于这么小的示例来说,这可能是过分的了。
If you can assume that you can simply split your string on &
's, ie, there aren't any &
's in there that do not signal the boundary of two attribute-value pairs, you can first decode the string, then extract the attribute-value pairs from it and finally use a DOM parser + XPath to get to the request token: 如果你可以假设你可以简单地在分割你的字符串
&
的,即没有任何&
“在里面没有信号中的两个属性-值对的边界,你可以首先解码字符串,然后提取从中获取属性值对,最后使用DOM解析器+ XPath来获取请求令牌:
// split up URL parameters into attribute value pairs
String[] pairs = s.split("&");
// expect the first attribute/value pair to contain the data
// and decode the URL escape sequences
String resData = URLDecoder.decode(pairs[0], "utf-8");
int equalIndex = resData.indexOf("=");
if (equalIndex >= 0) {
// the value is right of the '=' sign
String xmlString = resData.substring(equalIndex + 1);
// prepare XML parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document doc = parser.parse(is);
// prepare XPath expression to extract request token
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xp = xpath.compile("//request_token/text()");
String requestToken = xp.evaluate(doc);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.