解决方案 »

  1.   

    推荐一个 xml 解析包 dom4j 。
    不过解析还得自己写,目前没发现好用且完善的把xml转成map的包。
      

  2.   

    一定要用xml配置?
    既然你想读到map里,最简单的用properites来配置
      

  3.   

    是的,一定要,放到map里我只是举个例子,因为我要拿到键值对,方便做各种处理。
      

  4.   

    package xmlpar;import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;import date.Reflect;/**以下使用SAX解析xml文件*/
    public class SaxXml {
    //定义HashMap存储数据
    //key:存储节点的name值
    //value:存储对应节点文本的值
            //xml文件放在src目录下
    static Map<String,String> map=new HashMap<String, String>();
    public static void main(String[] args) throws Exception{
    LoadBean("hibernate.xml");
    System.out.println(map);
    }
    /**解析数据方法*/
    private static void LoadBean(String fileName) throws Exception{
    //构建解析器
    SAXParser p=SAXParserFactory.newInstance().newSAXParser();
    //设置输入流解析数据
    InputStream in=Reflect.class.getClassLoader()
    .getResourceAsStream(fileName);
    //数据处理
    p.parse(in, new DefaultH());

    }
    /**构建DefaultH解析xml*/
    static class  DefaultH extends DefaultHandler{
    //定义路径名称xpath
    StringBuffer xpath=new StringBuffer();
    //定义文本内容变量s
    String s="";
    private String key;
    private String value;
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    //链接路径名
    xpath.append("/").append(qName);
    //获得key的值
    key=attributes.getValue("name");
    //如果节点中没有文本内容
    if("/hibernate-configuration/session-factory".equals(xpath.toString())){
    map.put(key, null);
    }

    }
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    //遇到结束节点时删除这个节点路径
    xpath.delete(xpath.lastIndexOf("/"), xpath.length());
    }
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    //提取节点中的文本
    value=String.copyValueOf(ch, start, length);
    //将数据封装到map中
    map.put(key, value);
    }
    }
    }
      

  5.   

    这个不用推荐了,现在指定要解析必须用dom4j ,这里请教大神,具体代码怎么写?
      

  6.   

    http://www.cnblogs.com/macula/archive/2011/07/27/2118003.html
    http://blog.csdn.net/shan9liang/article/details/8664540#comments
      

  7.   

    package hello.ant;import java.io.InputStream;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class SaxXml {
    public static void main(String[] args) throws Exception {
    Map<String,String> map=new LinkedHashMap();
    InputStream is = SaxXml.class.getResourceAsStream("/hibernate.xml");
    Document doc = new SAXReader().read(is);
    Element root_node = doc.getRootElement();
    //System.out.println(root_node.getName());
    List<Element> child_node_list = root_node.elements();
    for (Element child_node : child_node_list) {
    //System.out.println(child_node.getName());
    String key=child_node.attributeValue("name");
    String value=child_node.getText();
    map.put(key, value);
    List<Element> child_node_child = child_node.elements();
    for(Element child_node_child_list : child_node_child){
    //System.out.println(child_node_child_list.getName());
    String key1=child_node_child_list.attributeValue("name");
    String value1=child_node_child_list.getText();
    map.put(key1, value1);
    }
    }
    for(Map.Entry<String, String> entry:map.entrySet()){
    System.out.println(entry.getKey()+"==="+entry.getValue());
    }
    //System.out.println(map.size());
    }
    }