急求应用 jsp 修改 xml 实例!!!!!!!

flyshp 2003-11-21 11:44:16
由于项目需要 我急需应用jsp 对 xml 内容进行修改的实例!!

希望有这方面经验的人给予帮助 !!

我想用 jsp 调用 ejb 来实现 对xml 的修改 ,网上说在 orb.xml.dom 类包理由封装好的函数 ,具体那相应用请给予提示!!!!!!!! 谢谢
...全文
107 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
MyObject 2004-01-05
  • 打赏
  • 举报
回复
正好是我需要的,谢谢 shaokun305(混口饭吃!)
不过,Jdom 写出来的XML文件,怎么没有换行呢?
onefox 2003-12-14
  • 打赏
  • 举报
回复
mark
flyshp 2003-11-24
  • 打赏
  • 举报
回复
我调试过了 ! 你的应用程序可以运行的!

但是 我想知道怎么用jsp 直接对xml 进行修改! 而且你的方法没有修改数据的功能! 除非先 del 然后再 add 那样做可太费事了!!

另外 应用程序不同于web端程序 , 我们对安全级别要求的比较高!! 你在客户端 可以随便操作xml 的配置文件么????

希望你帮我看看.......!!
为了表示感谢 上面的分都给你吧! ^_^
shaokun305 2003-11-21
  • 打赏
  • 举报
回复
先下载jdom包。加入环境变量:
然后看程序:
package jdomstury;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: xxxxxxxxxxxxxxxxxxx</p>
* @author csk
* @version 1.0
*/

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.*;
import java.math.*;

public class test {
public void addNewBook(Element root,String name,String author,String date,String price)
{
try {
Element newbook=new Element("book");
newbook.addContent(new Element("name"));
newbook.addContent(new Element("author"));
newbook.addContent(new Element("publishDate"));
newbook.addContent(new Element("price"));
// List ls=newbook.getChildren();
Element name1=(Element)newbook.getChild("name");
// Element name1=e.getChild("name");
name1.setText(name);
Element author1=newbook.getChild("author");
author1.setText(author);
Element publishDate1=newbook.getChild("publishDate");
publishDate1.setText(date);
Element price1=newbook.getChild("price");
price1.setText(price);
root.addContent(newbook);
}
catch (Exception ex) {
ex.printStackTrace();
}

}
public void delBook(Element root,String name)
{

List booklist=root.getChildren();
System.out.println(booklist.size());
for(int i=0;i<booklist.size();i++)
{
System.out.println("boolist["+i+"]");
Element delbook=(Element)booklist.get(i);
String delbookname=null;
delbookname=delbook.getChild("name").getText().trim();
System.out.println("bookname="+delbookname);
if(delbookname.equals(name))
System.out.println(root.removeContent(delbook));
}
}
public ArrayList searchBook(Element root,String bookname)
{
List list=root.getChildren();
ArrayList booklist=new ArrayList();
Iterator it= list.iterator();
Element book=null;
while(it.hasNext())
{
book=(Element)it.next();
if(book.getChildText("name").equals(bookname))
booklist.add(book);
}
return booklist;
}
public test() {
}
public static void main(String[] args) {
test test1 = new test();
try {
SAXBuilder sb = new SAXBuilder();

//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("F:\\exampleA.xml"));

//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction
("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
doc.addContent(pi);
Element root=doc.getRootElement();
test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
// test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
// test1.addNewBook(root,"java jdom","csk","2003-9-22","20.33");
test1.delBook(root,"java jdom");
ArrayList booklist=test1.searchBook(root,"Java编程入门");
for(int i=0;i<booklist.size();i++)
{
Element searchbook=(Element)booklist.get(i);
if(searchbook!=null)
{
System.out.println("name="+searchbook.getChildText("name"));
System.out.println("author="+searchbook.getChildText("author"));
System.out.println("publishDate="+searchbook.getChildText("publishDate"));
System.out.println("price="+searchbook.getChildText("price"));

}
}
String indent = " ";
boolean newLines = false;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
outp.output(doc, new FileOutputStream("F:\\exampleB.xml"));
}
catch (JDOMException ex) {
}catch (IOException ex) {
}
}
}

再把那个例子的exampleA.xml给你:
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book>
<name>Java编程入门</name>
<author>张三</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book>
<book>
<name>XML在Java中的应用</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
<book>
<name>Java编程入门</name>
<author>张三</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>

</book>

</bookList>

好了,运行你的程序吧。注意:1.包的位置,2.你的目标文件xampleA.xml的路径。
flyshp 2003-11-21
  • 打赏
  • 举报
回复
ok !!

我调试一下! 再一次感谢你的帮助!

问题解决马上给分

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧