各位兄弟同仁,我现在用dom解析xml,但不知道怎样把得到结果放入到MAP中去,希望哪位能给出一些代码或是思路
我用List存过,但不好,所以想换成用map来存放
<?xml version="1.0" encoding="UTF-8"?><elements>
<student sid="001">
<sheet_Amount>3456</sheet_Amount>
<sheet_Approve>approved</sheet_Approve>
<sheet_Name>ruantongdongli</sheet_Name>
<sheet_ID>1111111111111111</sheet_ID>
<sheet_Date>20090524</sheet_Date>
</student>
</elements>
用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\\workflowElement.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){
//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, 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();
//List<Object> list = new ArrayList<Object>();
for (int i = 0; i < nodelist.getLength(); i++) {
Node node = nodelist.item(i);
if (node instanceof Text) {
String value = node.getNodeValue();
if (value != null && !value.trim().equals("")) {
//System.out.println("content: " + value);
list.add(value);
}
}
if (node instanceof Element) {
//System.out.println("node: " + node.getNodeName());
list.add( node.getNodeName());
Iterator( (Element)node, list);
}
}
return list;
}
}
我用List存过,但不好,所以想换成用map来存放
<?xml version="1.0" encoding="UTF-8"?><elements>
<student sid="001">
<sheet_Amount>3456</sheet_Amount>
<sheet_Approve>approved</sheet_Approve>
<sheet_Name>ruantongdongli</sheet_Name>
<sheet_ID>1111111111111111</sheet_ID>
<sheet_Date>20090524</sheet_Date>
</student>
</elements>
用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\\workflowElement.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){
//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, 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();
//List<Object> list = new ArrayList<Object>();
for (int i = 0; i < nodelist.getLength(); i++) {
Node node = nodelist.item(i);
if (node instanceof Text) {
String value = node.getNodeValue();
if (value != null && !value.trim().equals("")) {
//System.out.println("content: " + value);
list.add(value);
}
}
if (node instanceof Element) {
//System.out.println("node: " + node.getNodeName());
list.add( node.getNodeName());
Iterator( (Element)node, list);
}
}
return list;
}
}
3楼说的我倒还没仔细想过,现在我们只需要结果出来,达到我预期的目的就可以了
1楼你说的我知道啊,但问题是我在递归里怎么才能取得当前的node的name所对应的值呢
public static void main(String[] args) {
List list = getListForServices("student");
for(int i = 0; i < list.size(); i++){
Element e = (Element) list.get(i);
List drop = e.selectNodes("sheet_Amount");
for(int j = 0; j < drop.size(); j++){
Element ee = (Element) drop.get(j);
//.把你想要的元素放到map里面去
}
List skill = e.selectNodes("sheet_Approve");
for(int j = 0; j < skill.size(); j++){
Element ee = (Element) drop.get(j);
//.把你想要的元素放到map里面去
}
......
}
private List<Element> getListForServices(String node)
{
SAXReader saxReader = new SAXReader();
Document document = null;
try
{
document = saxReader.read(TestMain.class.getResourceAsStream("/.....xml"));
//TestMain是方法所在的这个类,/......xml是你放xml的路径.
}
catch (DocumentException e)
{
e.printStackTrace();
}
List<Element> list = document.selectNodes(node);
return list;
}