日期:2014-05-20  浏览次数:20935 次

怎样搜索用xml描述的资源!!!急用 !望大家解救
我们在每台存放资源的服务器上设置一个xml文件,由于xml文件与语言无关,纯文本,便于统一。我们使用xml文件来描述该服务器上资源的信息

<?xml version="1.0" encoding="UTF-8"?>
<allresource>
  <resourceitem> //一条资源记录
  <title>07各地高考作文题</title> //资源标题
  <keywords>作文,高考</keywords> //资源关键字
  <kind>doc</kind> //资源类型(资源文件的扩展名)
  <describe>07各地高考作文题</describe> //资源具体描述
  <date>2007-12-1 8:50:43</date> //发布日期
  <url>http://192.168.0.122:8080/Query/source/07各地高考作文题.doc</url> //资源保存地址
  <author>李明</author> //资源作者
  <publisher>山东师大</publisher> //资源发布者  
  </resourceitem>
  <resourceitem>
  <title>汽车动画</title>
  <keywords>汽车,动画</keywords>
  <kind>wmv</kind>
  <describe>学生作品</describe>
  <date>2007-12-1 8:50:43</date>
  <url>http://192.168.0.122:8080/Query/source/jc.wmv</url>
  <author>王朋</author>
  <publisher>山东师大</publisher>
</resourceitem>

</allresource>

以上就是该xml文件的一条资源记录。该记录描述的一个资源的详细信息,作为检索关键字的依据。

------解决方案--------------------
我给你一个自己用LOG4J做的
package dom4j;

import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class testCSDN {
public static String DOM_FILE_PATH = new String("testCSDN.xml");
public static void main(String[] args) throws DocumentException {
new testCSDN().parseXML(DOM_FILE_PATH);
}
public void parseXML(String DOM_FILE_PATH) throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read(DOM_FILE_PATH);
Element root = document.getRootElement();

Element foo = null;
Element elm = root.element("resourceitem");
Iterator<Element> it = root.elementIterator("resourceitem");

while (it.hasNext()) {
StringBuffer temp = new StringBuffer();
foo = it.next();
temp.append(foo.elementText("title") + " ");
temp.append(foo.elementText("keywords") + " ");
temp.append(foo.elementText("kind") + " ");
temp.append(foo.elementText("describe") + " ");
temp.append(foo.elementText("date") + " ");
temp.append(foo.elementText("url") + " ");
temp.append(foo.elementText("author") + " ");
temp.append(foo.elementText("publisher"));
System.out.println(temp.toString());
}
}
}
运行结果:
07各地高考作文题 作文,高考 doc 07各地高考作文题 2007-12-1 8:50:43 http://192.168.0.122:8080/Query/source/07各地高考作文题.doc 李明 山东师大 
汽车动画 汽车,动画 wmv 学生作品 2007-12-1 8:50:43 http://192.168.0.122:8080/Query/source/jc.wmv 王朋 山东师大