Java Xpath - 如何获取元素的所有属性名称和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2460592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xpath - How to get all the attribute names and values of an element
提问by SWDeveloper
I am using xpath in java. I want to get all the attributes (name & Value) of an element. I found the query to get the attribute values of an element, now I want to get attribute names alone or names and values in single query.
我在 java 中使用 xpath。我想获取元素的所有属性(名称和值)。我找到了获取元素属性值的查询,现在我想单独获取属性名称或在单个查询中获取名称和值。
<Element1 ID="a123" attr1="value1" attr2="value2" attr3="value3" attr4="value4" attr5="value5" />
Here using the following query to get all the attribute values of Element1
XmlUtils.getAttributes(Path, String.format("//*/@*"));
Using this format //*/@*
I can get the values. result would be value1 value2 value3 value4 value5 a123
这里使用以下查询获取Element1
XmlUtils.getAttributes(Path, String.format("//*/@*"));
使用此格式的所有属性值//*/@*
我可以获取值。结果将是value1 value2 value3 value4 value5 a123
Now I want to know the query to get all the attribute names, or query to get all the attributes name and value.
现在我想知道获取所有属性名称的查询,或获取所有属性名称和值的查询。
采纳答案by Lachlan Roche
To select all attributes of all elements in the document named Element1: //Element1/@*
. This will return a nodesetcontaining attributenodes. You can then iterate the nodeset.
要选择命名部件1文档中的所有元素的所有属性://Element1/@*
。这将返回一个节点集包含属性节点。然后您可以迭代节点集。
If you already have a context node and wish to find results under it, the query would be .//Element1/@*
. This is usually more efficient than querying the entire document.
如果您已经有一个上下文节点并希望在其下查找结果,则查询将为.//Element1/@*
. 这通常比查询整个文档更有效。
// input is an InputSource or a DOM node
NodeList nl = (NodeList) xpath.evaluate("//Element1/@*", input, XPathConstants.NODESET);
int length = nl.getLength();
for( int i=0; i<length; i++) {
Attr attr = (Attr) nl.item(i);
String name = attr.getName();
String value = attr.getValue();
}
And it may be more efficient to find all elements of a given name using getElementsByTagName.
使用getElementsByTagName查找给定名称的所有元素可能更有效。
NodeList nl = document.getElementsByTagName("Element1");
To get the attributes of a particular element, iterate its attributesproperty.
要获取特定元素的属性,请迭代其attributes属性。
NamedNodeMap nl = element.getAttributes();
int length = nl.getLength();
for( int i=0; i<length; i++) {
Attr attr = (Attr) nl.item(i);
String name = attr.getName();
String value = attr.getValue();
}
回答by Flavio Sousa
I had to do it in Oracle Service Bus and had to do using only xPath to create a cache key and the solution that work for me was:
我必须在 Oracle Service Bus 中执行此操作,并且必须仅使用 xPath 来创建缓存键,而对我有用的解决方案是:
concat(
string-join(//*[string-length(normalize-space(string-join(text(), ''))) > 0]/concat(local-name(),
':',
normalize-space(string-join(text(), ''))), '_'),
'_',
string-join(//@*[normalize-space(.) != '']/concat(name(), ':', .), '_')
)