package com.ldg;
/***
* �ļ���ReadXML.java
* �汾��Ϣ�� V1.0
* ���ڣ�2012-3-15
* Copyright Corporation 2010
* ��Ȩ���� SNZKE
*/
//package org.study.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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;
//import ObjectToXmlUtil;
//import tcp.RfidTcpServer;
/**
* ��Ŀ��ƣ�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;
}
@SuppressWarnings("unchecked")
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("");
//String path= ReadXml.class.getResource("/");
ReadXml p = new ReadXml(ReadXml.class.getResource("/").getPath().substring(1)+"MyXml.xml");
Map<String,Object> map = p.readXml();
//System.out.println(ReadXml.class.getResource("/").getPath().substring(1)+"MyXml.xml");
System.out.println(map);
try {
List<?> list = ObjectToXmlUtil.objectXmlDecoder(ReadXml.class.getResource("/").getPath().substring(1)
+ "MyXml.xml");
Iterator<?> it = list.iterator();
String a = "";
while (it.hasNext()) {
a = it.next().toString();
System.out.println(a);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}