使用java语言,谁能提供一个读取xml文件,并将其转换成hashmap的通用类
最好支持以下2种xml格式:1、<person><name>zhangsan</name><age>18</age><sex>male</sex></person>2、<...
最好支持以下2种xml格式:
1、
<person>
<name>zhangsan</name>
<age>18</age>
<sex>male</sex>
</person>
2、
<person>
<name value="zhangsan"></name>
<age value="18"></age>
<sex value="male"></sex>
</person>
其中各标签名为键,value或者是标签中的文字为值 展开
1、
<person>
<name>zhangsan</name>
<age>18</age>
<sex>male</sex>
</person>
2、
<person>
<name value="zhangsan"></name>
<age value="18"></age>
<sex value="male"></sex>
</person>
其中各标签名为键,value或者是标签中的文字为值 展开
4个回答
展开全部
/**
* 文件名:ReadXML.java
* 版本信息: V1.0
* 日期:2012-3-15
* Copyright Corporation 2010
* 版权所有 SNZKE
*/
package org.study.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* 项目名称:JavaOO
* <br>类名称:ReadXML
* <br>类描述:
* <br>创建人:zengfc
* <br>创建时间:2012-3-15 上午10:29:31
* <br>修改人:zengfc
* <br>修改时间:2012-3-15 上午10:29:31
* <br>修改备注:
* <br>@version
*/
public class ReadXML {
private static final String TAGNAME = "_tagName";
private static final String ATTRIBUTES = "_attributes";
private static final String CONTENT = "_content";
private Node root;
public ReadXML(String path,String rootName){
loadXMLFILE(path,rootName);
}
public ReadXML(String path){
loadXMLFILE(path);
}
public void loadXMLFILE(String path){
loadXMLFILE(path,null);
}
public void loadXMLFILE(String path,String rootName){
File file = new File(path);
try {
if (file.exists()) {
Document document = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new File(path));
if(rootName == null || "".equals(rootName)){
root = document.getFirstChild();
}else{
NodeList nodeList = document.getElementsByTagName(rootName);//根节点
root = nodeList.item(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Map<String,Object> readXml(){
try {
return readXml(root,null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Map<String,Object> readXml(Node node,Map<String,Object> parent) throws Exception{
Map<String,Object> map=null;
String name = node.getNodeName();
map=new HashMap<String,Object>();
if("#text".equals(name)){
parent.put(CONTENT, node.getNodeValue());
return null;
}
map.put(TAGNAME, name);
map.put(CONTENT, node.getTextContent());
map.put(ATTRIBUTES, loadAttributes(node));
if(node.getNodeType() == Node.TEXT_NODE && !node.getNodeValue().trim().equals("")){
map.put("value", node.getNodeValue());
}
if(node.getFirstChild() != null){
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
Map<String,Object> par = new HashMap<String,Object>();
par.putAll(map);
String childName = childNode.getNodeName();
Map<String,Object> inner = readXml(childNode,par);
if(inner != null){
if(map.containsKey(childName)){
Object obj = map.get(childName);
if(obj instanceof Map<?,?>){
List<Map<?,?>> list = new ArrayList<Map<?,?>>();
list.add((Map<?,?>)map.get(childName));
list.add(inner);
map.put(childName, list);
}else if(obj instanceof List<?>){
((List<Map<?,?>>)map.get(childName)).add(inner);
}
}else{
map.put(childName, inner);
}
}
}
}
return map;
}
private Map<String,String> loadAttributes(Node node){
Map<String,String> attributes;
if(node.hasAttributes()){
attributes = new HashMap<String,String>();
}else{
return null;
}
NamedNodeMap maps = node.getAttributes();
for (int i = 0; i < maps.getLength(); i++) {
Node nod = maps.item(i);
attributes.put(nod.getNodeName(), nod.getNodeValue());
}
return attributes;
}
public static void main(String[] args) {
Map<String,Object> map = new ReadXML("person.xml").readXml();
System.out.println(map);
}
}
* 文件名:ReadXML.java
* 版本信息: V1.0
* 日期:2012-3-15
* Copyright Corporation 2010
* 版权所有 SNZKE
*/
package org.study.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* 项目名称:JavaOO
* <br>类名称:ReadXML
* <br>类描述:
* <br>创建人:zengfc
* <br>创建时间:2012-3-15 上午10:29:31
* <br>修改人:zengfc
* <br>修改时间:2012-3-15 上午10:29:31
* <br>修改备注:
* <br>@version
*/
public class ReadXML {
private static final String TAGNAME = "_tagName";
private static final String ATTRIBUTES = "_attributes";
private static final String CONTENT = "_content";
private Node root;
public ReadXML(String path,String rootName){
loadXMLFILE(path,rootName);
}
public ReadXML(String path){
loadXMLFILE(path);
}
public void loadXMLFILE(String path){
loadXMLFILE(path,null);
}
public void loadXMLFILE(String path,String rootName){
File file = new File(path);
try {
if (file.exists()) {
Document document = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new File(path));
if(rootName == null || "".equals(rootName)){
root = document.getFirstChild();
}else{
NodeList nodeList = document.getElementsByTagName(rootName);//根节点
root = nodeList.item(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Map<String,Object> readXml(){
try {
return readXml(root,null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Map<String,Object> readXml(Node node,Map<String,Object> parent) throws Exception{
Map<String,Object> map=null;
String name = node.getNodeName();
map=new HashMap<String,Object>();
if("#text".equals(name)){
parent.put(CONTENT, node.getNodeValue());
return null;
}
map.put(TAGNAME, name);
map.put(CONTENT, node.getTextContent());
map.put(ATTRIBUTES, loadAttributes(node));
if(node.getNodeType() == Node.TEXT_NODE && !node.getNodeValue().trim().equals("")){
map.put("value", node.getNodeValue());
}
if(node.getFirstChild() != null){
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
Map<String,Object> par = new HashMap<String,Object>();
par.putAll(map);
String childName = childNode.getNodeName();
Map<String,Object> inner = readXml(childNode,par);
if(inner != null){
if(map.containsKey(childName)){
Object obj = map.get(childName);
if(obj instanceof Map<?,?>){
List<Map<?,?>> list = new ArrayList<Map<?,?>>();
list.add((Map<?,?>)map.get(childName));
list.add(inner);
map.put(childName, list);
}else if(obj instanceof List<?>){
((List<Map<?,?>>)map.get(childName)).add(inner);
}
}else{
map.put(childName, inner);
}
}
}
}
return map;
}
private Map<String,String> loadAttributes(Node node){
Map<String,String> attributes;
if(node.hasAttributes()){
attributes = new HashMap<String,String>();
}else{
return null;
}
NamedNodeMap maps = node.getAttributes();
for (int i = 0; i < maps.getLength(); i++) {
Node nod = maps.item(i);
attributes.put(nod.getNodeName(), nod.getNodeValue());
}
return attributes;
}
public static void main(String[] args) {
Map<String,Object> map = new ReadXML("person.xml").readXml();
System.out.println(map);
}
}
追问
现在还没时间试,晚点我试试看,还有个小问题,dom解析大数据量文件时会比较占用内存,如果用jdk6的stax方式解析,不知道行不行?
追答
如果要关注效率的话,那还是用dom4j比较合适..
展开全部
没分啊
<person>
就是标记。name age sex 就是属性跟对应的值
<person>
就是标记。name age sex 就是属性跟对应的值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static Map ReadXml(){
File file = new File(path);
Map map=null;
try {
if (file.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc=db.parse(new File(path));
NodeList nodeList = doc.getElementsByTagName("LOGINDATE");//跟节点
Node fatherNode = nodeList.item(0);
NodeList childNodes = fatherNode.getChildNodes();
map=new HashMap();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof org.w3c.dom.Element) {
if(!childNode.getFirstChild().getNodeValue().equals(null)){
map.put(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
/* System.out.println(childNode.getNodeName()
+ ":" + childNode.getFirstChild().getNodeValue());*/
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
File file = new File(path);
Map map=null;
try {
if (file.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc=db.parse(new File(path));
NodeList nodeList = doc.getElementsByTagName("LOGINDATE");//跟节点
Node fatherNode = nodeList.item(0);
NodeList childNodes = fatherNode.getChildNodes();
map=new HashMap();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof org.w3c.dom.Element) {
if(!childNode.getFirstChild().getNodeValue().equals(null)){
map.put(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
/* System.out.println(childNode.getNodeName()
+ ":" + childNode.getFirstChild().getNodeValue());*/
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xml 文件解析 涉及到 属性 和 相同的节点元素, 解析成map需要做相应的处理,
具体实现可以看以下链接,
http://blog.csdn.net/u011490355/article/details/77775544
具体实现可以看以下链接,
http://blog.csdn.net/u011490355/article/details/77775544
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询