各位仁兄,帮帮忙,我现在要对一个xml文件进行解析,将所有的结点信息保存到一个list中,但我写的代码好像有点问题,递归的时候没有把所有结点信息都传给list,哪位帮忙一下,上面还等着我交任务呢package com.ibm.filenet.edu;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;public class ParseXML {
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>(); 
ParseXML instance = new ParseXML();
String filePath = "C:\\Source\\xml file\\xml\\XML ppt\\xml day4\\dom_ sax\\dom\\student.xml"; 
list = instance.parse(filePath);
Iterator iterator = list.iterator();
//System.out.println("**************************");
//while(iterator.hasNext()){
//System.out.println(iterator.next().toString());
//}
}
    


public List<Object> parse(String filePath){
List<Object> list = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//File file = new File("C:\\Source\\xml file\\xml\\XML ppt\\xml day4\\dom_ sax\\sax\\candidate.xml");
Document dom = db.parse(filePath);
System.out.println("file path: " + dom.getDocumentURI());
Element root = dom.getDocumentElement(); //root node
list = Iterator(root);
System.out.println("********************");
Iterator  iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
//return list;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;

}
/**
 * 
 * 
 * 
 * 
 */
public List<Object> Iterator(Element root) {
NodeList nodelist = root.getChildNodes();
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < nodelist.getLength(); i++) {
Node node = nodelist.item(i);

//下面这两个if块中有问题 
if (node instanceof Text) {
String value = node.getNodeValue();
//list.add(value);
if (value != null && !value.trim().equals("")) {
System.out.println("content: " + value);
list.add( value);
}
}
if (node instanceof Element) {
System.out.println("node: " + node.getNodeName());
Iterator((Element) node);
list.add( node.getNodeName());
}
}
//System.out.println("Iterator method success.");
return list;
}
}

解决方案 »

  1.   

    Iterator((Element) node);
                    list.add( node.getNodeName());
    换换位置
      

  2.   

    Iterator((Element) node); 
                    list.add( node.getNodeName()); 
    换换位置
      

  3.   

    换位置不行的,content信息一个也没有拿到
      

  4.   

      <?xml version="1.0" encoding="UTF-8" ?> 
    - <students>
    - <student sid="001">
      <name>pitiya</name> 
    - <source>
      <java>99</java> 
      <oracle>79</oracle> 
      </source>
      </student>
    - <student sid="002">
      <name>dinaya</name> 
    - <source>
      <java>99</java> 
      <oracle>101</oracle> 
      </source>
      </student>
    - <student sid="003">
      <name>maya</name> 
    - <source>
      <java>120</java> 
      <oracle>100</oracle> 
      </source>
      </student>
      </students>
      

  5.   

    把你那个list弄成属性,在方法里他每次不都被重新创建了吗
      

  6.   


    嗯,抽到外面,或者传入list参数。
      

  7.   


    package com.ibm.filenet.edu;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;public class ParseXML {
        public static void main(String[] args) {
            List<Object> list = new ArrayList<Object>(); 
            ParseXML instance = new ParseXML();
            String filePath = "C:\\Source\\xml file\\xml\\XML ppt\\xml day4\\dom_ sax\\dom\\student.xml"; 
            list = instance.parse(filePath,list);
            Iterator iterator = list.iterator();
            //System.out.println("**************************");
            //while(iterator.hasNext()){
                //System.out.println(iterator.next().toString());
            //}
        }
        
        
        
        public List<Object> parse(String filePath,List<Object> list){
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                //File file = new File("C:\\Source\\xml file\\xml\\XML ppt\\xml day4\\dom_ sax\\sax\\candidate.xml");
                Document dom = db.parse(filePath);
                System.out.println("file path: " + dom.getDocumentURI());
                Element root = dom.getDocumentElement(); //root node
                list = Iterator(root,list);
                System.out.println("********************");
                Iterator  iterator = list.iterator();
                while(iterator.hasNext()){
                    System.out.println(iterator.next().toString());
                }
                //return list;
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return list;
            
        }
        /**
         * 
         * 
         * 
         * 
         */
        public List<Object> Iterator(Element root,List<Object> list) {
            NodeList nodelist = root.getChildNodes();
            for (int i = 0; i < nodelist.getLength(); i++) {
                Node node = nodelist.item(i);
                
                //下面这两个if块中有问题 
                if (node instanceof Text) {
                    String value = node.getNodeValue();
                    //list.add(value);
                    if (value != null && !value.trim().equals("")) {
                        System.out.println("content: " + value);
                        list.add( value);
                    }
                }
                if (node instanceof Element) {
                    System.out.println("node: " + node.getNodeName());
                    Iterator((Element) node);
                    list.add( node.getNodeName());
                }
            }
            //System.out.println("Iterator method success.");
            return list;
        }
    }
      

  8.   

    如果要把所有的信息都保存到一个Map中,然后把Map作为方法的返回值,这样以后我可把它当成一个组件,想用的时候就调用它