跳到导航
BEA Dev2Dev Oracle and BEA
首页 资源中心 dev2dev学堂 在线技术论坛 User Group CodeShare
dev2dev 首页 > 资源中心 > 技术文章
在Apache Xindice中保存XML文档

时间:2006-05-26
作者:Deepak Vohra
浏览次数:
本文关键字:apacheXindicexmlxpathXML:DB
文章工具
推荐给朋友 推荐给朋友
打印文章 打印文章

  Apache Xindice是一种本机XML数据库,可以在该数据库中保存、查询和修改XML文档。与关系数据库相比,本机数据库的优点是无须将XML映射到SQL。该数据库使用XPath查询Xindice数据库,使用XML:DB XUpdate来更新数据库。Xindice实现了Java XML:DB API,用于在Xindice数据库中添加、查询和更新XML文档。Xindice数据库中的XML文档保存在集合中;一个集合可以包含一个或多个XML文档。Xindice还提供一种命令行工具,该工具具有与XML:DB API相同的功能。

初始安装

  Xindice数据库可作为一种Web应用程序安装在Web服务器上。安装Xindice数据库时需要Xindice API JAR文件和Xindice服务器Web应用程序。

  • 下载Version 1.1b4 binary (JAR),即xml-xindice-1.1b4-jar.zip,其中包含Xindice XML:DB API JAR文件。
  • 下载Version 1.1b4 binary (webapp),即xml-xindice-1.1b4-war.zip,其中包含Xindice服务器Web应用程序。
  • 将xml-xindice-1.1b4-jar.zip解压缩到安装目录C:/xindice下。
  • 将xerces-2.6.0.jar添加到CLASSPATH变量前面。然后将下面的Xindice JAR文件添加到CLASSPATH,其中,<Xindice>是Xindice安装目录。
Xindice JAR文件 描述
<Xindice>/xindice-1.1b4/lib/xerces-2.6.0.jar Xerces XML解析器
<Xindice>/xindice-1.1b4/xindice-1.1b4.jar 内核服务器API
<Xindice>/xindice-1.1b4/lib/commons-logging-1.0.3.jar Jakarta Commons Logging API
<Xindice>/xindice-1.1b4/lib/xalan-2.5.2.jar XPath API
  • <Xindice>/xindice-1.1b4/lib/xmldb-api-20030701.jar
  • <Xindice>/xindice-1.1b4/lib/xmldb-api-sdk-20030701.jar
  • <Xindice>/xindice-1.1b4/lib/xmldb-common-20030701.jar
  • <Xindice>/xindice-1.1b4/lib/xmldb-xupdate-20040205.jar
XML:DB API 和XUpdate API的实现
<Xindice>/xindice-1.1b4/lib/xmlrpc-1.1.jar XML-RPC API
<Xindice>/xindice-1.1b4/lib/xml-apis.jar DOM API

表1 Xindice JAR文件

  • 将<Xindice>/xindice-1.1b4/bin添加到PATH变量。
  • 为XINDICE_HOME环境变量添加值<Xindice>/xindice-1.1b4。
  • 将xml-xindice-1.1b4-war.zip解压缩到安装目录C:/xindice。
  • Xindice Web应用程序部署在Web服务器/应用服务器中(本教程使用了WebLogic server 9.0,但也可以使用其他Web服务器/应用服务器)。安装WebLogic Server 9.0。然后使用WebLogic server配置向导创建一个WebLogic server域。创建的默认域为base_domain。

概述

  可以使用XML:DB和XUpdate API或Xindice命令行工具访问和修改Xindice数据库。XML:DB API包括使用XPath查询数据库的XpathQueryService服务以及使用XUpdate更新数据库的XUpdateQueryService服务。XML:DB/XUpdate API和命令行工具用于创建Xindice数据库集合、将XML文档添加到数据库、从数据库检索XML文档、查询数据库和更新数据库。在下面的各小节中,我们将先演示命令行工具,然后演示Xindice API。

   使用xindice命令访问Xindice命令行工具。xindice命令的语法是:

xindice action [switch] [parameter]

  表2列出了一些xindice命令操作值。

Xindice操作 描述
ac 添加一个集合
dc 删除一个集合
ad 添加一个文档
dd 删除一个文档
lc 列出集合
rd 检索一个文档
ld 列出集合中的文档
xpath 使用XPath查询文档
xupdate 使用XUpdate更新文档

表 2. Xindice命令操作值

  表3列出了一些xindice命令切换值。.

Xindice切换 描述
-c 指定一个集合上下文。上下文语法的形式是 xmldb:xindice://host:port/db
-f 指定一个文件路径
-n 指定一个名称
-q 指定一个XPath查询

表 3. Xindice命令切换值

  以下部分参考表2和表3,说明了xindice命令中使用的Xindice操作或切换。作为xindice命令的一个示例,使用以下命令列出了Xindice数据库中的集合:

xindice lc -c xmldb:xindice://localhost:7001/db

  该xindice命令的输出结果是:

system
meta
Total collections: 2

使用WebLogic Server配置Xindice

  需要使用Xerces解析器来配置Xindice 1.1.b4数据库。默认情况下,Xindice使用在JVM中配置的XML解析器。因为JDK 1.4.2中包括的XML解析器是Crimson解析器,将Xindice 1.1b4与JDK 1.4.2一起使用将发生以下错误:

Failed to create SAXParser

org.xml.sax.SAXNotSupportedException: 

Feature: http://xml.org/sax/features/

external-general-entities

  将JDK 5.0与Xindice 1.1b4一起使用。JDK 5.0包括Xerces解析器。WebLogic Server 9.0用于部署Xindice数据库。需要使用WebLogic 9.0(而不是8.1)版,因为9.0版本支持JDK 5.0。

   JDK 1.4.2可以与Xindice一起使用,方法是通过认可的标准覆盖机制将Xerces2-j 2.7.1中的Xerces实现类添加到JDK 1.4.2中Xerces类前面的类路径。要使用认可的标准覆盖机制,请使用安装Xerces2-j(如<xerces-2_7_1>)的目录定义系统属性java.endorsed.dirs,或将JAR文件从xerces-2_7_1目录复制到<JDK1.4.2>/jre/lib/endorsed目录。

   本节中的配置过程仅适用于WebLogic Server 9.0:

  • 将<Xindice>/Xindice-webapp/xindice-1.1b4/xindice-1.1b4.jar修改为xindice.jar。
  • 从xindice.jar文件中解压缩WEB-INF/server.xml配置文件。
  • 在server.xml文件中,将root集合元素中的dbroot属性值指定为C:/xindice/db,该目录路径是用于创建Xindice数据库的目录路径。
  • 从xindice.jar文件删除WEB-INF/system.xml。将修改后的WEB-INF/server.xml 添加到xindice.jar。
  • 将xindice.jar文件复制到<weblogic>/user_projects/domains/base_domain/autodeploy目录。
  • 将JAVA_HOME环境变量定义为<weblogic>/jdk150_03,其中<weblogic>为安装WebLogic server的目录。
  • 启动WebLogic server。这将启动WebLogic server中的Xindice数据库服务器,并打开默认的Xindice数据库db。在WebLogic server中部署Xindice服务器Web应用程序。使用URL http://localhost:7001/console访问WebLogic server管理控制台。选择base_domain>Deployments节点。选择Xindice Web应用程序链接和Testing选项卡。Xindice服务器URL显示为http://localhost:/7001/xindice。

在数据库中创建集合

  在WebLogic server中配置了Xindice数据库服务器后,我们将使用命令行工具和XML:DB API创建一个Xindice数据库集合。在Xindice数据库中,集合是一组XML文档。

   使用Xindice命令创建顶级集合catalog:

>xindice ac -c xmldb:xindice://localhost:7001/db -n catalog

  示例命令行命令中的集合上下文是db数据库实例,它是默认的Xindice数据库实例。还可以将db数据库作为根集合引用。这会在db数据库中创建集合catalog,并输出以下消息:

Created :xmldb:xindice://localhost:7001/db/catalog

  可以使用以下命令删除创建的集合:

>xindice dc -c xmldb:xindice://localhost:7001/db -n catalog

  这将删除集合,并显示如下消息:

Deleted: xmldb:xindice://localhost:7001/db/catalog

  接下来,我们将使用XML:DB API创建一个集合,导入Xindice核心服务器类和XML:DB API类。

import org.apache.xindice.client.xmldb.services.*;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.xml.dom.*;
import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;

  创建一个Xindice数据库引擎实例。Xindice数据库的XML:DB驱动程序实现类为DatabaseImpl。向org.xmldb.api.DatabaseManager注册Xindice数据库,使用该数据库从XML:DB数据库获取集合。

String xindiceDriver = 
  "org.apache.xindice.client.xmldb.DatabaseImpl";
org.xmldb.api.base.Database xindiceDatabase = 
  (org.xmldb.api.base.Database)
  ((Class.forName(xindiceDriver)).newInstance());
org.xmldb.api.DatabaseManager.
   registerDatabase(xindiceDatabase);

  接下来,从该数据库获取db集合。

 String url = "xmldb:xindice://localhost:7001/db";
 org.xmldb.api.base.Collection  collection = 
    DatabaseManager.getCollection(url);

  创建org.apache.xindice.client.xmldb.services.CollectionManager对象。要从数据库创建并删除集合,CollectionManager是必需的。指定一个XML配置以创建集合。在Xindice文档中没有详细介绍XML配置。请使用CollectionManager服务创建一个数据库集合。

CollectionManager collectionManagerService = 
   (CollectionManager) 
   collection.getService("CollectionManager", "1.0");

String collectionName = "catalog";
String collectionConfig = "<collection compressed=\"true\" " +
  "   name=\"" + collectionName + "\">" +
  " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\"/>" +
  "</collection>";

catalogCollection = 
  collectionManagerService.createCollection
  (collectionName, 
  DOMParser.toDocument(collectionConfig));

  这将在db数据库中创建一个集合catalog。

将XML文档添加到数据库

  接下来,将XML示例文档添加到数据库集合catalog。XML示例文档如下所示:

<catalog title="OnJava.com" publisher="OReilly"> 
 <journal date="Sept 2005"> 
  <article>
   <title>What Is a Portlet</title>
   <author> Sunil Patil</author>
  </article>
 </journal>
 <journal date="Sept 2005">
  <article> 
   <title>What Is Hibernate</title>
   <author>James Elliott</author>
  </article>
 </journal>
 <journal date="Oct 2003">
  <article> 
   <title>BCEL Maven and CSS with Swing</title>
   <author>Daniel Steinberg</author>
  </article>
 </journal>
</catalog>

  使用命令行工具通过以下命令将XML文档添加到Xindice数据库:

>xindice ad -c xmldb:xindice://localhost:7001/db/catalog
 -f c:/xindice/catalog.xml -n catalog.xml

  这将catalog.xml文档添加到catalog集合,并输出以下确认:

Added document
xmldb:xindice://localhost:7001/db/catalog/catalog.xml

  接下来,我们将使用XML:DB API添加集合。导入前一节列出的XML:DB API和核心服务器类。创建并注册“在数据库中创建集合”一节中的数据库驱动程序。从数据库获取catalog集合:

Collection collection = DatabaseManager.getCollection
  ("xmldb:xindice://localhost:7001/db/catalog");

  获取添加到数据库的XML文档的Document对象。

DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance();
File datafile = new File("c:/Xindice/catalog.xml");
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(datafile);

  创建添加到集合的XML文档资源的ID。ID将集合与标识符关联起来。为从集合添加的XML文档创建org.xmldb.api.modules.XMLResource。在Xindice数据库中,XMLResource对象表示XML资源,如Document对象。从Document对象设置XMLResource的内容。将XML资源添加到集合。

String  resourceID = collection.createId();

XMLResource resource = (XMLResource) 
 (collection.createResource(resourceID,
              "XMLResource"));
resource.setContentAsDOM(document);
collection.storeResource(resource);

  这将XML文档添加到数据库集合。

从数据库检索XML文档

  接下来,检索在上一节中添加的XML文档。使用下列命令从Xindice数据库检索XML文档:

>xindice rd -c xmldb:xindice://localhost:7001/db/catalog
  -n catalog.xml

  这将检索XML文档。接下来,使用XML:DB API检索XML文档。获取“将XML文档添加到数据库”一节中的catalog集合。获取用于XML文档的XML资源。输出XML资源中的XML文档。

XMLResource resource = (XMLResource)
  (collection.getResource(resourceID));
System.out.println(resource.getContent());

  这将检索上一节中添加的XML文档。

使用XPath查询数据库

  Xindice提供XPath查询引擎,以便使用XPath查询数据库的XML文档。接下来,使用XPath查询Xindice数据库中的XML文档。首先,使用命令行工具通过xindice命令xpath操作查询Xindice数据库集合。例如,检索第一个<journal/>元素中<article/>的title。

>xindice xpath -c xmldb:xindice://localhost:7001/db/catalog
   -q /catalog/journal[1]/article/title

  参数-q指定Xpath查询。来自Xpath查询的输出是:

<title src:col="/db/catalog" src:key="catalog.xml"
xmlns:src="http://xml.apache.org/xindice/Query">
What Is a Portlet</title>

  作为另一个示例,检索catalog元素的publisher属性。

>xindice xpath -c xmldb:xindice://localhost:7001/db/catalog
    -q /catalog/@publisher

  来自Xpath查询的输出是:

<xq:result publisher="OReilly"
xmlns:xq="http://xml.apache.org/xindice/Query"
xq:col="/db/catalog" xq:key="catalog.xml" />

  接下来,使用XML:DB API查询数据库。从“将XML文档添加到数据库”一节中的数据库获取catalog集合。接下来,指定Xpath查询字符串。以下XPath表达式将找到第一个journal元素中article的title。

String xpath = "/catalog/journal[1]/article/title";

  使用org.xmldb.api.modules.XpathQueryService服务查询数据库集合。创建一个XpathQueryService对象。使用XPathQueryService query()方法查询数据库。查询返回org.xmldb.api.base.ResourceSet。在资源集上迭代以获取XML文档资源org.xmldb.api.base.Resource。输出资源中的XML文档。

XPathQueryService queryService = 
  (XPathQueryService) 
  collection.getService("XPathQueryService","1.0");
ResourceSet resourceSet = queryService.query(xpath);
ResourceIterator iterator = resourceSet.getIterator();
   while (iterator.hasMoreResources()) {
       Resource resource = iterator.nextResource();
       System.out.println(resource.getContent());
   }

  Xpath查询的输出为:

<title src:col="/db/catalog"
src:key="022705cf47a9e30900000107728ed85e"
 xmlns:src="http://xml.apache.org/xindice/Query">
What Is a Portlet</title>

使用Xupdate修改数据库

  Xindice实现XML:DB XUPdate机制,以更新XML文档。让我们使用Xupdate更新数据库中的XML文档。首先,我们将使用命令行工具。使用Xindice命令xupdate操作更新XML文档。表4列出了一些Xupdate命令,用于更新XML文档。

Xupdate命令 描述
xupdate:insert-after 在选择的节点后添加节点。
xupdate:update 更新选择的节点。
xupdate:remove 移除选择的节点。

表4. Xupdate命令

使用xindice命令添加元素

  例如,我们将更新catalog.xml文件,以便将journal元素添加到XML文档。在xupdate配置文件xupdate.xml中指定了要更新/添加的元素/属性。要添加journal元素的配置文件xupdate.xml如下:

<xupdate:modifications version="1.0"
  xmlns:xupdate="http://www.xmldb.org/xupdate">
 <xupdate:insert-after select="/catalog/journal[3]">
  <xupdate:element name="journal">
  <xupdate:attribute name="date">Aug 2005</xupdate:attribute>
   <article> 
    <title>iBatis DAO</title>
    <author>Sunil Patil</author>
   </article>
  </xupdate:element>
 </xupdate:insert-after>
</xupdate:modifications>

  使用以下命令更新XML文档:

>xindice xupdate -c
  xmldb:xindice://localhost:7001/db/catalog -n catalog.xml
  -f  c:/xindice/xupdate.xml

  xupdate操作指定要更新的XML文档。参数-f指定配置文件xupdate.xml。这将更新XML文档,并显示以下消息:

1 documents updated

使用xindice命令删除和修改元素

  再如,移除journal元素,并修改另一个journal元素中的title。让我们移除第一个journal元素,并修改第三个journal元素中的title。因为在更新第三个journal元素之前,移除了第一个journal元素,所以要更新的journal元素成为第二个journal元素。我们会使用xupdate:remove移除某一元素和xupdate:update,以更新一个元素。下面是用于移除和修改元素的配置文件xupdate.xml:

<xupdate:modifications version="1.0"
  xmlns:xupdate="http://www.xmldb.org/xupdate">
<xupdate:remove select="/catalog/journal[1]"/>
<xupdate:update select="/catalog/journal[2]/article/title">
Maven with Swing</xupdate:update>
</xupdate:modifications>

  使用以下命令更新XML文档:

>xindice xupdate -c
  xmldb:xindice://localhost:7001/db/catalog -n catalog.xml
  -f  c:/xindice/xupdate.xml

  这将更新Xindice数据库中的XML文档。

使用XML:DB API添加元素

  接下来,使用XML:DB API更新XML示例文档。作为一个示例,在第三个journal元素后面添加一个journal元素。从“将XML文档添加到数据库”一节中的数据库获取catalog集合。然后用XUpdate 字符串指定XUpdate命令。

String xupdate = 
"<xupdate:modifications version=\"1.0\"" +
"    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
"    <xupdate:insert-after select=\"/catalog/journal[3]\">" +
"    <journal date=\"Aug 2005\">" + " <article>" +
"    <title>iBatis DAO</title>" +
"    <author>Sunil Patil</author>" + " </article>" +
"    </journal>" + "    </xupdate:insert-after>" +
"</xupdate:modifications>";

  使用org.xmldb.api.modules.XUpdateQueryService通过XUpdate更新数据库。从要更新的集合创建XUpdateQueryService对象。使用XUpdateQueryService对象的update()方法更新数据库,如下所示:

XUpdateQueryService queryService =
(XUpdateQueryService) collection.getService("XUpdateQueryService",
                   "1.0");
queryService.update(xupdate);

使用XML:DB API删除元素

  类似地,可以使用xupdate:remove命令从数据库中的XML文档移除journal元素。为移除第一个journal元素,可以创建一个XUpdate命令String。使用XUpdate查询服务更新XML文档:

xupdate = "<xupdate:modifications version=\"1.0\"" +
    "    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
    "    <xupdate:remove select=\"/catalog/journal[1]\"/>" +
    "</xupdate:modifications>"; queryService.update(xupdate);

使用XML:DB API修改元素

  接下来,使用xupdate:update命令修改元素。修改第二个journal元素中article的title。此外,我们将使用XUpdate查询服务创建一个XUpdate命令String来更新XML文档。

xupdate = "<xupdate:modifications version=\"1.0\"" +
"    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
"    <xupdate:update select=\"/catalog/journal[2]/article/title\"> +
"    Maven with Swing</xupdate:update>" +
"</xupdate:modifications>";
 queryService.update(xupdate);

  在数据库集合catalog中检索修改的XML文档。修改后的XML文档列表如下:

<?xml version="1.0"?>
<catalog publisher="OReilly" title="OnJava.com"> 
 <journal date="Sept 2005">
  <article> 
   <title>What Is Hibernate</title>
   <author>James Elliott</author>
  </article>
 </journal>
 <journal date="Oct 2003">
  <article> 
   <title>Maven with Swing</title>
   <author>Daniel Steinberg</author>
  </article>
 </journal><journal date="Aug 2005">    
  <article>    
   <title>iBatis DAO</title> 
   <author>Sunil Patil</author>   
  </article>    
 </journal>    
</catalog>

删除XML文档

  接下来,从数据库集合删除XML文档。使用命令行工具通过以下命令删除XML文档:

>xindice dd -c
  xmldb:xindice://localhost:7001/db/catalog -n catalog.xml

  这将删除catalog集合中的XML文档catalog.xml,并显示以下消息:

DELETED: xmldb:xindice://localhost:7001/db/catalog/catalog.xml

  接下来,使用XML:DB API删除XML文档。需要从将要删除的XML文档获取catalog集合,并获取要删除的XML资源。删除XML资源的方法如下:

XMLResource resource = (XMLResource)
  (collection.getResource(resourceID));
collection.removeResource(resource);

  这将从数据库删除选择的XML文档。

   用于创建集合、添加XML文档、检索XML文档、查询数据库、更新数据库和删除XML文档的Xindice Java应用程序XIndiceDB.java可从xindice-resources.zip文件中获得。

结束语

  在本教程中,我们使用WebLogic server 9.0配置了Xindice数据库服务器。也可以使用其他应用服务器(如JBoss)配置Xindice。使用JDK 5.0是因为JDK 1.4.2会生成Xindice 1.1b4的异常。使用WebLogic server 9.0是因为9.0版本支持JDK 5.0。

参考资料

原文出处:http://www.onjava.com/pub/a/onjava/2006/03/08/storing-xml-document-with-apache-xindice.html

 作者简介
Deepak Vohra 是一名NuBean顾问兼web开发人员。他拥有Sun Certified Java 1.4 Programmer和Sun Certified Web Component Developer for J2EE证书。
dot dot dot

dot
  作者其它文章
您对本文的评价
您对这篇文章的看法如何?
太棒了!5分 不错啊 4分 一般般 3分 有待提高 2分 不好 1分

   
相关产品
相关技术