Java XPath:提取多个子节点值
我试图使用XPath从Java XPath:提取多个子节点值,java,xpath,nodes,Java,Xpath,Nodes,我试图使用XPath从多个节点提取值,但得到的只是第一个值 SOAP看起来像: <addressBlock xmlns:i="http://www.w3.org/2001/XMLSchema-instance" s:relay="1"> <from>mailto:user1@test.local</from> <to>mailto:user2@test.local</to> <to>mailto:us
多个节点提取值,但得到的只是第一个值
SOAP看起来像:
<addressBlock xmlns:i="http://www.w3.org/2001/XMLSchema-instance" s:relay="1">
<from>mailto:user1@test.local</from>
<to>mailto:user2@test.local</to>
<to>mailto:user3@test2.local</to>
</addressBlock>
private String extractFieldFromXml(Document doc, XPath xPath, String expression)
{
try
{
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
return node == null ? null : node.getTextContent();
} catch (XPathExpressionException e)
{
log.info(e.getMessage());
return null;
}
}
然后我试着这么做:
String to = extractFieldFromXml(doc, xpath, msgExpression);
for (Iterator<String> to = to.iterator(); to.hasNext();) {
String value = to.next();
System.out.println(value);
}
String to=extractFieldFromXml(doc、xpath、msgExpression);
for(迭代器to=to.Iterator();to.hasNext();){
字符串值=to.next();
系统输出打印项次(值);
}
获取所有值的XPath是
/addressBlock/to
此表达式返回到/text()
字符串的串联。
使用Java为每个
在该结果的所有项上运行一个。获取所有到
值的XPath是
/addressBlock/to
此表达式返回到/text()
字符串的串联。
使用Java为每个
在该结果的所有项上运行一个。如果有人要寻找解决方案,工作代码如下:
private String extractFieldFromXml(Document doc, XPath xPath, String expression)
{
if (expression.equals(RECIPIENT_EXPRESSION)) {
try
{
NodeList nodes = (NodeList) xPath.evaluate(expression,doc, XPathConstants.NODESET);
ArrayList<String> recipientsList = new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++){
Node node = nodes.item(i);
if (node != null) {
String recipient = node.getTextContent();
recipientsList.add(recipient);
}
}
String recipients = StringUtils.join(recipientsList,",");
return recipients == null ? null: recipients;
} catch (XPathExpressionException e)
{
log.info(e.getMessage());
return null;
}
}
else {
try
{
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
return node == null ? null : node.getTextContent();
} catch (XPathExpressionException e)
{
log.info(e.getMessage());
return null;
}
}
}
private String extractFieldFromXml(文档文档、XPath、字符串表达式)
{
if(表达式.equals(接受者_表达式)){
尝试
{
NodeList节点=(NodeList)xPath.evaluate(表达式、文档、XPathConstants.NODESET);
ArrayList recipientsList=新的ArrayList();
对于(int i=0;i
如果有人要寻找解决方案,工作代码如下:
private String extractFieldFromXml(Document doc, XPath xPath, String expression)
{
if (expression.equals(RECIPIENT_EXPRESSION)) {
try
{
NodeList nodes = (NodeList) xPath.evaluate(expression,doc, XPathConstants.NODESET);
ArrayList<String> recipientsList = new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++){
Node node = nodes.item(i);
if (node != null) {
String recipient = node.getTextContent();
recipientsList.add(recipient);
}
}
String recipients = StringUtils.join(recipientsList,",");
return recipients == null ? null: recipients;
} catch (XPathExpressionException e)
{
log.info(e.getMessage());
return null;
}
}
else {
try
{
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
return node == null ? null : node.getTextContent();
} catch (XPathExpressionException e)
{
log.info(e.getMessage());
return null;
}
}
}
private String extractFieldFromXml(文档文档、XPath、字符串表达式)
{
if(表达式.equals(接受者_表达式)){
尝试
{
NodeList节点=(NodeList)xPath.evaluate(表达式、文档、XPathConstants.NODESET);
ArrayList recipientsList=新的ArrayList();
对于(int i=0;i
在这种情况下,迭代器是什么?我已经用为所选节点提取值的代码更新了我的问题。很抱歉,我现在无法测试它。但有一种方法是可行的。因此,将XPath计算为NodeList
并将其传递给迭代器-因此在Node
s而不是String
s上进行操作,并仅在最后一步获取文本内容。是的,这是有意义的。我将尝试这种方法并用结果更新。在这种情况下,迭代器是什么?我已经用为所选节点提取值的代码更新了我的问题。很抱歉,我现在无法测试它。但有一种方法是可行的。因此,将XPath计算为NodeList
并将其传递给迭代器-因此在Node
s而不是String
s上进行操作,并仅在最后一步获取文本内容。是的,这是有意义的。我将尝试这种方法,并根据结果进行更新。