当前位置:编程学习 > JAVA >>

求一个jdom的封装类

功能读取、写入、修改。三个模块。
截点可以无限延伸,千万不要写死的那种。
也就是说,随便一个xml都可以读取、写入、修改。 --------------------编程问答-------------------- --------------------编程问答--------------------
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.html

http://topic.csdn.net/u/20090504/20/59786ba6-c35a-49c4-989a-fc3d57091dd2.html

http://blog.csdn.net/xcw931924821/article/details/7065234

http://duan520199fy.iteye.com/blog/902783

http://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");
}
    

}


不管我传递什么类型值,怎么都是报错,是我不会用,还是代码有问题
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,