求一个jdom的封装类 功能读取、写入、修改。三个模块。截点可以无限延伸,千万不要写死的那种。也就是说,随便一个xml都可以读取、写入、修改。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package sample;import java.util.Stack;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.*;import org.xml.sax.helpers.DefaultHandler;public class MyXMLReader2 extends DefaultHandler { Stack<String> tags = new Stack<String>(); public MyXMLReader2() { super(); } public static void main(String args[]) { long lasting = System.currentTimeMillis(); try { SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); MyXMLReader reader = new MyXMLReader(); sp.parse(new InputSource("data_10k.xml"), reader); } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); } public void characters(char ch[], int start, int length) throws SAXException { String tag = (String) tags.peek(); if (tag.equals("NO")) { System.out.print("车牌号码:" + new String(ch, start, length)); } if (tag.equals("ADDR")) { System.out.println(" 地址:" + new String(ch, start, length)); } } public void startElement(String uri, String localName, String qName, Attributes attrs) { tags.push(qName); }} package sample;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NodeList;public class MyXMLReader { public static void main(String[] args){ long lasting =System.currentTimeMillis(); try{ File f=new File("C:/lsdj.xml"); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("VALUE"); for (int i=0;i<nl.getLength();i++){ System.out.println("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i) .getFirstChild().getNodeValue()); } }catch(Exception e){ e.printStackTrace(); } System.out.println("运行时间:"+(System.currentTimeMillis() - lasting)+"毫秒"); } } http://hi.baidu.com/zhanghao_1986/blog/item/f09dd2beb4651d1318d81fa0.htmlhttp://topic.csdn.net/u/20090504/20/59786ba6-c35a-49c4-989a-fc3d57091dd2.htmlhttp://blog.csdn.net/xcw931924821/article/details/7065234http://duan520199fy.iteye.com/blog/902783http://developer.51cto.com/art/200903/117512.htm 各位,我求的是jdom或者xml的封装工具类,而不是要一个解析的例子 http://www.4ucode.com/Study/Topic/2134455 6楼发的。。package com.jbp.xml;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.XMLOutputter;import org.jdom.xpath.XPath;/** * XML简化操作类 * * @author 杨伦亮 3:11:54 PM */public class XmlDbUntl { private static Document document = null; /** * 是否存在此文件 <br> * 如果不存在就抛出异常,存在就返回此文件 * * @param path * @return File * @throws IOException */ protected static File readFile(File file) throws IOException { try { if (file.exists()) { return file; } } catch (Exception e) { throw new IOException("找不到文件" + e.getMessage()); } throw new IOException("找不到文件"); } /** * 添加节点 * @param file * @param element * @param express * @return boolean */ @SuppressWarnings("unchecked") public static boolean appendElement(File file,Element element,String express){ try{ @SuppressWarnings("unused") List<Element> list=XPath.newInstance(express).selectNodes(fileToElement(file)); }catch (Exception e) { } return false; } /** * 读取doucment * * @param filePath * @return Document * @throws JDOMException */ public static Document fileToDocument(File file) throws JDOMException { try { document = new SAXBuilder().build(readFile(file)); return document; } catch (JDOMException e) { throw e; } catch (IOException e) { throw new JDOMException("Can't find ["+file.getPath()+"]file !"); } } /** * 自定义表达式的方式来读取配置文件信息:并指定你需要操作的文件的路径 * * @param path * xml 文件路径 * @param express * @return List<Element> * @throws JDOMException */ @SuppressWarnings("unchecked") public static List<Element> queryToElementList(File file, String express) throws JDOMException { return XPath.newInstance(express).selectNodes(fileToElement(file)); } public static Element fileToElement(File file) throws JDOMException{ return fileToDocument(file).getRootElement(); } @SuppressWarnings("unchecked") public static List<Element> fileToElementList(File file) throws JDOMException { return fileToElement(file).getChildren(); } public static List<Map<String, String>> queryToMapList(File file, String express) throws JDOMException { return elementToMap(queryToElementList(file, express)); } @SuppressWarnings("unchecked") public static List<Map<String, String>> elementToMap( List<Element> elementList) { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (Element element : elementList) { List<Element> listElement = element.getChildren(); for (Element element2 : listElement) { Map<String, String> map = new HashMap<String, String>(); map.put(element2.getName(), element2.getText()); list.add(map); } } return list; } /** * 将Document对象保存为一个xml文件到本地 * * @return true:保存成功 flase:失败 * @param filename * 保存的文件名 * @param document * 需要保存的document对象 */ public static boolean documentToFile(Document document, File tagFile) { try { new XMLOutputter().output(document, new FileOutputStream(tagFile)); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public static void main(String[] args) throws JDOMException { List<Map<String,String>> maps=XmlDbUntl.queryToMapList(new File("d:/1.xml"),"//course"); } }不管我传递什么类型值,怎么都是报错,是我不会用,还是代码有问题 如何让float型保留两位小数?谢谢!(附简码) 请教如何求两个double类型数据差的绝对值 如何新开一个打印窗口? 最近想研究JDK中的jar java中有没有提供专门的排序类呢?除Collections类 急!!日文系统,g.drawString问题 结构拷贝 紧急问题 巨分伺候 求Optimizeit 5 的注册机 我又来给分了,清说说Vector的用法和特点,最好有例子。 java扫雷程序,胜利了怎么判定,就可以弹出胜利的窗口 做一个在线视频教学平台的项目 迷茫阿 。。。。诸位专家高手 给点建议
import javax.xml.parsers.SAXParserFactory;import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;public class MyXMLReader2 extends DefaultHandler { Stack<String> tags = new Stack<String>(); public MyXMLReader2() {
super();
} public static void main(String args[]) {
long lasting = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
MyXMLReader reader = new MyXMLReader();
sp.parse(new InputSource("data_10k.xml"), reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
+ " 毫秒");
} public void characters(char ch[], int start, int length)
throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("NO")) {
System.out.print("车牌号码:" + new String(ch, start, length));
}
if (tag.equals("ADDR")) {
System.out.println(" 地址:" + new String(ch, start, length));
}
} public void startElement(String uri, String localName, String qName,
Attributes attrs) {
tags.push(qName);
}
}
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.NodeList;public class MyXMLReader {
public static void main(String[] args){
long lasting =System.currentTimeMillis();
try{
File f=new File("C:/lsdj.xml");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i=0;i<nl.getLength();i++){
System.out.println("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
System.out.println("车主地址:"
+ doc.getElementsByTagName("ADDR").item(i)
.getFirstChild().getNodeValue());
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("运行时间:"+(System.currentTimeMillis() - lasting)+"毫秒");
}
}
package com.jbp.xml;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;/**
* XML简化操作类
*
* @author 杨伦亮 3:11:54 PM
*/
public class XmlDbUntl {
private static Document document = null;
/**
* 是否存在此文件 <br>
* 如果不存在就抛出异常,存在就返回此文件
*
* @param path
* @return File
* @throws IOException
*/
protected static File readFile(File file) throws IOException {
try {
if (file.exists()) {
return file;
}
} catch (Exception e) {
throw new IOException("找不到文件" + e.getMessage());
}
throw new IOException("找不到文件");
}
/**
* 添加节点
* @param file
* @param element
* @param express
* @return boolean
*/
@SuppressWarnings("unchecked")
public static boolean appendElement(File file,Element element,String express){
try{
@SuppressWarnings("unused")
List<Element> list=XPath.newInstance(express).selectNodes(fileToElement(file));
}catch (Exception e) {
}
return false;
}
/**
* 读取doucment
*
* @param filePath
* @return Document
* @throws JDOMException
*/
public static Document fileToDocument(File file) throws JDOMException {
try {
document = new SAXBuilder().build(readFile(file));
return document;
} catch (JDOMException e) {
throw e;
} catch (IOException e) {
throw new JDOMException("Can't find ["+file.getPath()+"]file !");
}
} /**
* 自定义表达式的方式来读取配置文件信息:并指定你需要操作的文件的路径
*
* @param path
* xml 文件路径
* @param express
* @return List<Element>
* @throws JDOMException
*/
@SuppressWarnings("unchecked")
public static List<Element> queryToElementList(File file, String express)
throws JDOMException {
return XPath.newInstance(express).selectNodes(fileToElement(file));
}
public static Element fileToElement(File file) throws JDOMException{
return fileToDocument(file).getRootElement();
}
@SuppressWarnings("unchecked")
public static List<Element> fileToElementList(File file)
throws JDOMException {
return fileToElement(file).getChildren();
} public static List<Map<String, String>> queryToMapList(File file,
String express) throws JDOMException {
return elementToMap(queryToElementList(file, express));
} @SuppressWarnings("unchecked")
public static List<Map<String, String>> elementToMap(
List<Element> elementList) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Element element : elementList) {
List<Element> listElement = element.getChildren();
for (Element element2 : listElement) {
Map<String, String> map = new HashMap<String, String>();
map.put(element2.getName(), element2.getText());
list.add(map);
}
}
return list;
} /**
* 将Document对象保存为一个xml文件到本地
*
* @return true:保存成功 flase:失败
* @param filename
* 保存的文件名
* @param document
* 需要保存的document对象
*/
public static boolean documentToFile(Document document, File tagFile) {
try {
new XMLOutputter().output(document, new FileOutputStream(tagFile));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws JDOMException {
List<Map<String,String>> maps=XmlDbUntl.queryToMapList(new File("d:/1.xml"),"//course");
}
}
不管我传递什么类型值,怎么都是报错,是我不会用,还是代码有问题