<root>
<node name="a">
<xml>1</xml>
</node>
<node name="b">
<xml>2</xml>
</node>
<node name="c">
<xml>3</xml>
</node>
....
我想獲得 node name="b" 的這個節點該怎麼辦?求各位大神幫忙解決下!!
public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new File(Dom4j.class.getClass()
.getResource("/").getFile().toString()
+ "test.xml"));
Element root = document.getRootElement();
Element node = Dom4j.parse(root , "name" , "b");
...
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 獲得X屬性結果是X值的整個標簽
*/
public static Element parse(Element node , String type , String val) {
for (Iterator iter = node.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
Attribute name = element.attribute(type);
if (name != null) {
String value = name.getValue();
if (value != null && val.equals(value))
return element;
else
parse(element , type , val);
}
}
return null;
}