⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java中合并xml文档的设计与实现 - fanqiang_com.htm

📁 详细的讲述了XML相关知识
💻 HTM
📖 第 1 页 / 共 2 页
字号:
                  messageItems.getLength 
                  ();<BR> //如果去掉根节点下的第一个元素,比如&lt;所属管理系统&gt; 
                  ,那么i从3开始。否则i从1开始。<BR> For (int i=1; i &lt; item_number; i=i+2 
                  ) {<BR>  //调用dupliate(),依次复制被合并XML文档中根节点下的元素。 <BR>  Element 
                  messageItem = (Element) messageItems.item (i);<BR>  IsOver = 
                  dupliate (doc_main, root_main, messageItem);<BR> }<BR> //调用 
                  write To(),将合并得到的Document写入目标XML文档。<BR> Boolean isWritten = 
                  write To (doc_main, mainFileName);<BR> Return isOver 
                  &amp;&amp; isWritten; 
            <BR>}</TD></TR></TBODY></TABLE><BR>  方法dupliate 
            ()有三个参数(分别是目标XML文档的Document,目标XML文档中要添加节点的父节点和被合并XML文档的复制节点),采用递归的形式,将一个XML文档中的元素复制到另一个XML文档中。<BR><BR>
            <TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf 
            border=1>
              <TBODY>
              <TR>
                <TD>Private Boolean dupliate (Document doc_dup, Element 
                  father, Element son) throws Exception {<BR> Boolean is done = 
                  false;<BR> String son_name = son.getNodeName ();<BR> Element 
                  sub ITEM = doc_dup.createElement 
                  (son_name);<BR> //复制节点的属性<BR> If (son.hasAttributes 
                  ()){<BR>  NamedNodeMap attributes = son.getAttributes 
                  ();<BR>  For (int i=0; i &lt; attributes.getLength () ; i 
                  ++){<BR>   String attribute_name = attributes. Item (i). 
                  GetNodeName ();<BR>   String attribute_value = attributes. 
                  Item (i). GetNodeValue ();<BR>   SubITEM.setAttribute 
                  (attribute_name, 
                  attribute_value);<BR>  }<BR> }<BR> Father.appendChild (sub 
                  ITEM);<BR> //复制节点的值<BR> Text value son = (Text) 
                  son.getFirstChild ();<BR> String nodevalue_root = "";<BR> If 
                  (value_son! = null &amp;&amp; value_son.getLength () &gt; 0) 
                  nodevalue_root = (String) value_son.getNodeValue ();<BR> Text 
                  valuenode_root = null;<BR> If ((nodevalue_root! = 
                  null)&amp;&amp;(nodevalue_root.length () &gt; 0)) 
                  valuenode_root = doc_dup.createTextNode 
                  (nodevalue_root);<BR> If (valuenode_root! = null &amp;&amp; 
                  valuenode_root.getLength () &gt; 0) subITEM.appendChild 
                  (valuenode_root);<BR> //复制子结点<BR> Novelist sub_messageItems = 
                  son.getChildNodes ();<BR> int sub_item_number = 
                  sub_messageItems.getLength();<BR> if (sub_item_number &lt; 
                  2){<BR>  //如果没有子节点,则返回<BR>  Is done = true;<BR> }<BR> Else 
                  {<BR>  For (int j = 1; j &lt; sub_item_number; j=j+2) 
                  {<BR>   //如果有子节点,则递归调用本方法 <BR>   Element sub_messageItem = 
                  (Element) sub_messageItems.item (j);<BR>   Is done = dupliate 
                  (doc_dup, subITEM, sub_messageItem);<BR>  }<BR> }<BR> Return 
                  is 
            done;<BR>}</TD></TR></TBODY></TABLE><BR>  方法writeTo()有两个参数(分别是目标XML文档的Document和文件名),将所得目标XML文档写入文件。<BR><BR>
            <TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf 
            border=1>
              <TBODY>
              <TR>
                <TD>Private Boolean write To (Document doc, String fileName) 
                  throws Exception {<BR> Boolean isOver = false;<BR> DOM Source 
                  doms = new DOM Source (doc);<BR> File f = new File 
                  (fileName);<BR> Stream Result sr = new Stream Result 
                  (f);<BR> Try<BR> {<BR>  Transformer Factory 
                  tf=TransformerFactory.newInstance ();<BR>  Transformer 
                  t=tf.newTransformer ();<BR>  Properties properties = 
                  t.getOutputProperties ();<BR>  Properties.setProperty 
                  (OutputKeys.ENCODING,"GB2312");<BR>  T.setOutputProperties 
                  (properties);<BR>  T.transform (doms, sr);<BR>  IsOver = 
                  true;<BR> }<BR> Catch (TransformerConfigurationException 
                  tce)<BR> {<BR>  Tce.printStackTrace ();<BR> }<BR> Catch 
                  (Transformer Exception te)<BR> {<BR>  Te.printStackTrace 
                  ();<BR> }<BR> Return 
            isOver;<BR>}</TD></TR></TBODY></TABLE><BR>  最后使用测试函数进行测试。对于两个已经存在的XML文件(比如,存在文件D:/a.xml和D:/b.xml,要将b.xml合并到a.xml中),可以测试如下:<BR><BR>
            <TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf 
            border=1>
              <TBODY>
              <TR>
                <TD>Public static void main (String [] args) throws Exception 
                  {<BR> Boolean is done = is Merging 
                  ("D:/a.xml","D:/b.xml");<BR> If (is Done) System.out.println 
                  ("XML files have been merged.");<BR> Else System.out.println 
                  ("XML files have NOT been 
            merged.");<BR>}</TD></TR></TBODY></TABLE><BR>  <B>总结</B><BR><BR>  本文介绍了如何利用JAVA中的XML解析器,合并两个XML文档。当然,在合并的过程中,还可以加入其他的约束条件,比如要求过滤掉特定的元素等。另外,复制元素的插入位置也可以加以限制。<BR><!-- 正文end --><BR>(http://www.fanqiang.com)<BR></FONT><BR><FONT 
            color=#999999><SMALL>原文链接:<A 
            href="http://dev.yesky.com/490/2013490.shtml" 
            target=_blank>http://dev.yesky.com/490/2013490.shtml</A></SMALL></FONT> 
            <BR></FONT></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=750 border=0>
  <TBODY>
  <TR>
    <TD align=middle width=620>
      <TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
        <TBODY>
        <TR>
          <TD width=562>
            <TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
              <TBODY>
              <TR>
                <TD>
                  <TABLE cellSpacing=1 cellPadding=5 width="100%" border=0>
                    <TBODY>
                    <TR>
                      <TD class=f14 bgColor=#666666><B><FONT color=#ffffff 
                        size=2>&nbsp;<B>相关文章</B></FONT></B></TD></TR>
                    <TR>
                      <TD class=f14><A 
                        href="http://fanqiang.chinaunix.net/program/html/2001-10-31/1981.shtml" 
                        target=_blank>什么是XML?</A><FONT 
                        color=gray>&nbsp;2001-10-31 11:42:01</FONT> <BR><A 
                        href="http://fanqiang.chinaunix.net/program/html/2001-05-13/1982.shtml" 
                        target=_blank>XML初学者必读</A><FONT 
                        color=gray>&nbsp;2001-05-13 08:28:00</FONT> <BR><A 
                        href="http://fanqiang.chinaunix.net/program/html/2001-10-29/2262.shtml" 
                        target=_blank>中文 XML FAQ</A><FONT 
                        color=gray>&nbsp;2001-10-29 09:00:00</FONT> <BR><A 
                        href="http://fanqiang.chinaunix.net/program/html/2002-01-29/2287.shtml" 
                        target=_blank>使用Perl创建VoiceXML应用程序</A><FONT 
                        color=gray>&nbsp;2002-01-29 20:54:06</FONT> <BR><A 
                        href="http://fanqiang.chinaunix.net/program/html/2002-01-29/2291.shtml" 
                        target=_blank>用XML优化二次检索</A><FONT 
                        color=gray>&nbsp;2002-01-29 20:53:13</FONT> <BR><A 
                        href="http://fanqiang.chinaunix.net/program/php/2002-01-31/2315.shtml" 
                        target=_blank>用PHP实现XML备份Mysql数据库</A><FONT 
                        color=gray>&nbsp;2002-01-31 
                    10:41:13</FONT><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD height=10></TD></TR></TBODY></TABLE><!--结束:底部-->
      <TABLE width=750 border=0>
        <TBODY>
        <TR>
          <TD width="100%" bgColor=#d09f0d colSpan=5 height=2><IMG height=1 
            src="Java中合并XML文档的设计与实现 - fanqiang_com.files/c.gif" width=1></TD></TR>
        <TR>
          <TD vAlign=top width="100%" colSpan=5 height=40>
            <P align=center><FONT 
            color=#ffffff>★&nbsp;&nbsp;感谢所有的作者为我们学习技术知识提供了一条捷径&nbsp;&nbsp;★ 
            <BR>www.fanqiang.com</FONT></P></TD></TR></TBODY></TABLE>
      <CENTER></CENTER></TR></TBODY></TABLE></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -