jdom解析
package com.aisino.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import com.aisino.util.CommonUtil;
/**
*
* 功能概要描述
*
* @category:java学习.jdom.xml
* @author <a href="mailto:han_huayi@163.com">hanhuayi</a>
* @version v1.0,Aug 16, 2013 2:45:16 PM
* @see
*/
public class XMLParser {
static{
//手工加载配置文件
PropertyConfigurator.configureAndWatch("F:\\workspace\\JavaSample\\log4jj.properties");
}
//文件路径
final static String filePath = "F:\\workspace\\JavaSample\\src\\com\\aisino\\xml\\";
/**
* log4j
*/
private static Logger logger = Logger.getLogger(XMLParser.class);
/**
*
* 方法功能描述
*
* @param args
* @throws Exception
* @return void
* @exception 异常描述
* @see
*/
public static void main(String[] args) throws Exception {
//读文件到内存
Document doc = new XMLParser().readXMLFileToDocument(filePath + "students.xml");
//写Document到文件
new XMLParser().writeDocumentToFile(doc, filePath + "service.xml");
//查询Document
new XMLParser().queryXML(doc);
//添加元素和属性
new XMLParser().addElementAndAttributeToDocument(doc);
//克隆
new XMLParser().copyElementAndToTree(doc);
//修改XML
new XMLParser().updateXML(doc);
//删除XML元素和属性
new XMLParser().removeXMLContent(doc);
new XMLParser().writeDocumentToFile(doc, filePath + "service.xml");
//内存中的Document->String
String docS = new XMLOutputter().outputString(doc);
logger.info("docs:" + docS);
//String->Document
Document doc1 = new SAXBuilder().build(new StringReader(docS));
new XMLParser().writeDocumentToFile(doc1, "StringToDocument.xml");
}
/**
*
* 读xml文件到内存
*
* @param fileName 文件名
* @return
* @return Document
* @exception 异常描述
* @see
*/
public Document readXMLFileToDocument(String fileName) {
SAXBuilder saxb = new SAXBuilder();
try {
Document doc = saxb.build(new File(fileName));
return doc;
} catch (JDOMException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
return null;
}
/**
*
* 写Document到文件
*
* @param doc 文件
* @param fileName 文件名
* @return void
* @exception 异常描述
* @see
*/
public void writeDocumentToFile(Document doc, String fileName) {
XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
try {
xo.output(doc, new FileWriter(new File(fileName)));
} catch (IOException e) {
//e.printStackTrace();
}
}
/**
*
* 查询Document
*
* @param doc
* @return void
* @exception 异常描述
* @see
*/
@SuppressWarnings("unchecked")
public void queryXML(Document doc) {
//获得根元素
Element rootElement = doc.getRootElement();
logger.info("rootElement:" + rootElement);
//获得根元素的所有孩子元素
List rootChildren = rootElement.getChildren();
logger.info("rootChildren:" + rootChildren);
//获得元素的指定名称的所有元素
List studentChildren = rootElement.getChildren("student");
logger.info("studentChildren:" + studentChildren);
//获得指定名称的第一个元素
Element studentChild = rootElement.getChild("student");
logger.info("studentChild:" + studentChild);
//对studnetChildren进行迭代
Iterator studentChildIte = studentChildren.iterator();
while(studentChildIte.hasNext()){
Element studentElement = (Element)studentChildIte.next();
//获得元素名称和值
String studentName = studentElement.getName();
String studentValue = studentElement.getValue();
logger.info("studentName:" + studentName);
logger.info("studentValue:" + studentValue);
//获得元素的所有属性
List studentAttributes = studentElement.getAttributes();
Attribute currentAttribute = studentElement.getAttribute("studentid");
logger.info("currentAttribute:" + currentAttribute);
if(studentAttributes != null){
Iterator studentAttrIte = studentAttributes.iterator();
while(studentAttrIte.hasNext()){
Attribute currentAttr = (Attribute)studentAttrIte.next();
//取得属性的名称和值
String curAttrName = currentAttr.getName();
String curAttrValue = currentAttr.getValue();
logger.info("curAttrName:" + curAttrName + " curAttrValue:" + curAttrValue);
}
}
}
}
/**
*
* 向XML中添加元素和属性
*
* @param doc
* @return void
* @exception 异常描述
* @see
*/
@SuppressWarnings("unchecked")
private void addElementAndAttributeToDocument(Document doc) {
//根元素
Element rootElement = doc.getRootElement();
//新元素
Element companyElement = new Element("aisino");
rootElement.addContent(companyElement);
//添加文本值
companyElement.setText(CommonUtil.setStrUTF8Encode("航天信息软件"));
//【第一种】添加属性
Attribute addressid = new Attribute("addressid",CommonUtil.setStrUTF8Encode("杏石口路甲18号"));
Attribute companygender = new Attribute("gender","3");
companyElement.setAttribute(addressid);
companyElement.setAttribute(companygender);
//【第二种】添加属性
List companyAttrsList = companyElement.getAttributes();
Attribute age = new Attribute("age","5");
companyAttrsList.add(age);
Attribute people = new Attribute("people","200");
companyAttrsList.add(people);
}
/**
*
* 克隆(复制)XML元素
*
* @param doc
* @return void
* @exception 异常描述
* @see
*/
private void copyElementAndToTree(Document doc) {
Element rootElement = doc.getRootElement();
//获得指定元素的第一个元素
Element studentElement = rootElement.getChild("student");
//克隆,复制
Element cloneStudentElement = (Element)studentElement.clone();
rootElement.addContent(cloneStudentElement);
cloneStudentElement.setText("hanhuayi");
cloneStudentElement.getAttribute("studentid").setValue("4");
}
/**
*
* 修改XML
*
* @param doc
* @return void
* @exception 异常描述
* @see
*/
private void updateXML(Document doc) {
Element rootElement = doc.getRootElement();
//获得指定名称的第一个孩子元素
Element studentElement = (Element)rootElement.getChild("student");
studentElement.setName("stud");
studentElement.setText("newText");
studentElement.getAttribute("studentid").setValue("11");
studentElement.getAttribute("age").setValue("201");
}
/**
*
* 删除XML元素
*
* @param doc
* @return void
* @exception 异常描述
* @see
*/
private void removeXMLContent(Document doc) {
Element rootElement = doc.getRootElement();
//获得指定元素的第一个元素
Element studentElement = rootElement.getChild("student");
rootElement.removeContent(studentElement);
}
}