Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 jdom--强大好用的xml处理工具
qilong2000





发贴: 17
积分: 0
于 2003-05-13 15:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一.简介

JDOM是基于Java2的API,它用Java的数据类型来定义操作数据树的各个节点...呵,就是这样,还是看怎么用它来解析XML文档吧^_^

二.API

JDOM的API还是很简单的,数量也不多,通过看方法的名字就能知道他们的作用。在这里就不罗列了,需要完整最新的API参考,可以到http://www.jdom.org网站查看。

三.解析实例

JDOM提供了很多操作节点非常方便有效的方法,因此能很轻松的读取,修改XML文档。下面这个例子是修改bigmouse的CAD成绩,然后添加一条新学生资料。

---------- MyJDOM.java ----------

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

public class MyJDOM
{

public MyJDOM()
{
}

public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("Usage:java MyJDOM [XML file URI]");
}

MyJDOM myJDOM = new MyJDOM();
myJDOM.parserXMLFile(args[0]);
}

/**
* 解析文档
* @param fileURI
*/
private void parserXMLFile(String fileURI)
{
try
{
//获得org.jdom.input.SAXBuilder的一个实例,用来获得XML文档。
//参数false表示不进行验证。
//参数中没有指定使用什么解析器,因此使用默认的解析器。
SAXBuilder builder = new SAXBuilder(false);
//得到Document
Document doc = builder.build(fileURI);
//名字空间
Namespace ns = Namespace.getNamespace("LIT", "http://www.lit.edu.cn/student/");
//得到根节点LIT:StuInfo
Element elmtStuInfo = doc.getRootElement();
//取得所有LIT:student节点的集合
List lstStudents = elmtStuInfo.getChildren("student", ns);
//修改bigmouse的CAD分数
for (int i = 0; i < lstStudents.size(); i++)
{
//当前学生节点
Element elmtStudent = (Element)lstStudents.getLight Bulb;

if (elmtStudent.getChildTextTrim("name", ns).equals("bigmouse"))
{
//所有课程节点的集合
List lstLesson = elmtStudent.getChildren("lesson", ns);

for (int j = 0; j < lstLesson.size(); j++)
{
Element elmtLesson = (Element)lstLesson.get(j);

if (elmtLesson.getChildTextTrim("lessonName", ns).equals("autoCAD"))
{
//修改CAD分数,100分 @_@
elmtLesson.getChild("lessonScore", ns).setText("100");
System.out.println("** autoCAD:100");
}
}
}//end if
}//end for

//添加一条学生记录
elmtStuInfo.addContent(new Element("student", ns).addContent(new Element("name", ns).setText("fannWong"))
.addContent(new Element("sex", ns).setText("female"))
.addContent(new Element("lesson", ns).addContent(new Element("lessonName", ns).setText("math"))
.addContent(new Element("lessonScore", ns).setText("85")))
.addContent(new Element("lesson", ns).addContent(new Element("lessonName", ns).setText("English"))
.addContent(new Element("lessonScore", ns).setText("95"))))
.addContent(new Element("breakLine", ns));

System.out.println("** New Element added!");

//调整一下格式,把实体引用放到最后面
//先删除,后追加。
elmtStuInfo.removeChild("master", ns);
elmtStuInfo.addContent(new Element("master", ns).addContent(new Entity("masterName")));

//输出文档
//第一个参数是缩进字符串,这里是4个空格。
//第二个参数是true,表示需要换行。
XMLOutputter printDoc = new XMLOutputter(" ", true);
printDoc.output(doc, new FileOutputStream("StuInfo.xml"));
}
catch (JDOMException jdome)
{
System.out.println(jdome.getMessage());
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
}

---------- 改变后的 StuInfo.dtd ----------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE LIT:StuInfo SYSTEM "dtd\student.dtd">
<?xml-stylesheet href="xsl\StuInfo.xsl" type="text/xsl"?>
<LIT:StuInfo xmlns:LIT="http://www.lit.edu.cn/student/">
<LIT:student>
<LIT:name>bigmouse</LIT:name>
<LIT:sex>male</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>60</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Englist</LIT:lessonName>
<LIT:lessonScore>59</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>autoCAD</LIT:lessonName>
<LIT:lessonScore>100</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>SCM</LIT:lessonName>
<LIT:lessonScore>90</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>mechanics</LIT:lessonName>
<LIT:lessonScore>61</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<LIT:breakLine />
<LIT:student>
<LIT:name>coco</LIT:name>
<LIT:sex>female</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>90</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Englist</LIT:lessonName>
<LIT:lessonScore>95</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>C++</LIT:lessonName>
<LIT:lessonScore>80</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Java</LIT:lessonName>
<LIT:lessonScore>85</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<LIT:breakLine />
<LIT:student>
<LIT:name>fannWong</LIT:name>
<LIT:sex>female</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>85</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>English</LIT:lessonName>
<LIT:lessonScore>95</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<LIT:breakLine />
<LIT:master>
&masterName; </LIT:master>
</LIT:StuInfo>



作者 Re:jdom--强大好用的xml处理工具 [Re:qilong2000]
hatpdb



发贴: 0
积分: 0
于 2003-06-02 16:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好工具,我已经用了!



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923