解决方案 »
- java图形界面里面怎么实现图片缩放
- Collection List接口的问题
- 出个简单问题,大家进来看看~
- 调用的一个方法返回一个InputStream,然后再把此InputStream作为参数传给另一个方法,我想问那谁来负责关闭此InputStream了?
- 再散100分,准备开发一款手机游戏,名称暂定“轰炸东京”,求好的游戏背景和好的美工。
- JAVA反射,field.set(obj1,obj2)obj2类型的问题
- 为什么该段程序不响应键盘?
- 电子商务
- 我该去那里工作阿??——高分请求帮忙!!
- 有没有类似vb中unload me 的语句,可以unload某一个Frame,而不仅仅是setVisible(false) ?
- java如何将图片合并成mp4等音频格式播放?
- 关于拆分窗口的一些问题
不过解析还得自己写,目前没发现好用且完善的把xml转成map的包。
既然你想读到map里,最简单的用properites来配置
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);
}
}
}
http://blog.csdn.net/shan9liang/article/details/8664540#comments
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());
}
}