java Get the list or name of all attributes in a XML element -
i have xml element
<base baseatt1="aaa" baseatt2="tt"> <innerelement att1="one" att2="two" att3="bazinga"/> </base>
and list of attributes. both base element , inner element.
i dont know name of innerelement can have many different names.
nodelist baseelmntlst_gold = goldanalysis.getelementsbytagname("base"); element baseelmnt_gold = (element) baseelmntlst_gold.item(0);
the goal kind of dictionary output,
for example xml above output dictionary valuse.
baseatt1 = "aaa" baseatt2 = "tt" att1 = "one" att2 = "two" att3 = "bazinga"
i using jre 1.5
here plain dom based solution (however there nothing wrong combine xpath dom in java):
nodelist baseelmntlst_gold = goldanalysis.getelementsbytagname("base"); element baseelmnt_gold = (element) baseelmntlst_gold.item(0); namednodemap baseelmnt_gold_attr = baseelmnt_gold.getattributes(); (int = 0; < 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; (int = 0; < innerelmntlst_gold.getlength(); ++i) { if (innerelmntlst_gold.item(i) instanceof element) { innerelement_gold = (element) innerelmntlst_gold.item(i); break; // first child } } namednodemap innerelmnt_gold_attr = innerelement_gold.getattributes(); (int = 0; < 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"
Comments
Post a Comment