- <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;
- }