[英]java Get the list or name of all attributes in a XML element
i have an xml element我有一个 xml 元件
<base baseAtt1="aaa" baseAtt2="tt">
<innerElement att1="one" att2="two" att3="bazinga"/>
</base>
and i would like to get the list of attributes.我想获取属性列表。 for both the base element and the inner element.
对于基本元素和内部元素。
i dont know the name of the innerElement it can have many different names.我不知道innerElement 的名称,它可以有许多不同的名称。
NodeList baseElmntLst_gold = goldAnalysis.getElementsByTagName("base");
Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
the goal is to get a kind of dictionary as output,目标是得到一种字典,如 output,
for example for the xml above the output will be a dictionary with those valuse.例如对于 xml 上面的 output 将是具有这些价值的字典。
baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"
i am using jre 1.5我正在使用 jre 1.5
Here is plain DOM based solution (however there is nothing wrong to combine XPath with DOM in Java):这是基于 DOM 的简单解决方案(但是将 XPath 与 Java 中的 DOM 结合起来并没有错):
NodeList baseElmntLst_gold = goldAnalysis.getElementsByTagName("base");
Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i)
{
Node attr = baseElmnt_gold_attr.item(i);
System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
}
NodeList innerElmntLst_gold = baseElmnt_gold.getChildNodes();
Element innerElement_gold = null;
for (int i = 0; i < innerElmntLst_gold.getLength(); ++i)
{
if (innerElmntLst_gold.item(i) instanceof Element)
{
innerElement_gold = (Element) innerElmntLst_gold.item(i);
break; // just get first child
}
}
NamedNodeMap innerElmnt_gold_attr = innerElement_gold.getAttributes();
for (int i = 0; i < innerElmnt_gold_attr.getLength(); ++i)
{
Node attr = innerElmnt_gold_attr.item(i);
System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
}
Result:结果:
baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"
You can use this XPath to retrieve all attributes of 1st element
node:您可以使用此 XPath 检索第一个
element
节点的所有属性:
base/element[1]/@*
To get all attributes of all nodes in your XML yo can use this expression:要获取 XML 中所有节点的所有属性,您可以使用以下表达式:
//@*
If you use XPath you will have less code, but for a dom base solution I have a suggestion here:如果您使用 XPath 您将拥有更少的代码,但对于 dom 基础解决方案,我在这里有一个建议:
public void printElementsAndAttributes() throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new File("test.xml"));
NodeList base = doc.getElementsByTagName("base");
Node basenode = base.item(0);
System.out.println(basenode.getNodeName() + getAttributesAsString(basenode.getAttributes()));
NodeList children = basenode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node item = children.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(item.getNodeName() + getAttributesAsString(item.getAttributes()));
}
}
}
private String getAttributesAsString(NamedNodeMap attributes) {
StringBuilder sb = new StringBuilder("\n");
for (int j = 0; j < attributes.getLength(); j++) {
sb.append("\t- ").append(attributes.item(j).getNodeName()).append(": ").append(attributes.item(j).getNodeValue()).append("\n");
}
return sb.toString();
}
Use this method..用这个方法。。
public static void listAllAttributes(Element element) {
System.out.println("List attributes for node: " + element.getNodeName());
// get a map containing the attributes of this node
NamedNodeMap attributes = element.getAttributes();
// get the number of nodes in this map
int numAttrs = attributes.getLength();
for (int i = 0; i < numAttrs; i++) {
Attr attr = (Attr) attributes.item(i);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
System.out.println("Found attribute: " + attrName + " with value: " + attrValue);
}
}
call this method by using following call in the main method通过在主方法中使用以下调用来调用此方法
NodeList entries = doc.getElementsByTagName("NameOfTheNode");
int num = entries.getLength();
for (int i=0; i<num; i++) {
Element node = (Element) entries.item(i);
listAllAttributes(node);
}
NodeList bList = eElement.getElementsByTagName("base");
Node node1 = bList.item(0);
if (node1.getNodeType() == node1.ELEMENT_NODE) {
Element ele = (Element) node1;
System.out.print("Attribute : ");
System.out.println(ele.getAttributes());
}
Should work.应该管用。
Very nice explanation is given at https://www.tutorialspoint.com/java_xml/java_xml_quick_guide.htm You can refer that for more clarity. https://www.tutorialspoint.com/java_xml/java_xml_quick_guide.htm给出了非常好的解释。您可以参考它以获得更清晰的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.