日期:2014-05-17 浏览次数:20944 次
HashMap<String, String> xmlMap = new HashMap<String, String>();
xmlMap.put("nameSpace", cnml.getNamespaceURI());
reader.getDocumentFactory().setXPathNamespaceURIs(xmlMap)
------解决方案--------------------
package com.util.utils;
import java.io.File;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 此工具类适用于生成xml报文及读取xml报文
* @author danly.feng
* @category XML
* @since 2012-07-25
*/
@SuppressWarnings("unchecked")
public class XMLUtil {
/**
* 获取xml根
* @param xmlFile xml文件
* @return
*/
public static Element getXMLRoot(File xmlFile) {
Element root = null;
if(xmlFile != null) {
Document doc;
try {
SAXReader reader = new SAXReader();
doc = reader.read(xmlFile);
root = doc.getRootElement();
} catch (Exception e) {
e.printStackTrace();
}
}
return root;
}
/**
* 获取xml根
* @param xmlStr xml格式的字符串
* @return
*/
private static Element getXMLRoot(String xmlContent) {
Element root = null;
if (xmlContent != null) {
SAXReader reader = new SAXReader();
Document doc;
try {
doc = reader.read(new StringReader(xmlContent));
root = doc.getRootElement();
} catch (Exception e) {
e.printStackTrace();
}
}
return root;
}
/**
* 取得xml文件的根节点名称
* @param
* @return
* @throws Exception
*/
public static String getRootName(Object obj, String defaultName) throws Exception {
Element root = null;
if(obj != null) {
if(obj instanceof File) {
root = getXMLRoot((File) obj);
} else if(obj instanceof String) {
root = getXMLRoot((String) obj);
}
}
String rootName = root == null ? defaultName : root.getName();
return rootName;
}
/**
* 返回根节点以下所有有value的节点
* @param xmlDoc xml文本
* @return Map
*/
public static Map<String, String> parseXmlToMap(Element root) {
Map<String, String> xmlMap = null;
try {
if (root != null) {
xml