请教一个eclipse 中应用jdom 的问题

fush76 2010-02-26 09:07:57
我在 eclipse 里面要实现 jdom 解析XML,在 窗口---》首选项》---》JAVA》--》构建路径 》--用户库 中增加了 jdom 的所在路径(E:\projects\readxml\).

PATH=c:\jdk1.5;C:\jdk1.5\bin;
classpath=.;%catalina_home%\common\lib\serlet-api.jar;%catalina_home%\common\lib\

项目所在目录:e:\projects\readxml
将jdom1.0.zip 中的 jdom.jar 放在 readxml 目录。



但eclipse 对以下三行报错:无法解析导入 org.jdom
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;






程序如下(程序转自网络):

package com.duduli.xml;

import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;


public class JDomParse {
@SuppressWarnings("unchecked")
JDomParse(String path){
SAXBuilder builder = new SAXBuilder(false);
try {
Document doc = builder.build(path);
Element persons = doc.getRootElement();
List list = persons.getChildren("person");
for(Iterator it = list.iterator();it.hasNext();){
Element person = (Element) it.next();
// getArrtibuteValue得到的是属性值
String email = person.getAttributeValue("email");
System.out.println(email);
String name = person.getChildTextTrim("name");
System.out.println(name);
String sex = person.getChildTextTrim("sex");
System.out.println(sex);
String age = person.getChildTextTrim("age");
System.out.println(age);
String className = person.getChildTextTrim("class");
System.out.println(className);
System.out.println();
}
XMLOutputter out = new XMLOutputter();
out.output(doc, new FileOutputStream(path));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new JDomParse("src\\com\\duduli\\xml\\person.xml");

}

}

...全文
107 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fush76 2010-02-26
  • 打赏
  • 举报
回复
感谢 二位,问题已解决。
bayougeng 2010-02-26
  • 打赏
  • 举报
回复
新建用户库不代表一定就加入到你的项目中了。
用户库只是将一组你常用的,有一定联系的jar或路径组合在一起打包,方便你以后添加。
你应该将这个jar拷贝到项目的lib目录中,然后通过Add jars来添加。这样是比较规范的。
霜之哀伤 2010-02-26
  • 打赏
  • 举报
回复
首先,首选项里不用加。
然后,项目右键-->Build Path->Config Build-path

58,454

社区成员

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

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