解决方案 »
- 数组的倒序输出?麻烦看看为什么不行?谢谢
- 如何中断一个void 的方法,有点研究价值
- 难!用java实现检索模型算法
- java telnet实现的问题
- 关于提取TextField中的值为int的问题
- 如果求一个无向图里的一个任一顶点到其余各个顶点的路径?
- JAVA编译后线程的Bytecode和汇编指令都有哪些区别?可以像写汇编一样写Bytecode吗?
- =====try finally的问题=====
- 请问如何将文件发送给Servlet,感谢!
- 大家看看http://www-900.ibm.com/developerWorks/java/l-oicq/index.shtml好不好?
- java初学者
- java处理彩色图片转为黑白图片?
下面的文章你看看,也许对你有启发http://www.360doc.com/content/08/1230/09/61497_2225163.shtml
-<xml_api_reply version="1">
−<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
−<forecast_information>
<city data="Kuala Lumpur, Selangor"/>
<postal_code data="Kuala Lumpur"/>
</forecast_information>
</weather>
</xml_api_reply>
private final String xmlFile;
private Element root;
private Document document;
private String charSet = "UTF-8"; /**
* 构造方法
* @param xmlFile 文件名
*/
public SteelXmlUtil(String xmlFile) {
this.xmlFile = xmlFile;
} /**
* 创建一个新的XML文件 默认字符集为UTF-8
* @param root 根元素名
* @throws Exception
*/
public void create(String root) throws Exception {
create(root, charSet);
} /**
* 创建一个新的XML文件
* @param root 根元素名
* @param charSet 字符集
* @throws Exception
*/
public void create(String root, String charSet) throws Exception {
if (xmlFile.trim().length() == 0) {
throw new Exception("Can't file name is empty!");
} // 根元素为空时, 默认为MT
this.root = new Element(root.trim().length() == 0 ? "MT" : root); // 字符集为空时, 默认为UTF-8
this.charSet = charSet.trim().length() == 0 ? "UTF-8" : charSet; FileOutputStream xmlWrite = null;
try {
document = new Document(this.root);
XMLOutputter output = new XMLOutputter("", true, charSet);
xmlWrite = new FileOutputStream(xmlFile);
output.output(document, xmlWrite);
} finally {
xmlWrite.close();
}
}
* 打开XML文件,获取根元素
* @throws Exception
*/
public void open() throws Exception {
if (xmlFile.trim().length() == 0) {
throw new Exception("Can't file name is empty!");
}
FileInputStream xmlRead = null;
try {
charSet = getEncoding();
xmlRead = new FileInputStream(xmlFile);
SAXBuilder readXml = new SAXBuilder();
document = readXml.build(xmlRead);
root = document.getRootElement();
} finally {
xmlRead.close();
}
} /**
* 根据提供的路径,查找元素
* @param elePath 路径
* @param eleName 元素
* @return Element 元素
* @throws Exception
*/
public Element findElement(String elePath, String eleName) throws Exception {
return findElement(elePath, eleName, 0);
} /**
* 根据主键查询记录的索引值
* @param elePath 元素路径
* @param eleName 元素名(记录)
* @param primaryKey [n][2] 主键 0:key 1:value
* @return int 索引值
*/
public int getElementIndex(String elePath, String eleName,
String[][] primaryKey) {
try {
int size = findElement(elePath).getChildren(eleName).size();
boolean isTrue;
for (int i = 0; i < size; i++) {
isTrue = true;
HashMap map = getElementAttribs(elePath, eleName, i);
for (int j = 0; j < primaryKey.length; j++) {
if (!primaryKey[j][1].equals(map.get(primaryKey[j][0])
.toString())) {
isTrue = false;
break;
}
}
if (isTrue) {
return i;
}
}
} catch (Exception e) {
}
return -1;
}
* 根据提供的路径,查找第index个元素
* @param elePath 路径
* @param eleName 元素
* @param index 索引
* @return Element
* @throws Exception
*/
public Element findElement(String elePath, String eleName, int index)
throws Exception {
Element element = null;
try {
element = (Element) findElement(elePath).getChildren(eleName).get(
index);
} catch (Exception e) {
throw e;
}
return element;
} /**
* 获取元素的当前子元素个数
* @param elePath 元素路径
* @param eleName 元素名
* @return int 元素总数
*/
public int getChildSize(String elePath, String eleName) {
try {
return findElement(elePath, eleName).getChildren().size();
} catch (Exception e) {
}
return 0;
} /**
* 获取元素的当前指定名称的子元素个数
* @param elePath 元素路径
* @param eleName 元素名
* @param childName 子元素名
* @return int 元素总数
*/
public int getChildSize(String elePath, String eleName, String childName) {
try {
return findElement(elePath, eleName).getChildren(childName).size();
} catch (Exception e) {
}
return 0;
}
* 获取给定的路径值
* @param elePath 路径
* @return Element 路径对应的元素
* @throws Exception
*/
public Element findElement(String elePath) throws Exception {
// 根元素为空,则抛出异常
if (root == null) {
throw new Exception("Can't found root.");
} // 路径为空,返回根元素
if (elePath == null || elePath.trim().length() == 0) {
return root;
} // 遍历路径,找到最后一个元素,路径异常抛出错误。
Element element = root;
String[] paths = getPaths(elePath);
for (int i = 0; i < paths.length; i++) {
element = element.getChild(paths[i]);
}
return element;
} /**
* 获取元素文本值
* @param elePath 元素路径
* @param eleName 元素名
* @param index 元素索引
* @return String 文本值
*/
public String getElementText(String elePath, String eleName, int index) {
try {
return findElement(elePath, eleName, index).getText();
} catch (Exception e) {
}
return "";
}
* 获取元素属性
* @param elePath 元素路径
* @param eleName 元素名
* @param index 元素索引
* @return HashMap
*/
public HashMap getElementAttribs(String elePath, String eleName, int index) {
HashMap map = new HashMap();
try {
List attribs = findElement(elePath, eleName, index).getAttributes();
for (int i = 0; i < attribs.size(); i++) {
Attribute attrib = (Attribute) attribs.get(i);
map.put(attrib.getName(), attrib.getValue());
}
} catch (Exception e) {
}
return map;
} /**
* 元素是否存在
* @param elePath 元素路径
* @return True: 存在; False: 不存在;
*/
public boolean isExistElement(String elePath) {
try {
if (findElement(elePath) != null) {
return true;
}
} catch (Exception e) {
}
return false;
}
* 把路径转换为数组
* @param path 路径
* @return String[]
*/
private String[] getPaths(String path) {
if(path == null) {
return new String[]{};
}
path = path.replace('.', '/').replace('\\', '/');
return path.split("/");
} /**
* 获取XML文件设置的字符集, 未设置则默认为"UTF-8"
* @return String 字符集
*/
private String getEncoding() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(xmlFile));
String head = reader.readLine();
if (head.indexOf("encoding") > 0) {
head = head.substring(head.indexOf("encoding"));
int index = head.indexOf("\"") + 1;
return head.substring(index, head.indexOf("\"", index));
}
} catch (Exception e) {
} finally {
try {
reader.close();
} catch (Exception ex) {
}
}
return "UTF-8";
}
}