我有一个xml元素
<base baseAtt1="aaa" baseAtt2="tt">
<innerElement att1="one" att2="two" att3="bazinga"/>
</base>
我希望得到属性列表。 对于两者基本元素和内部元素。
我不知道 innerElement的名称 它可以有许多不同的名称。
NodeList baseElmntLst_gold = goldAnalysis.getElementsByTagName("base");
Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
目标是获得一种字典作为输出,
例如对于输出上面的xml,将是一个带有这些值的字典。
baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"
我正在使用jre 1.5
答案 0 :(得分:9)
这是基于纯DOM的解决方案(但是在Java中将XPath与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() + "\"");
}
结果:
baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"
答案 1 :(得分:2)
您可以使用此XPath检索1st element
节点的所有属性:
base/element[1]/@*
要获取XML中所有节点的所有属性,可以使用以下表达式:
//@*
答案 2 :(得分:2)
如果您使用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();
}
答案 3 :(得分:0)
使用此方法..
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);
}
}
通过在main方法中使用以下调用来调用此方法
NodeList entries = doc.getElementsByTagName("NameOfTheNode");
int num = entries.getLength();
for (int i=0; i<num; i++) {
Element node = (Element) entries.item(i);
listAllAttributes(node);
}