一个用Jdom输出XML的java程序
2008-03-27 15:05
393 查看
通过java程序组织数据输出XML的方法有很多种,用JDOM的组件包构建XML便是其中比较方便的一种,以下通过一个例子来说明。
首先,从这里下载jdom的包,并将/build/jdom.jar导入eclipse。
其次,新建一个类。具体代码参考下面。通过下面的代码就可以输出XML代码了。
jdom的使用方法,请参考这里。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package xmlcreate;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.input.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.output.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.PrintWriter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServlet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Xmlcreate extends HttpServlet...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintWriter out = response.getWriter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//下面这句话必备,否则输出的XML不能被AJAX的对象XMLHttpResquest的方法responseXML()所识别,只能在responseText里输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
response.setContentType("text/xml;charset=UTF-8");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String keyword = Escape.unescape(request.getParameter("keyword"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] key = ...{"111","112","113","132","131","232","321","211","258"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] content = ...{"aaa","bbbbbbbbbbbbbbbbaa","ccc","ddd","中文","测试","中国人","测试成员","一个好长好长的试一下啊"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Xmlcreate s1 = new Xmlcreate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s1.BuildXMLDoc(key,content,keyword,out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void BuildXMLDoc(String[] key,String[] content,String keyword,PrintWriter out)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws IOException, JDOMException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element resultRoot = new Element("result");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Document Doc = new Document(resultRoot);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i =0;i<key.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int keyindex = key[i].indexOf(keyword);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int contentindex = content[i].indexOf(keyword);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(keyindex == 0 || contentindex == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element rsNode = new Element("rs");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resultRoot.addContent(rsNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element keyNode = new Element("key");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
keyNode.addContent(Escape.escape(key[i]));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rsNode.addContent(keyNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element contentNode = new Element("content");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
contentNode.addContent(Escape.escape(content[i]));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rsNode.addContent(contentNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Format format = Format.getPrettyFormat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//format.setEncoding("GB2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//XMLOutputter XMLOut = new XMLOutputter(format);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLOutputter XMLOut = new XMLOutputter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLOut.output(Doc, out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
首先,从这里下载jdom的包,并将/build/jdom.jar导入eclipse。
其次,新建一个类。具体代码参考下面。通过下面的代码就可以输出XML代码了。
jdom的使用方法,请参考这里。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package xmlcreate;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.input.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.jdom.output.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.PrintWriter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServlet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Xmlcreate extends HttpServlet...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintWriter out = response.getWriter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//下面这句话必备,否则输出的XML不能被AJAX的对象XMLHttpResquest的方法responseXML()所识别,只能在responseText里输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
response.setContentType("text/xml;charset=UTF-8");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String keyword = Escape.unescape(request.getParameter("keyword"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] key = ...{"111","112","113","132","131","232","321","211","258"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] content = ...{"aaa","bbbbbbbbbbbbbbbbaa","ccc","ddd","中文","测试","中国人","测试成员","一个好长好长的试一下啊"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Xmlcreate s1 = new Xmlcreate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s1.BuildXMLDoc(key,content,keyword,out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void BuildXMLDoc(String[] key,String[] content,String keyword,PrintWriter out)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws IOException, JDOMException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element resultRoot = new Element("result");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Document Doc = new Document(resultRoot);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i =0;i<key.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int keyindex = key[i].indexOf(keyword);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int contentindex = content[i].indexOf(keyword);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(keyindex == 0 || contentindex == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element rsNode = new Element("rs");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resultRoot.addContent(rsNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element keyNode = new Element("key");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
keyNode.addContent(Escape.escape(key[i]));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rsNode.addContent(keyNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Element contentNode = new Element("content");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
contentNode.addContent(Escape.escape(content[i]));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rsNode.addContent(contentNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Format format = Format.getPrettyFormat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//format.setEncoding("GB2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//XMLOutputter XMLOut = new XMLOutputter(format);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLOutputter XMLOut = new XMLOutputter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLOut.output(Doc, out);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 2013-03-07:编写一个JAVA程序,用户用键盘只能输入整数,程序输出这些整数的乘积
- java程序题:将一个数组逆序输出
- 写一个Java输出网址的程序
- 今天开始学Java 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。
- 【practice】编写一个 JAVA 程序,实现输出考试成绩的前三名
- 一个可以完成读取、打印输出、保存xml等等功能的java例子
- java读取数据库字段和值,输出到regedit,xml,text,excel,pdf的程序
- 一个程序互交式逆向输出文字的JAVA算法
- [Java] JDOM 读取 xml 文件 示例程序初步
- Java程序运行结果输出到一个文件中去,结果显示乱码
- 编写一个JAVA应用程序,用户从键盘只能输入整数,程序输出这些整数的乘积
- java程序,编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数及非英文字母数
- 第一个java小程序 applet 画一个圆 获取网页自定义的参数来输出
- [Java] JDOM 读取 xml 文件 示例程序初步
- Java:编写一个程序输出斐波那契数列中的前20个数
- 1、编写一个Java程序在屏幕上输出“你好!”。
- 5.编写一个程序输出从2开始的素数,素数个数由用户指定,要求10个一行。java
- 写一个程序三个线程分别输出A,B,C, 顺序输出ABC十次,用java实现
- 网易2015编程题(一用Java实现一个多线程程序将这个10000个数输出到5个不用文件中)
- 编写一个 JAVA 程序,实现输出考试成绩的前三名