用dom4j解析XML如何找到一个节点的子结点

方法一:

root = DocumentHelper.parseText(datas).getRootElement();

 int num=-1; //遍历根结点(books)的所有孩子节点(book节点)

for ( Iterator iter = root.elementIterator(); iter.hasNext(); )

{

   Element element = (Element) iter.next(); num++; //遍历book结点的所有孩子节点(即title....),并进行处理

   for ( Iterator iterInner = element.elementIterator(); iterInner.hasNext(); )

      {

          Element elementInner = (Element) iterInner.next(); String a=elementInner.getName();//节点的名称,如title

           String b=elementInner.getText();//节点的内容,如title标签里的内容

     }

}

方法二

books = DocumentHelper.parseText(datas).getRootElement();

 Element book = null; Element title = null;

List list = books .elements("book");

 for (int i = 0; i < list.size(); i++)

 {

      book = (Element) list.get(i);

      List listfile = book .elements("title ");

     string a=((Element) listfile.get(0)).getTextTrim();//Dom4j Tutorials

     string b=((Element) listfile.get(1)).getTextTrim();//Lucene Studing

}

你可能感兴趣的:(xml,list,String,iterator,tutorials,books)