今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐Java中合并XML文档的设计与实现,希望大家看完后也有个好心情,快快行动吧!
【 tulaoshi.com - 编程语言 】
摘 要:介绍了XML应用中合并XML文档的方法与应用,在基于XML的应用中,有着广泛的应用前景。Import java.io. *; //Java基础包,包含各种IO操作
Import java.util. *; //Java基础包,包含各种标准数据结构操作
Import javax.xml.parsers. *; //XML解析器接口
Import org.w3c.dom. *; //XML的DOM实现
import org.apache.crimson.tree.XmlDocument;//写XML文件要用到
Import javax.xml.transform. *;
Import javax.xml.transform.dom. *;
Import javax.xml.transform.stream. *;
Private Boolean is Merging (String mainFileName, String subFilename) throws Exception {
Boolean isOver = false;
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document Builder db = null;
Try {
Db = dbf.newDocumentBuilder ();
} Catch (ParserConfigurationException pce) {
System.err.println(pce); //出现异常时,输出异常信息
}
Document doc_main = null,doc_vice= null;
//获取两个XML文件的Document。
Try {
Doc_main = db.parse (mainFileName);
Doc_vice = db.parse (sub Filename);
} Catch (DOM Exception dom) {
System.err.println (dom.getMessage ());
} Catch (Exception ioe) {
System.err.println (ioe);
}
//获取两个文件的根元素。
Element root_main = doc_main.getDocumentElement ();
Element root_vice = doc_vice.getDocumentElement ();
//下面添加被合并文件根节点下的每个元素
Novelist message Items = root_vice.getChildNodes ();
Int item_number = messageItems.getLength ();
//如果去掉根节点下的第一个元素,比如所属管理系统,那么i从3开始。否则i从1开始。
For (int i=1; i item_number; i=i+2 ) {
//调用dupliate(),依次复制被合并XML文档中根节点下的元素。
Element messageItem = (Element) messageItems.item (i);
IsOver = dupliate (doc_main, root_main, messageItem);
}
//调用 write To(),将合并得到的Document写入目标XML文档。
Boolean isWritten = write To (doc_main, mainFileName);
Return isOver && isWritten;
}
Private Boolean dupliate (Document doc_dup, Element father, Element son) throws Exception {
Boolean is done = false;
String son_name = son.getNodeName ();
Element sub ITEM = doc_dup.createElement (son_name);
//复制节点的属性
If (son.hasAttributes ()){
NamedNodeMap attributes = son.getAttributes ();
For (int i=0; i attributes.getLength () ; i ++){
String attribute_name = attributes. Item (i). GetNodeName ();
String attribute_value= attributes. Item (i). GetNodeValue ();
SubITEM.setAttribute (attribute_name, attribute_value);
}
}
Father.appendChild (sub ITEM);
//复制节点的值
Textvalue son = (Text) son.getFirstChild ();
String nodevalue_root = "";
If (value_son! = null && value_son.getLength () 0) nodevalue_root = (String) value_son.getNodeValue ();
Text valuenode_root = null;
If ((nodevalue_root! = null)&&(nodevalue_root.length () 0)) valuenode_root = doc_dup.createTextNode (nodevalue_root);
If (valuenode_root! = null && valuenode_root.getLength () 0) subITEM.appendChild (valuenode_root);
//复制子结点
Novelist sub_messageItems = son.getChildNodes ();
int sub_item_number = sub_messageItems.getLength();
if (sub_item_number 2){
//如果没有子节点,则返回
Is done = true;
}
Else {
For (int j = 1; j sub_item_number; j=j+2) {
//如果有子节点,则递归调用本方法
Element sub_messageItem = (Element) sub_messageItems.item (j);
Is done = dupliate (doc_dup, subITEM, sub_messageItem);
}
}
Return is done;
}
Private Boolean write To (Document doc, String fileName) throws Exception {
Boolean isOver = false;
DOM Source doms = new DOM Source (doc);
File f = new File (fileName);
Stream Result sr = new Stream Result (f);
Try
{
Transformer Factory tf=TransformerFactory.newInstance ();
Transformer t=tf.newTransformer ();
Properties properties = t.getOutputProperties ();
Properties.setProperty (OutputKeys.ENCODING,"GB2312");
T.setOutputProperties (properties);
T.transform (doms, sr);
IsOver = true;
}
Catch (TransformerConfigurationException tce)
{
Tce.printStackTrace ();
}
Catch (Transformer Exception te)
{
Te.printStackTrace ();
}
Return isOver;
}
Publicstatic voidmain (String [] args) throws Exception {
Boolean is done = is Merging ("D:/a.xml","D:/b.xml");
If (is Done) System.out.println ("XML files have been merged.");
Else System.out.println ("XML files have NOTbeen merged.");
}
来源:http://www.tulaoshi.com/n/20160219/1600376.html
看过《Java中合并XML文档的设计与实现》的人还看了以下文章 更多>>