JDOM对XML文档的读写增删改转换等
用JDOM对XML文档的读写增删改转换等操作,是我自己的练习题,没有整理和注释,只做以后查看之用。主要方法同DOM4J对XML文档的读写增删改转换等文章,涉及到的XML、XSLT等文档不再写入。
package xml.jdom.wkjava;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.jdom.Attribute;
import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;
import org.jdom.xpath.XPath;
public class Test {
Document doc = null;
public Test() throws IOException, TransformerException, JDOMException {
Document doc = load("product.xml");
doc.getRootElement().addContent(new Element("test").addContent(new Text("中文")));
printDoc(doc);
printDoc(trans(doc,"product2cn.xslt"));
doc = createDoc();
printDoc(doc);
updatePrice(doc);
printDoc(doc);
storeDoc(doc, "new.xml");
}
public static void main(String[] args) {
try {
new Test();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
}
public Document load(String xmlfile)
throws FileNotFoundException, JDOMException, IOException{
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new FileInputStream(xmlfile));
return doc;
}
public void printDoc(Document doc) throws IOException{
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
Writer out = new OutputStreamWriter(System.out);
outputter.output(doc,out);
out.flush();
}
public Document createDoc(){
Document doc = new Document();
Element root = new Element("product");
doc.addContent(root);
root.setAttribute(new Attribute("id","p10"))
.addContent(new Element("name").addContent(new CDATA("<test>by weiking</test>")))
.addContent(new Element("price").addContent(new Text("200.00"))).setAttribute("单位","人民币");
return doc;
}
public void updatePrice(Document doc) throws JDOMException{
XPath path = XPath.newInstance("//price");
Element e = (Element)path.selectSingleNode(doc);
e.setText("300.00");
}
public void storeDoc(Document doc,String filename) throws IOException{
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
OutputStream out = new FileOutputStream(filename);
outputter.output(doc,out);
out.close();
}
public Document trans(Document doc,String xslt)
throws TransformerException, FileNotFoundException{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new StreamSource(new FileInputStream(xslt)));
JDOMSource source = new JDOMSource(doc);
JDOMResult result = new JDOMResult();
transformer.transform(source,result);
return result.getDocument();
}
}
weiking
2006-04-20 15:35:06
评论:1
阅读:966
引用:0
@2007-03-15 09:42:48 游客
好是很多,不知道可不可以添加一点注释,
