PO VO DTO POJO

PO :persistent object持久对象

1 .有时也被称为Data对象,对应数据库中的entity,可以简单认为一个PO对应数据库中的一条记录。
2 .在hibernate持久化框架中与insert/delet操作密切相关。
3 .PO中不应该包含任何对数据库的操作。

POJO :plain ordinary java object 无规则简单java对象

一个中间对象,可以转化为PO、DTO、VO。
1 .POJO持久化之后==〉PO
(在运行期,由Hibernate中的cglib动态把POJO转换为PO,PO相对于POJO会增加一些用来管理数据库entity状态的属性和方法。PO对于programmer来说完全透明,由于是运行期生成PO,所以可以支持增量编译,增量调试。)
2 .POJO传输过程中==〉DTO
3 .POJO用作表示层==〉VO
PO 和VO都应该属于它。

BO :business object 业务对象

封装业务逻辑为一个对象(可以包括多个PO,通常需要将BO转化成PO,才能进行数据的持久化,反之,从DB中得到的PO,需要转化成BO才能在业务层使用)。
关于BO主要有三种概念
1 、只包含业务对象的属性;
2 、只包含业务方法;
3 、两者都包含。
在实际使用中,认为哪一种概念正确并不重要,关键是实际应用中适合自己项目的需要。

VO :value object值对象 / view object表现层对象

1 .主要对应页面显示(web页面/swt、swing界面)的数据对象。
2 .可以和表对应,也可以不,这根据业务的需要。
注 :在struts中,用ActionForm做VO,需要做一个转换,因为PO是面向对象的,而ActionForm是和view对应的,要将几个PO要显示的属性合成一个ActionForm,可以使用BeanUtils的copy方法。

DTO (TO) :Data Transfer Object数据传输对象

1 .用在需要跨进程或远程传输时,它不应该包含业务逻辑。
2 .比如一张表有100个字段,那么对应的PO就有100个属性(大多数情况下,DTO 内的数据来自多个表)。但view层只需显示10个字段,没有必要把整个PO对象传递到client,这时我们就可以用只有这10个属性的DTO来传输数 据到client,这样也不会暴露server端表结构。到达客户端以后,如果用这个对象来对应界面显示,那此时它的身份就转为VO。

DAO :data access object数据访问对象

1 .主要用来封装对DB的访问(CRUD操作)。
2 .通过接收Business层的数据,把POJO持久化为PO。

Java连接各种数据库

1. Oracle8/8i/9i数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2. DB2数据库

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3. SQL Server7.0/2000数据库

String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=testdb";
String user="sa";
String password="sa";
Connection conn= DriverManager.getConnection(url,user,password);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

4. Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

5. Informi数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB为数据库名
Connection conn= DriverManager.getConnection(url);

6. MySQL数据库

Class.forName("org.gjt.mm.mysql.Driver").newInstance();//或者Class.forName("com.mysql.jdbc.Driver");
//myDB为数据库名
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
Connection conn= DriverManager.getConnection(url);

7. PostgreSQL数据库

Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

8. access数据库直连用ODBC的

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver
(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;

JAVA引用类型和原始类型区别

本文是从 Practical Java(Addison-Wesley 出版)一书节选改编而来的。您可以从 Borders.com 订购该书。请阅读我们对作者 Peter Haggar 的采访。
Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。另外,Java 还为每个原始类型提供了封装类(Wrapper)。如果需要一个整型变量,是使用基本的 int 型呢,还是使用 Integer 类的一个对象呢?如果需要声明一个布尔类型,是使用基本的 boolean,还是使用 Boolean 类的一个对象呢?本文可帮助您作出决定。
下表列出了原始类型以及它们的对象封装类。

原始类型 封装类
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

引用类型和原始类型的行为完全不同,并且它们具有不同的语义。例如,假定一个方法中有两个局部变量,一个变量为 int 原始类型,另一个变量是对一个 Integer 对象的对象引用:
int i = 5;// 原始类型
Integer j = new Integer(10);// 对象引用
这两个变量都存储在局部变量表中,并且都是在 Java 操作数堆栈中操作的,但对它们的表示却完全不同。(本文中以下部分将用通用术语堆栈代替操作数堆栈或局部变量表。)原始类型 int 和对象引用各占堆栈的 32 位。(要表示一个 int 或一个对象引用,Java 虚拟机实现至少需要使用 32 位存储。)Integer 对象的堆栈项并不是对象本身,而是一个对象引用。
Java 中的所有对象都要通过对象引用访问。对象引用是指向对象存储所在堆中的某个区域的指针。当声明一个原始类型时,就为类型本身声明了存储。前面的两行代码表示如下:
引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始类型实例变量的缺省值与它们的类型有关。
许多程序的代码将同时包含原始类型以及它们的对象封装。当检查它们是否相等时,同时使用这两种类型并了解它们如何正确相互作用和共存将成为问题。程序员必须了解这两种类型是如何工作和相互作用的,以避免代码出错。
例如,不能对原始类型调用方法,但可以对对象调用方法:

int j = 5;
j.hashCode();     // 错误
//…
Integer i = new Integer(5);
i.hashCode();     // 正确

使用原始类型无须调用 new,也无须创建对象。这节省了时间和空间。混合使用原始类型和对象也可能导致与赋值有关的意外结果。看起来没有错误的代码可能无法完成您希望做的工作。例如:

import java.awt.Point;
class Assign
{
  public static void main(String args[])
  {
    int a = 1;
    int b = 2;
    Point x = new Point(0,0);
    Point y = new Point(1,1);//1
    System.out.println("a is " + a);
    System.out.println("b is " + b);
    System.out.println("x is " + x);
    System.out.println("y is " + y);
    System.out.println("Performing assignment and setLocation…");
    a = b;
    a++;
    x = y;//2
    x.setLocation(5,5);//3
    System.out.println("a is "+a);
    System.out.println("b is "+b);
    System.out.println("x is "+x);
    System.out.println("y is "+y);
  }
}

这段代码生成以下输出:
a is 1
b is 2
x is java.awt.Point[x=0,y=0]
y is java.awt.Point[x=1,y=1]
Performing assignment and setLocation…
a is 3
b is 2
x is java.awt.Point[x=5,y=5]
y is java.awt.Point[x=5,y=5]
修改整数 a 和 b 的结果没什么意外的地方。b 的值被赋予整型变量 a,结果 a 的值增加了 1。这一输出反映了我们希望发生的情况。但是,令人感到意外的,是在赋值并调用 setLocation之后 x 和 y 对象的输出。我们在完成 x = y 赋值之后特意对 x 调用了 setLocation,x 和 y 的值怎么会相同呢?我们毕竟将 y 赋予 x,然后更改了 x,这与我们对整数 a 和 b 进行的操作没什么不同。
这种混淆是由原始类型和对象的使用造成的。赋值对这两种类型所起的作用没什么不同。但它可能看起来所有不同。赋值使等号 (=) 左边的值等于右边的值。这一点对于原始类型(如前面的 int a 和 b)是显而易见的。对于非原始类型(如 Point 对象),赋值修改的是对象引用,而不是对象本身。因此,在语句
x = y;
之后,x 等于 y。换句话说,因为 x 和 y 是对象引用,它们现在引用同一个对象。因此,对 x 所作的任何更改也会更改 y。下面是 //1 处的代码执行以后的情况:
执行 //2 处的赋值以后情况如下:
当在 //3 处调用 setLocation 时,这一方法是对 x 引用的对象执行的。因为 x 引用的 Point 对象也正是 y 所引用的对象,所以我们现在得到以下结果:
因为 x 和 y 引用同一个对象,所以对 x 执行的所有方法与对 y 执行的方法都作用于同一个对象。
区分引用类型和原始类型并理解引用的语义是很重要的。若做不到这一点,则会使编写的代码无法完成预定工作。

Java基本数据类型与流的操作方法

Java中除了二进制文件和使用文本文件外还有基于Data的数据操作,这里的Data指的是Java的基本数据类型和String。基本数据类型包括byte、int、char、long、float、double、boolean和short。
说到Java的基本数据类型必须谈到的两个类是DataInputStream和DataOutputStream。它们提供了对Java基本 数据类型的操作,但是这些方法事实上是在两个重要的接口中定义的DataInput和DataOutput,它们的功能就是把二进制的字节流转换成 Java的基本数据类型,同时还提供了从数据中使用UTF-8编码构建String的功能。有一个重要的类RandomAccessFile实现了 DataInput和DataOutput两个接口使得他能够对文件同时进行写和读的操作。
在DataInputStream和DataOutputStream两个类中的方法都很简单,基本结构为readXXXX()和 writeXXXX()其中XXXX代表基本数据类型或者String。在这里不多讲述,不过值得一提的是我们有必要读读java中unicode的编码 规则,在API doc中有比较详细的介绍。通常我们的对象有很多都是由java的基本数据类型构成的,比如一个人的信息包括姓名,电子信箱,电话号码和性别等。其实我们 可以用DataInputStream中的方法和DataOutputStream中的方法按照一定的序列把数据写入流中再按照相同的序列把他们读取出 来,这就是我们自己实现的序列化,这可以用在数据传输中,比如在J2ME联网程序中使用序列化机制传输数据。下面我们看看如何自己实现序列化,首先我们要 有两个构造函数其中一个参数为空。

public Account()
{
}
public Account(String userName, String email, int age, boolean gender)
{
  this.userName = userName;
  this.email = email;
  this.age = age;
  this.gender = gender;
}

当我们进行序列化的时候也很简单,我们只是往DataOutputStream中按照顺序写入对象的成员变量。例如:

public void serialize(DataOutputStream dos) throws IOException
{
  dos.writeUTF(userName);
  dos.writeUTF(email);
  dos.writeInt(age);
  dos.writeBoolean(gender);
}

当我们进行反序列化的时候则按照相同的顺序从DataInputStream里面读取数据并赋值给成员变量。例如:

public static Account deserialize(DataInputStream dis) throws IOException
{
  Account account = new Account();
  account.userName = dis.readUTF();
  account.email = dis.readUTF();
  account.age = dis.readInt();
  account.gender = dis.readBoolean();
  return account;
}

为了便于调试我们还提供一个toString()的方法打印出对象的实际信息。这是个好的习惯。

public String toString()
{
 return "UserName = " + userName
      + " Email = " + email
      + " age = " + age
      + " gender = " + (gender ? "male" : "female");
}

为了测试序列化我们编写下面的程序进行测试,代码比较简单。

package com.j2medev.mingjava;
import java.io.*;
public class TestDataIO
{
  public static void main(String[] args) throws IOException
  {
    Account account = new Account("mingjava","eric.zhan@263.net",25,true);
    System.out.println("before serialization………");
    System.out.println(account.toString());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    account.serialize(dos);
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Account sAccount = Account.deserialize(dis);
    System.out.println("after serialization……….");
    System.out.println(sAccount.toString());
    dos.close();
    dis.close();
  }
}
package com.j2medev.mingjava;
import java.io.*;
public class Account
{
  private String userName = "";
  private String email = "";
  private int age = 0;
  private boolean gender = false;
 public Account()
  {}
 public Account(String userName, String email, int age, boolean gender)
  {
    this.userName = userName;
    this.email = email;
    this.age = age;
    this.gender = gender;
  }
 public void serialize(DataOutputStream dos) throws IOException
  {
    dos.writeUTF(userName);
    dos.writeUTF(email);
    dos.writeInt(age);
    dos.writeBoolean(gender);
  }
 public static Account deserialize(DataInputStream dis) throws IOException
  {
    Account account = new Account();
    account.userName = dis.readUTF();
    account.email = dis.readUTF();
    account.age = dis.readInt();
    account.gender = dis.readBoolean();
    return account;
  }
 public String toString()
  {
    return "UserName = " + userName
         + " Email = " + email
         + " age = " + age
         + " gender = " + (gender ? "male" : "female");
  }
}

编译运行程序在控制台输出:

before serialization………
UserName = mingjava Email = eric.zhan@263.net age = 25 gender = male
after serialization……….
UserName = mingjava Email = eric.zhan@263.net age = 25 gender = male

序列化成功!

简单介绍log4j一般的使用步骤

1.建一个log4j.properties的配置文件,放到有main的入口类的相同路径下。

log4j.rootLogger=INFO, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=Log4jTest.log
log4j.appender.R.MaxFileSize=1000KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
#define log level for package
log4j.logger.log4jTest=DEBUG

2.在入口类的静态区初始化log4j

static {
PropertyConfigurator.configure(Log4jTest.class
.getResource("log4j.properties"));
}

3.在每一个需要log的地方都初始化一个私有静态的变量
private static Logger logger = Logger.getLogger(Log4jTest.class);
4.使用log4j提供的方法
logger.info("hello");
5.用log4j输出异常的堆栈信息
logger.error("oops, got an exception: ", e);

log4j中的ConversionPattern参数的格式含义

格式名 含义
%c 输出日志信息所属的类的全名
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-MM-dd HH:mm:ss },输出类似:2002-10-18- 22:10:28
%f 输出日志信息所属的类的类名
%l 输出日志事件的发生位置,即输出日志信息的语句处于它所在的类的第几行
%m 输出代码中指定的信息,如log(message)中的message
%n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n”
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。如果是调用debug()输出的,则为DEBUG,依此类推
%r 输出自应用启动到输出该日志信息所耗费的毫秒数
%t 输出产生该日志事件的线程名

使用dom4j读写xml文件

dom4j 是一种解析 XML 文档的开放源代码 XML 框架。本文介绍如何使用包含在 dom4j 中的解析器创建并修改 XML 文档。

dom4j API 包含一个解析 XML 文档的工具。本文中将使用这个解析器创建一个示例 XML 文档。清单 1 显示了这个示例 XML 文档,catalog.xml。

清单 1. 示例 XML 文档(catalog.xml)

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!--An XML Catalog-->
  <?target instruction?>
  <journal title="XML Zone"
  publisher="IBM developerWorks">
    <article level="Intermediate" date="December-2001">
      <title>Java configuration with XML Schema</title>
      <author>
        <firstname>Marcello</firstname>
        <lastname>Vitaletti</lastname>
      </author>
    </article>
  </journal>
</catalog>

然后使用同一个解析器修改 catalog.xml,清单 2 是修改后的 XML 文档,catalog-modified.xml。

清单 2. 修改后的 XML 文档(catalog-modified.xml)

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!--An XML catalog-->
  <?target instruction?>
  <journal title="XML Zone"
  publisher="IBM developerWorks">
    <article level="Introductory" date="October-2002">
      <title>Create flexible and extensible XML schemas</title>
      <author>
        <firstname>Ayesha</firstname>
        <lastname>Malik</lastname>
      </author>
    </article>
  </journal>
</catalog>

与 W3C DOM API 相比,使用 dom4j 所包含的解析器的好处是 dom4j 拥有本地的 XPath 支持。DOM 解析器不支持使用 XPath 选择节点。
本文包括以下几个部分:
– 预先设置
– 创建文档
– 修改文档
– 预先设置
这个解析器可以从 http://dom4j.org 获取。通过设置使 dom4j-1.4/dom4j-full.jar 能够在 classpath 中访问,该文件中包括 dom4j 类、XPath 引擎以及 SAX 和 DOM 接口。如果已经使用了 JAXP 解析器中包含的 SAX 和 DOM 接口,向 classpath 中增加 dom4j-1.4/dom4j.jar 。 dom4j.jar 包括 dom4j 类和 XPath 引擎,但是不含 SAX 与 DOM 接口。

创建文档

本节讨论使用 dom4j API 创建 XML 文档的过程,并创建示例 XML 文档 catalog.xml。
使用 import 语句导入 dom4j API 类:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API 工厂类。
Document document = DocumentHelper.createDocument();
使用 addElement() 方法创建根元素 catalog 。 addElement() 用于向 XML 文档中增加元素。
Element catalogElement = document.addElement("catalog");
在 catalog 元素中使用 addComment() 方法添加注释“An XML catalog”。
catalogElement.addComment("An XML catalog");
在 catalog 元素中使用 addProcessingInstruction() 方法增加一个处理指令。
catalogElement.addProcessingInstruction("target","text");
在 catalog 元素中使用 addElement() 方法增加 journal 元素。
Element journalElement = catalogElement.addElement("journal");
使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 属性。
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
向 article 元素中添加 journal 元素。
Element articleElement=journalElement.addElement("article");
为 article 元素增加 level 和 date 属性。
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
向 article 元素中增加 title 元素。
Element titleElement=articleElement.addElement("title");
使用 setText() 方法设置 article 元素的文本。
titleElement.setText("Java configuration with XML Schema");
在 article 元素中增加 author 元素。
Element authorElement=articleElement.addElement("author");
在 author 元素中增加 firstname 元素并设置该元素的文本。
Element firstNameElement=authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
在 author 元素中增加 lastname 元素并设置该元素的文本。
Element lastNameElement=authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
可以使用 addDocType() 方法添加文档类型说明。
document.addDocType("catalog", null,"file://c:/Dtds/catalog.dtd");
这样就向 XML 文档中增加文档类型说明:
<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd">
如果文档要使用文档类型定义(DTD)文档验证则必须有 Doctype。
XML 声明 <?xml version="1.0" encoding="UTF-8"?> 自动添加到 XML 文档中。
清单 3 所示的例子程序 XmlDom4J.java 用于创建 XML 文档 catalog.xml。
清单 3. 生成 XML 文档 catalog.xml 的程序(XmlDom4J.java)

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
public class XmlDom4J{
  public void generateDocument(){
    Document document = DocumentHelper.createDocument();
    Element catalogElement = document.addElement("catalog");
    catalogElement.addComment("An XML Catalog");
    catalogElement.addProcessingInstruction("target","text");
    Element journalElement = catalogElement.addElement("journal");
    journalElement.addAttribute("title", "XML Zone");
    journalElement.addAttribute("publisher", "IBM developerWorks");
    Element articleElement=journalElement.addElement("article");
    articleElement.addAttribute("level", "Intermediate");
    articleElement.addAttribute("date", "December-2001");
    Element titleElement=articleElement.addElement("title");
    titleElement.setText("Java configuration with XML Schema");
    Element authorElement=articleElement.addElement("author");
    Element firstNameElement=authorElement.addElement("firstname");
    firstNameElement.setText("Marcello");
    Element lastNameElement=authorElement.addElement("lastname");
    lastNameElement.setText("Vitaletti");
    document.addDocType("catalog",
    null,"file://c:/Dtds/catalog.dtd");
    try{
      XMLWriter output = new XMLWriter(
      new FileWriter( new File("c:/catalog/catalog.xml") ));
      output.write( document );
      output.close();
    }
    catch(IOException e){System.out.println(e.getMessage());}
  }
  public static void main(String[] argv){
    XmlDom4J dom4j=new XmlDom4J();
    dom4j.generateDocument();
  }
}

这一节讨论了创建 XML 文档的过程,下一节将介绍使用 dom4j API 修改这里创建的 XML 文档。

修改文档

这一节说明如何使用 dom4j API 修改示例 XML 文档 catalog.xml。
使用 SAXReader 解析 XML 文档 catalog.xml:

SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);

SAXReader 包含在 org.dom4j.io 包中。
inputXml 是从 c:/catalog/catalog.xml 创建的 java.io.File。使用 XPath 表达式从 article 元素中获得 level 节点列表。如果 level 属性值是“Intermediate”则改为“Introductory”。

List list = document.selectNodes("//article/@level" );
Iterator iter=list.iterator();
while(iter.hasNext()){
  Attribute attribute=(Attribute)iter.next();
  if(attribute.getValue().equals("Intermediate"))
  attribute.setValue("Introductory");
}

获取 article 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本。

list = document.selectNodes("//article" );
iter=list.iterator();
while(iter.hasNext()){
  Element element=(Element)iter.next();
  Iterator iterator=element.elementIterator("title");
  while(iterator.hasNext()){
    Element titleElement=(Element)iterator.next();
    if(titleElement.getText().equals("Java configuration with XML Schema"))
      titleElement.setText("Create flexible and extensible XML schema");
  }
}

通过和 title 元素类似的过程修改 author 元素。
清单 4 所示的示例程序 Dom4JParser.java 用于把 catalog.xml 文档修改成 catalog-modified.xml 文档。
清单 4. 用于修改 catalog.xml 的程序(Dom4Jparser.java)

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JParser{
  public void modifyDocument(File inputXml){
    try{
      SAXReader saxReader = new SAXReader();
      Document document = saxReader.read(inputXml);
      List list = document.selectNodes("//article/@level" );
      Iterator iter=list.iterator();
      while(iter.hasNext()){
        Attribute attribute=(Attribute)iter.next();
        if(attribute.getValue().equals("Intermediate"))
          attribute.setValue("Introductory");
      }
      list = document.selectNodes("//article/@date" );
      iter=list.iterator();
      while(iter.hasNext()){
        Attribute attribute=(Attribute)iter.next();
        if(attribute.getValue().equals("December-2001"))
        attribute.setValue("October-2002");
      }
      list = document.selectNodes("//article" );
      iter=list.iterator();
      while(iter.hasNext()){
        Element element=(Element)iter.next();
        Iterator iterator=element.elementIterator("title");
        while(iterator.hasNext()){
          Element titleElement=(Element)iterator.next();
          if(titleElement.getText().equals("Java configuration with XML Schema"))
            titleElement.setText("Create flexible and extensible XML schema");
        }
      }
      list = document.selectNodes("//article/author" );
      iter=list.iterator();
      while(iter.hasNext()){
        Element element=(Element)iter.next();
        Iterator iterator=element.elementIterator("firstname");
        while(iterator.hasNext()){
          Element firstNameElement=(Element)iterator.next();
          if(firstNameElement.getText().equals("Marcello"))
            firstNameElement.setText("Ayesha");
        }
      }
      list = document.selectNodes("//article/author" );
      iter=list.iterator();
      while(iter.hasNext()){
        Element element=(Element)iter.next();
        Iterator iterator=element.elementIterator("lastname");
        while(iterator.hasNext()){
          Element lastNameElement=(Element)iterator.next();
          if(lastNameElement.getText().equals("Vitaletti"))
          lastNameElement.setText("Malik");
        }
      }
      XMLWriter output = new XMLWriter(
      new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
      output.write( document );
      output.close();
    }
    catch(DocumentException e)
    {
      System.out.println(e.getMessage());
    }
    catch(IOException e){
      System.out.println(e.getMessage());
    }
  }
  public static void main(String[] argv){
    Dom4JParser dom4jParser=new Dom4JParser();
    dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
  }
}

这一节说明了如何使用 dom4j 中的解析器修改示例 XML 文档。这个解析器不使用 DTD 或者模式验证 XML 文档。如果 XML 文档需要验证,可以解释用 dom4j 与 JAXP SAX 解析器。

关于Linux下C/C++程序编译

在编译之前我们需要在系统里安装G++ GCC,它们就是Linux下的C++/C的编译器。代码如下
代码:

sudo apt-get install build-essential

好,现在我们在文本编辑器里写一个C的简单的程序(好像所有学习C或者C++的书都会出现)
代码:

#include <stdio.h>
int main()
{
printf("Hello,World!n");
return 0;
}

现在存盘为Hello.c,打开你的终端,并在文件当前目录输入:
代码:

gcc Hello.c -o hello   

编译时可能会出现如下警告:no newline at and of file ,只有在文件结尾添加一个新行就好了。
然后在终端中输入 ./hello ,你就能在终端中看到程序运行结果了。

下面来说下C++是如何编译的
写程序(不用我多说了吧)
代码:

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello,World!n"<<endl;
return 0;
}

存盘为Hello.cpp
使用gcc编译??? 不对,这里我们使用g++来编译C++程序
代码:

g++ Hello.cpp -o hello

编译多个文件我们怎么办??? 来看下面出了三个文件Hello.h, Hello.cpp, MyFirst.cpp
代码:

//file_NO1:Hello.h
class Hello {
Hello();
void Display();
}
//file_NO2:Hello.cpp
#include <iostream>
#include "Hello.h"
using namespace std;
Hello::Hello()
{
}
Hello::Display()
{
cout<<"Hello,World!n"<<endl;
}
//file_NO3:MyFirst.cpp
#include <iostram>
#include "Hello.cpp"
int main()
{
Hello theHello;
theHello->Display();
return 0;
}

在g++中有一个参数-c 可以只编译不连接,那么我们就可以按如下顺序编译文件,
代码:

g++ -c Hello.cpp -o Hello.o
g++ -c MyFirst.cpp -o MyFirst.o
g++ MyFirst.o hello.o -o MyFirst

你是否会问,如果是一个项目的话,可能会有上百个文件,这样的编译法,人不是要累死在电脑前吗,或者等到你编译成功了,岂不是头发都白了,呵呵,所以我们要把上述的编译过程写进以下一个文本文件中:
Linux下称之为makefile
[code]
#这里可以写一些文件的说明
MyFirst: MyFirst.o hello.o
g++ MyFirst.o hello.o -o MyFirst
Hello.o:Hello.cpp
g++ -c Hello.cpp -o Hello.o
MyFirst.o:MyFirst.cpp
g++ -c MyFirst.cpp -o MyFirst.o
[code]
存盘为MyFirst,在终端输入:make MyFist ,程序出现了错误可是所有程序员共同的敌人,在编写程序时我们应该尽量的去避免错误 的出现,不过编写的时候再怎么都不可避免的出现这样那样的错误,对程序进行必要的调试是一个好主意,那我们怎么来调试程序呢,看下面:
[code]
gdb ./文件名
[/code]
以下为调试状态下的可以用到的命令(可以仅输入单词的输入,如break可简为b),尖括号中为说明
[code]
list <显示源代码>
break 行号 <设置断点>
run <运行程序>
continue <继续从断点处执行>
print 变量 <调试时查看变量的值>
del 行号 <删除断点>
step <单步执行,可跟踪到函数内部>
next <单步执行,不可跟踪到函数内部>
quit <退出>
[/code]

简单常识――关于stream

从文件中读入一行

简单,这样就行了:

ifstream ifs("input.txt");
char buf[1000];

ifs.getline(buf, sizeof buf);

string input(buf);

当然,这样没有错,但是包含不必要的繁琐和拷贝,况且,如果一行超过1000个字符,就必须用一个循环和更麻烦的缓冲管理。下面这样岂不是更简单?

string input;
input.reserve(1000);
ifstream ifs("input.txt");
getline(ifs, input);

不仅简单,而且安全,因为全局函数 getline 会帮你处理缓冲区用完之类的麻烦,如果你不希望空间分配发生的太频繁,只需要多 reserve 一点空间。

这就是“简单常识”的含义,很多东西已经在那里,只是我一直没去用。

—————————————————————————

一次把整个文件读入一个 string

我希望你的答案不要是这样:

string input;
while( !ifs.eof() )
{
    string line;
    getline(ifs, line);
    input.append(line).append(1, ‘n’);
}

当然了,没有错,它能工作,但是下面的办法是不是更加符合 C++ 的精神呢?

string input(
    istreambuf_iterator<char>(instream.rdbuf()),
    istreambuf_iterator<char>()
);

同样,事先分配空间对于性能可能有潜在的好处:

string input;
input.reserve(10000);
input.assign(
    istreambuf_iterator<char>(ifs.rdbuf()),
    istreambuf_iterator<char>()
);

很简单,不是么?但是这些却是我们经常忽略的事实。
补充一下,这样干是有问题的:

    string input;

    input.assign(

        istream_iterator<char>(ifs),

        istream_iterator<char>()

    );

因为它会忽略所有的分隔符,你会得到一个纯“字符”的字符串。最后,如果你只是想把一个文件的内容读到另一个流,那没有比这更快的了:

    fstream fs("temp.txt");

    cout << fs.rdbuf();

因此,如果你要手工 copy 文件,这是最好的(如果不用操作系统的 API):

   ifstream ifs("in.txt");

   ofstream ofs("out.txt");

   ofs << in.rdbuf();

————————————————————————-

open 一个文件的那些选项

ios::in     Open file for reading
ios::out    Open file for writing
ios::ate    Initial position: end of file
ios::app    Every output is appended at the end of file
ios::trunc  If the file already existed it is erased
ios::binary Binary mode

————————————————————————-

还有 ios 的那些 flag

flag effect if set
ios_base::boolalpha input/output bool objects as alphabetic names (true, false).
ios_base::dec input/output integer in decimal base format.
ios_base::fixed output floating point values in fixed-point notation.
ios_base::hex input/output integer in hexadecimal base format.
ios_base::internal the output is filled at an internal point enlarging the output up to the field width.
ios_base::left the output is filled at the end enlarging the output up to the field width.
ios_base::oct input/output integer in octal base format.
ios_base::right the output is filled at the beginning enlarging the output up to the field width.
ios_base::scientific output floating-point values in scientific notation.
ios_base::showbase output integer values preceded by the numeric base.
ios_base::showpoint output floating-point values including always the decimal point.
ios_base::showpos output non-negative numeric preceded by a plus sign (+).
ios_base::skipws skip leading whitespaces on certain input operations.
ios_base::unitbuf flush output after each inserting operation.
ios_base::uppercase output uppercase letters replacing certain lowercase letters.

There are also defined three other constants that can be used as masks:

constant value
ios_base::adjustfield left | right | internal
ios_base::basefield dec | oct | hex
ios_base::floatfield scientific | fixed

————————————————————————–

用我想要的分隔符来解析一个字符串,以及从流中读取数据

这曾经是一个需要不少麻烦的话题,由于其常用而显得尤其麻烦,但是其实 getline 可以做得不错:

    getline(cin, s, ‘;’);   

    while ( s != "quit" )

    {

        cout << s << endl;

        getline(cin, s, ‘;’);

    }

简单吧?不过注意,由于这个时候 getline 只把 ; 作为分隔符,所以你需要用 ;quit; 来结束输入,否则 getline 会把前后的空格和回车都读入 s ,当然,这个问题可以在代码里面解决。

同样,对于简单的字符串解析,我们是不大需要动用什么 Tokenizer 之类的东西了:

#include <iostream>

#include <sstream>

#include <string>



using namespace std;



int main()

{

    string s("hello,world, this is a sentence; and a word, end.");

    stringstream ss(s);

   

    for ( ; ; )

    {

        string token;

        getline(ss, token, ‘,’);

        if ( ss.fail() ) break;

       

        cout << token << endl;

    }

}

输出:

hello

world

 this is a sentence; and a word

 end.

很漂亮不是么?不过这么干的缺陷在于,只有一个字符可以作为分隔符。

————————————————————————–

把原本输出到屏幕的东西输出到文件,不用到处去把 cout 改成 fs

#include <iostream>
#include <fstream>
using namespace std;
int main()
{    
    ofstream outf("out.txt"); 
    streambuf *strm_buf=cout.rdbuf();    
    cout.rdbuf(outf.rdbuf()); 
    cout<<"write something to file"<<endl; 
    cout.rdbuf(strm_buf);   //recover 
    cout<<"display something on screen"<<endl;
    system("PAUSE");
    return 0;
}
 

输出到屏幕的是:

display something on screen

输出到文件的是:

write something to file

也就是说,只要改变 ostream 的 rdbuf ,就可以重定向了,但是这招对 fstream 和 stringstream 都没用。

————————————————————————–

关于 istream_iterator 和 ostream_iterator

经典的 ostream_iterator 例子,就是用 copy 来输出:

#include <iostream>

#include <fstream>

#include <sstream>

#include <algorithm>

#include <vector>

#include <iterator>



using namespace std;



int main()

{  

    vector<int> vect;

    for ( int i = 1; i <= 9; ++i )

        vect.push_back(i);

       

    copy(vect.begin(), vect.end(),

        ostream_iterator<int>(cout, " ")

    );

    cout << endl;

   

    ostream_iterator<double> os_iter(cout, " ~ ");

    *os_iter = 1.0;

    os_iter++;

    *os_iter = 2.0;

    *os_iter = 3.0;

}

输出:

1 2 3 4 5 6 7 8 9

1 ~ 2 ~ 3 ~

很明显,ostream_iterator 的作用就是允许对 stream 做 iterator 的操作,从而让算法可以施加于 stream 之上,这也是 STL 的精华。与前面的“读取文件”相结合,我们得到了显示一个文件最方便的办法:

    copy(istreambuf_iterator<char>(ifs.rdbuf()),

         istreambuf_iterator<char>(),

         ostreambuf_iterator<char>(cout)

    );

同样,如果你用下面的语句,得到的会是没有分隔符的输出:

    copy(istream_iterator<char>(ifs),

         istream_iterator<char>(),

         ostream_iterator<char>(cout)

    );

那多半不是你要的结果。如果你硬是想用 istream_iterator 而不是 istreambuf_iterator 呢?还是有办法:

    copy(istream_iterator<char>(ifs >> noskipws),

         istream_iterator<char>(),

         ostream_iterator<char>(cout)

    );

但是这样不是推荐方法,它的效率比第一种低不少。
如果一个文件 temp.txt 的内容是下面这样,那么我的这个从文件中把数据读入 vector 的方法应该会让你印象深刻。

12345 234 567
89    10

程序:

#include <iostream>

#include <fstream>

#include <algorithm>

#include <vector>

#include <iterator>



using namespace std;



int main()

{  

    ifstream ifs("temp.txt");

   

    vector<int> vect;

    vect.assign(istream_iterator<int>(ifs),
        istream_iterator<int>()
    );




    copy(vect.begin(), vect.end(), ostream_iterator<int>(cout, " "));

}

输出:

12345 234 567 89 10

很酷不是么?判断文件结束、移动文件指针之类的苦工都有 istream_iterator 代劳了。

———————————————————————–

其它算法配合 iterator

计算文件行数:

    int line_count =

        count(istreambuf_iterator<char>(ifs.rdbuf()),

              istreambuf_iterator<char>(),

              ‘n’);       

当然确切地说,这是在计算文件中回车符的数量,同理,你也可以计算文件中任何字符的数量,或者某个 token 的数量:

    int token_count =

        count(istream_iterator<string>(ifs),

              istream_iterator<string>(),

              "#include");       

注意上面计算的是 “#include” 作为一个 token 的数量,如果它和其他的字符连起来,是不算数的。

————————————————————————
Manipulator

Manipulator 是什么?简单的说,就是一个接受一个 stream 作为参数,并且返回一个 stream 的函数,比如上面的 unskipws ,它的定义是这样的:

  inline ios_base&

  noskipws(ios_base& __base)

  {

    __base.unsetf(ios_base::skipws);

    return __base;

  }

这里它用了更通用的 ios_base 。知道了这一点,你大概不会对自己写一个 manipulator 有什么恐惧感了,下面这个无聊的 manipulator 会忽略 stream 遇到第一个分号之前所有的输入(包括那个分号):

template <class charT, class traits>
inline std::basic_istream<charT, traits>&
ignoreToSemicolon (std::basic_istream<charT, traits>& s)
{
    s.ignore(std::numeric_limits<int>::max(), s.widen(‘;’));
    return s;
}

不过注意,它不会忽略以后的分号,因为 ignore 只执行了一次。更通用一点,manipulator 也可以接受参数的,下面这个就是 ignoreToSemicolon 的通用版本,它接受一个参数, stream 会忽略遇到第一个该参数之前的所有输入,写起来稍微麻烦一点:

struct IgnoreTo {
    char ignoreTo;
    IgnoreTo(char c) : ignoreTo(c)
    {}
};
   
std::istream& operator >> (std::istream& s, const IgnoreTo& manip)
{
    s.ignore(std::numeric_limits<int>::max(), s.widen(manip.ignoreTo));
    return s;
}

但是用法差不多:

    copy(istream_iterator<char>(ifs >> noskipws >> IgnoreTo(‘;’)),

         istream_iterator<char>(),

         ostream_iterator<char>(cout)

    );

其效果跟 IgnoreToSemicolon 一样。