读书人

怎么将解析xml文件得到结果存到MAP中

发布时间: 2012-01-07 21:41:55 作者: rapoo

如何将解析xml文件得到结果存到MAP中
各位兄弟同仁,我现在用dom解析xml,但不知道怎样把得到结果放入到MAP中去,希望哪位能给出一些代码或是思路
我用List存过,但不好,所以想换成用map来存放

XML code
<?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存时我的代码
Java code
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;    }        }


[解决办法]
....你想用什么做键值,什么做 value 值,直接放进去不就可以了吗
[解决办法]
是啊,你要想好用什么做key,再map里是不能重复的呀
------解决方案--------------------


如果你使用的是Apache Xerces解析xml的话,那么你得为DocumentBuilderFactory 提供schema才能够解析这个xml,否则你将无法读取这个xml,最好的办法是为这个xml提供schema文件。但是DOM4J不会有这个限制。
[解决办法]
看看我写的代码,不知道对你有没有帮助.
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;
}

读书人网 >J2SE开发

热点推荐