/*
 * Created on 2004-7-3
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.jc.xml.util;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class JdomUtil { private static Logger logger = Logger.getLogger(JdomUtil.class.getName());
private Map propertyCache = new HashMap();
private File file;
private Document doc; public JdomUtil() { }
public JdomUtil(String file) {
this.file = new File(file);
SAXBuilder builder = new SAXBuilder();
try {
doc = builder.build(new File(file));
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
 * 解析属性名称,如England.David.BeckHam将被解析成String[]数组,以"."分解
 * 
 * @param name
 * @return String[]
 */
private String[] parsePropertyName(String name) {
if (name == null) {
return null;
}
int size = 1;
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) == '.') {
size++;
}
}
String[] proName = new String[size];
StringTokenizer token = new StringTokenizer(name, ".");
int i = 0;
while (token.hasMoreElements()) {
proName[i] = token.nextToken();
i++;
}
return proName;
} /**
 * 返回name属性对应的值
 * 
 * @param name
 * @return
 */
public String getProperty(String name) {
if (propertyCache.containsKey(name)) {
return (String) propertyCache.get(name);
}
String[] proName = this.parsePropertyName(name);
Element element = doc.getRootElement();
for (int i = 0; i < proName.length; i++) {
element = element.getChild(proName[i]);
if (element == null) {
return "no this value";
}
}
String value = element.getTextTrim();
if ("".equals(value)) {
return null;
} else {
propertyCache.put(name, value);
return value;
}
} /**
 * 修改属性的值
 * 
 * @param name
 * @param value
 */
public void setProperty(String name, String value) {
propertyCache.put(name, value); String[] proName = this.parsePropertyName(name);
Element element = doc.getRootElement();
for (int i = 0; i < proName.length; i++) {
if (element.getChild(proName[i]) == null) {
element.addContent(proName[i]);
}
element = element.getChild(proName[i]);
}
element.setText(value);
this.saveProperties();
}
    

/**
 * 删除一个属性和其对应的值
 * @param name
 */
public void deleteProperty(String name) {
if (propertyCache.containsKey(name)) {
propertyCache.remove(name);
}
String[] proName = this.parsePropertyName(name);
Element element = doc.getRootElement();
for (int i = 0; i < proName.length; i++) {
element = element.getChild(proName[i]);
if (element == null) {
return;
}
}
//得到其上级结点
element=element.getParent();
logger.info(">>>>>>>"+element.getName());
element.removeChild(proName[proName.length - 1]);
this.saveProperties();
} /**
 * 
 * 保存文件
 */
private synchronized void saveProperties() {
OutputStream out = null;
boolean error = false;
// Write data out to a temporary file first.
File tempFile = null;
try {
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
// Use JDOM's XMLOutputter to do the writing and formatting. The
// file should always come out pretty-printed.
XMLOutputter outputter = new XMLOutputter("    ", true);
out = new BufferedOutputStream(new FileOutputStream(tempFile));
outputter.output(doc, out);
} catch (Exception e) {
e.printStackTrace();
// There were errors so abort replacing the old property file.
error = true;
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
error = true;
}
}
// No errors occured, so we should be safe in replacing the old
if (!error) {
// Delete the old file so we can replace it.
file.delete();
// Rename the temp file. The delete and rename won't be an
// automic operation, but we should be pretty safe in general.
// At the very least, the temp file should remain in some form.
tempFile.renameTo(file);
}
} public static void main(String[] args) {
JdomUtil jdom = new JdomUtil("term.xml");
logger.info(">>>size<<<" + jdom.parsePropertyName("1.2.3.4").length);
logger.info(">>>" + jdom.getProperty("mail.smtp.port"));
// jdom.setProperty("mail.smtp.port", "101");
jdom.deleteProperty("mail.smtp.port");
}}

解决方案 »

  1.   

    楼上给出的是JAVA操作XML,实现了读写。
    那么如何将JSP中的数据动态输出成XML呢?
    举个例子,谢谢。
      

  2.   

    小弟,问个菜一点的问题。
    把XML格式写出来,往里边插入变量的那种,文件后缀名是xml,还是JSP
      

  3.   

    jsp文件里直接写xml:
    <%@ page contentType="text/xml;charset=GBK" %>
    <?xml version="1.0" encoding="GBK" ?>
    <?xml-stylesheet type="text/xsl" href="/yourApp/query/xsl/<%=(String)session.getAttribute("style")%>.xsl" ?>