JAVA对XML的几种解析方法讲解(JDOM)
为减少DOM、SAX的编码量,出现了JDOM;
优点:极大减少了代码量。
使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。
必须得下载jdom.jar文件
package xml.jdom;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import xml.XmlDocument;
public class JDomDemo implements XmlDocument {
public static void main(String[] args){
JDomDemo jDomeDemo = new JDomDemo();
jDomeDemo.createXml("Test2.xml");
jDomeDemo.parserXml("Test2.xml");
}
@Override
public void createXml(String fileName) {
Element root = new Element("employees");
Document document = new Document(root);
Element employee = new Element("employee");
root.addContent(employee);
Element name = new Element("name");
name.setText("ddvip");
employee.addContent(name);
Element sex = new Element("sex");
sex.setText("m");
employee.addContent(sex);
Element age = new Element("age");
age.setText("23");
employee.addContent(age);
XMLOutputter XMLOut = new XMLOutputter();
try {
XMLOut.output(document, new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void parserXml(String fileName) {
SAXBuilder builder = new SAXBuilder(false);
try {
Document document = builder.build(fileName);
Element employees = document.getRootElement
相关文档:
import java.text.DecimalFormat
double a = 2.3659874;
//小数格式化,引号中的0.000表示保留小数点后三位(第四位四舍五入)
DecimalFormat df = new DecimalFormat("0.000");
String num = df.format(a);
System.out.println(num);
输出结果就是 2.366
Java计算时间差
比如:现在是2004-03-26 13:31:40
&nbs ......
本文阐述了怎么使用DBMS存储过程。我阐述了使用存储过程的基本的和高级特性,比如返回ResultSet。本文假设你对DBMS和JDBC已经非常熟悉,也假设你能够毫无障碍地阅读其它语言写成的代码(即不是Java的语言),但是,并不要求你有任何存储过程的编程经历。
存储过程是指保存在数据库并在数据库端执行的程序。你可以使用特殊 ......
Java 标准与规范
本栏目提供了大量的 Java 技术标准与规范的简介、官方网址以及 developerWorks 网站上相关的技术资源。通过本栏目,您不但可以了解当前 Java 社区主要的技术标准和规范,还可以通过查看相关的技术文章和教程进行更深入的学习,从而更好地为实际的 Java 项目进行技术选型。
A B C D E F ......
这是一个Java版的世界时钟示例,移植自Gerrit创建的同名Swing应用(http://www.jug-muenster.de/swing-worldclock-427
),需要运行在JRE1.5或以上环境当中。
移植此示例主要是因为前一阵移植过Gerrit的swing原子钟示例,所谓好事成双,这个世界时钟的示例自然不能放过(话说Gerrit为什么那么喜欢做时钟?…&hellip ......