各位仁兄,帮帮忙,我现在要对一个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;
}
}
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;
}
}
list.add( node.getNodeName());
换换位置
list.add( node.getNodeName());
换换位置
- <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>
嗯,抽到外面,或者传入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,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;
}
}