2 回答
TA贡献1818条经验 获得超11个赞
我有两种方法可以解决您的问题
你可以使用任何你想要的方式
方法一:
Java正确解析xmls
听到是一个代码示例,您可以在其中从您的 xml 中获取订单号
String filepath = "/home/sample.xml"; //this file contains unecaped xml
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(filepath);
Node Order = xmlDoc.getElementsByTagName("Order").item(0);
NamedNodeMap attr = Order.getAttributes();
Node no = attr.getNamedItem("No");
System.out.println("Order no : " + no.getNodeValue());
此代码的输出
订单号:0NGAOR18100000000603
这样你就可以对 xmls 进行转义
String unescapedStr=StringEscapeUtils.unescapeXml(StringEscapeUtils.unescapeXml(str)));//you can store it in a file if you want
我已经完成了 unescapeXml twise
方法二:
如果您不想格式化 xml 并希望从中获得订单号 使用 Regex 有一种简单的方法可以实现它
private static final Pattern TAG_REGEX = Pattern.compile("Order No(.+?) ");
private static List<String> getOrderNo(final String str) {
final List<String> tagValues = new ArrayList<String>();
final Matcher matcher = TAG_REGEX.matcher(str);
while (matcher.find()) {
tagValues.add(matcher.group(1));
}
return tagValues;
}
以这种方式调用方法
System.out.println(Arrays.toString(getOrderNo(str).toArray()));
输出我们会
[="0NGAOR18100000000603"]
由于您使用了正则表达式,您可以使用字符串替换删除不需要的括号,或者您可以使用仍然有效的正则表达式
希望这对你有帮助!!!!
TA贡献1848条经验 获得超2个赞
首先尝试从 SOAP 消息中提取消息正文。然后运行您的代码以对其进行转义并处理其内容。
// create message factory
MessageFactory mf = MessageFactory.newInstance();
// headers for a SOAP message
MimeHeaders header = new MimeHeaders();
header.addHeader("Content-Type", "text/xml");
InputStream stream = new ByteArrayInputStream(responseXml .getBytes(StandardCharsets.UTF_8));
// create the SOAPMessage
SOAPMessage soapMessage = mf.createMessage(header,stream);
// get the body
SOAPBody soapBody = soapMessage.getSOAPBody();
添加回答
举报